2013-08-21 76 views
1

我有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]; 


    } 
} 

回答

14

自動佈局的第一條規則是,你不能碰的幀。

您無法更改自動佈局層次結構中視圖的框架,邊界或中心。

讀你的代碼有點混亂,所以我不能100%確定你實際上在做什麼動畫。但是,你需要做的是去除所有的線,如...

pageContainer.frame = CGRectMake(pageContainer.frame.origin.x, pageContainer.frame.origin.y, pageContainerSize.width, pageContainerSize.height); 

當你想打動你的需要,以使新的限制意味着新的佈局更新限制在屏幕上的東西。

舉例來說,如果你想動畫視圖的高度,那麼你需要......

保持到自動佈局約束參考...

@property (nonatomic, strong) NSLayoutConstraint *heightConstraint; 

或...

// if you're creating the constraint in Interface Builder then CTRL drag it like any other outlet. 
@property (nonatomic, weak) IBOutlet NSLayoutConstraint *heightConstraint; 

然後更改約束的constant屬性。

// animate the height to 150. 
heightConstraint.constant = 150; 

和動畫的佈局變化...

[UIView animateWithDuration:5.0 
         delay:0.0 
        options: UIViewAnimationOptionBeginFromCurrentState 
       animations:^{ 
        [pageContainer.superview layoutIfNeeded]; 
       } 
       completion:^(BOOL finished){ 
       }]; 

一旦你知道哪些約束需要改變你的工作如何打造你的約束,使他們能夠進行動畫處理。

編輯

你還需要確保你關閉自動調整大小面具翻譯...

[someView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

我就使代碼更易讀,雖然工作。可能移動到不同的功能,以減少代碼複製等...

這是目前閱讀困惑。

+0

我已添加CGSize pageContainerSize = [pageContainer systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 012Container.frame = CGRectMake(pageContainer。frame.origin.x,pageContainer.frame.origin.y,pageContainerSize.width,pageContainerSize.height); – Rajashekar

+0

對不起,我已經添加了上面的代碼來找到intrisinc內容大小並將其設置爲pagecontainer。我不能看到沒有它的視圖框架。是的,我已經爲我所有的視圖設置了setTranslatesAutoresizingMaskToConstraints屬性。我不知道現在該做什麼。 – Rajashekar

+0

未找到'setTranslatesAutoresizingMaskToConstraints'。你是不是指'setTranslatesAutoresizingMaskIntoConstraints'? – Pang