如何使用 ViewWillLayoutSubview 为 UIView 框架设置动画

Posted

技术标签:

【中文标题】如何使用 ViewWillLayoutSubview 为 UIView 框架设置动画【英文标题】:How To Animate UIView Frame with ViewWillLayoutSubview 【发布时间】:2018-02-04 08:40:50 【问题描述】:

我以编程方式创建了两个 UIView,并在 ViewWillLayoutSubview 函数中设置了它的框架,现在我想创建一个交换动画,以便每个视图通过单击动画交换彼此的位置。为了获得这个动画,我必须在动画中交换它的帧,但同时调用 ViewWillLayoutSubview 将帧设置为初始位置。

如何使用 ViewWillLayoutSubview 让我的 UIView 与动画交换?

我没有使用任何类型的约束或 xib 或故事板。整个屏幕都是程序化设计的。

提前致谢。

【问题讨论】:

【参考方案1】:
-(void)viewWillLayoutSubviews

    if(Once)
   
       // create 2 views here

      once = NO;
   


将此代码放入按钮点击

CGRect view2Frame = self.view2.frame;

[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveLinear  animations:^
    //code with animation


      self.view2.frame = self.view1.frame;

      self.view1.frame = view2Frame;


 completion:^(BOOL finished) 
    //code for completion
];

【讨论】:

UIview动画块应该写什么?

以上是关于如何使用 ViewWillLayoutSubview 为 UIView 框架设置动画的主要内容,如果未能解决你的问题,请参考以下文章