我有3個視圖層次結構(彈出窗口,頁面容器,瀏覽量),pageview放置在pagecontainer內,pagecontainer放置在popover內。所有這些都是UIView類。Autolayout動畫問題
我在pagecontainer上添加了輕掃手勢。當發生滑動時,頁面視圖將被替換爲另一個頁面視圖。我正在嘗試在我滑動後立即獲取動畫。它從當前狀態開始向左滑動,但是當我向右滑動時它會混亂,並且在所有滑動之後繼續保持混亂狀態。由於某些原因,動畫不一致。
下面是代碼,我試圖把約束內的動畫塊,它沒有任何區別。
- (IBAction)swipeLeft:(id)sender {
if (_currentPage < [_pages count] - 1) {
UIView *currentView = _pages[_currentPage];
UIView *pageContainer = [currentView superview];
[currentView removeFromSuperview];
++_currentPage;
if (_pageControl)
_pageControl.currentPage = _currentPage;
UIView *newPage = _pages[_currentPage];
[pageContainer addSubview:newPage];
newPage.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
newPage.frame = CGRectIntegral(newPage.frame);
[pageContainer.superview layoutIfNeeded];
NSDictionary *pageviews = NSDictionaryOfVariableBindings(newPage);
if (SYSTEM_VERSION_LESS_THAN(@"7.0"))
[pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10.0-[newPage]-10.0-|" options:0 metrics:nil views:pageviews]];
else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
[pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0.0-[newPage]-0.0-|" options:0 metrics:nil views:pageviews]];
[pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0.0-[newPage]-0.0-|" options:0 metrics:nil views:pageviews]];
CGSize pageContainerSize = [pageContainer systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
pageContainer.frame = CGRectMake(pageContainer.frame.origin.x, pageContainer.frame.origin.y, pageContainerSize.width, pageContainerSize.height);
[UIView animateWithDuration:5.0
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^{
[pageContainer.superview layoutIfNeeded];
}
completion:^(BOOL finished){
}];
UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[self adjustPopoverOrientationWithCurrentOrientation:window];
}
}
和...
- (IBAction)swipeRight:(id)sender {
if (_currentPage > 0) {
UIView *currentView = _pages[_currentPage];
UIView *pageContainer = [currentView superview];
[currentView removeFromSuperview];
--_currentPage;
if (_pageControl)
_pageControl.currentPage = _currentPage;
UIView *newPage = _pages[_currentPage];
[pageContainer addSubview:newPage];
newPage.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
newPage.frame = CGRectIntegral(newPage.frame);
[pageContainer.superview layoutIfNeeded];
NSDictionary *pageviews = NSDictionaryOfVariableBindings(newPage);
if (SYSTEM_VERSION_LESS_THAN(@"7.0"))
[pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10.0-[newPage]-10.0-|" options:0 metrics:nil views:pageviews]];
else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
[pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0.0-[newPage]-0.0-|" options:0 metrics:nil views:pageviews]];
[pageContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0.0-[newPage]-0.0-|" options:0 metrics:nil views:pageviews]];
CGSize pageContainerSize = [pageContainer systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
pageContainer.frame = CGRectMake(pageContainer.frame.origin.x, pageContainer.frame.origin.y, pageContainerSize.width, pageContainerSize.height);
[UIView animateWithDuration:5.0
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^{
[pageContainer.superview layoutIfNeeded];
}
completion:^(BOOL finished){
}];
UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[self adjustPopoverOrientationWithCurrentOrientation:window];
}
}
我已添加CGSize pageContainerSize = [pageContainer systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 012Container.frame = CGRectMake(pageContainer。frame.origin.x,pageContainer.frame.origin.y,pageContainerSize.width,pageContainerSize.height); – Rajashekar
對不起,我已經添加了上面的代碼來找到intrisinc內容大小並將其設置爲pagecontainer。我不能看到沒有它的視圖框架。是的,我已經爲我所有的視圖設置了setTranslatesAutoresizingMaskToConstraints屬性。我不知道現在該做什麼。 – Rajashekar
未找到'setTranslatesAutoresizingMaskToConstraints'。你是不是指'setTranslatesAutoresizingMaskIntoConstraints'? – Pang