2017-11-03 6 views
0

UIViewの制約を更新してアニメーションを作成しようとしています。最初に画面の下にビューを配置し(画面外)、上の制約を0に設定して、アニメーションがビューを上に移動するようにします。Slide UIViewで制約を更新して

私は運が一切なく、アニメーションが正しくありませんでした。

CGRect screenBound = [[UIScreen mainScreen] bounds]; 
     CGSize screenSize = screenBound.size; 
     //CGFloat screenWidth = screenSize.width; 
     CGFloat screenHeight = screenSize.height; 

     self.viewSearchWrapper.translatesAutoresizingMaskIntoConstraints = NO; 

     [self.view addSubview:self.viewSearchWrapper]; 

     NSLayoutConstraint *trailing =[NSLayoutConstraint 
             constraintWithItem:self.viewSearchWrapper 
             attribute:NSLayoutAttributeTrailing 
             relatedBy:NSLayoutRelationEqual 
             toItem:self.view 
             attribute:NSLayoutAttributeTrailing 
             multiplier:1.0f 
             constant:0.f]; 

     //Leading 

     NSLayoutConstraint *leading = [NSLayoutConstraint 
             constraintWithItem:self.viewSearchWrapper 
             attribute:NSLayoutAttributeLeading 
             relatedBy:NSLayoutRelationEqual 
             toItem:self.view 
             attribute:NSLayoutAttributeLeading 
             multiplier:1.0f 
             constant:0.f]; 

     //Bottom 
     NSLayoutConstraint *bottom =[NSLayoutConstraint 
            constraintWithItem:self.viewSearchWrapper 
            attribute:NSLayoutAttributeBottom 
            relatedBy:NSLayoutRelationEqual 
            toItem:self.view 
            attribute:NSLayoutAttributeBottom 
            multiplier:1.0f 
            constant:0.f]; 

     //Bottom 
     NSLayoutConstraint *top =[NSLayoutConstraint 
            constraintWithItem:self.viewSearchWrapper 
            attribute:NSLayoutAttributeTop 
            relatedBy:NSLayoutRelationEqual 
            toItem:self.view 
            attribute:NSLayoutAttributeTop 
            multiplier:1.0f 
            constant:screenHeight]; 

     [self.view addConstraint:trailing]; 
     [self.view addConstraint:bottom]; 
     [self.view addConstraint:leading]; 
     [self.view addConstraint:top]; 



     [self.viewSearchWrapper setNeedsUpdateConstraints]; 

     [top setConstant:0.f]; 

     [UIView animateWithDuration:1 animations:^{ 
      [self.view layoutIfNeeded]; 
     }]; 
+0

上限制約を減らすだけです。それに伴って下部の制約を増やしてみてください。あるいは、bottom制約のために 'equal'の代わりに' greaterThanOrEqual'関係を設定することもできます。 – execv

答えて

2

最初viewSearchWrapperに初期制約を設定するlayoutIfNeededを追加します。

// .... keep the code assigning the constraints as it is. 

[self.view layoutIfNeeded]; // This will set the initial frame of viewSearchWrapper 
[self.viewSearchWrapper setNeedsUpdateConstraints]; 
[top setConstant:0.f]; 
[UIView animateWithDuration:1 animations:^{ 
    [self.view layoutIfNeeded]; 
}]; 
0

「[top setConstant:0.f];」を追加します。 inside animateWithDuration以下に示すように

[UIView animateWithDuration:1 animations:^{ 
     [top setConstant:0.f]; 
     [self.view layoutIfNeeded]; 
    }]; 
関連する問題