简体   繁体   English

在 UIView.animateWithDuration 的完成块中添加视图

[英]Adding a view in a completion block of UIView.animateWithDuration

I have a view which I want to move to the center of the main view of the ViewController .我有一个视图,我想将其移动到ViewController的主视图的中心。 I want to do this by using UIView.animateWithDuration .我想通过使用UIView.animateWithDuration来做到这UIView.animateWithDuration In the completion block of the method I want to add a second view.在方法的完成块中,我想添加第二个视图。

Below the code from my ViewController class.在我的ViewController类的代码下方。 I have setup the view in the storyboard and connected it via the Outlet我已经在故事板中设置了视图并通过插座连接它

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.



}


override func viewDidAppear(animated: Bool) {
        animation()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBOutlet weak var card: UIView!



func animation() {
    print("viewCenter  \(self.view.center)")
    print("cardCenter 1 \(self.card.center)")

    UIView.animateWithDuration(1.0,
        delay: 2.0,
        options: UIViewAnimationOptions.CurveLinear,
        animations: { () -> Void in
            self.card.center = self.view.center
            print("number of constraints on card: \(self.card.constraints.count)")
        },
        completion: { finished in
            print("cardCenter 2 \(self.card.center)")

                let rect = CGRect(x: 200, y: 300, width: 50, height: 50)
                let tempView = UIView(frame: rect)
                tempView.backgroundColor = UIColor.blueColor()
                self.view.addSubview(tempView)

                self.view.layoutIfNeeded()

             print("cardCenter 3 \(self.card.center)")
        })
}

} }

The animation works fine, until the moment the completion is executed.动画工作正常,直到执行完成。 The first view is shown at its initial place before animation.第一个视图显示在动画之前的初始位置。 In the console however the printed coordinates of the center of the view match the center of the main View然而,在控制台中,视图中心的打印坐标与主视图的中心相匹配

Console output控制台输出

viewCenter  (160.0, 284.0)  
cardCenter 1 (140.0, 92.0)  
cardCenter 2 (160.0, 284.0)  
cardCenter 3 (160.0, 284.0)

Can anyone explain this behavior when adding a UIView in the completion block?在完成块中添加 UIView 时,任何人都可以解释这种行为吗?

EDIT编辑

Based on comment of Chris I added some logging on constraints of card and根据 Chris 的评论,我添加了一些关于card
self.view.layoutIfNeeded() to the completion. self.view.layoutIfNeeded()到完成。 Now the console output does show the initial coordinates of the View现在控制台输出确实显示了视图的初始坐标

New Console Output新的控制台输出

viewCenter (160.0, 284.0)视图中心 (160.0, 284.0)
cardCenter 1 (140.0, 92.0)卡中心 1 (140.0, 92.0)
number of constraints on card: 0卡上的约束数:0
cardCenter 2 (160.0, 284.0)卡中心 2 (160.0, 284.0)
cardCenter 3 (140.0, 92.0)卡中心 3 (140.0, 92.0)

If you have constraints in Interface Builder, remove the constraints from card and try again - you'll find that it works as expected.如果您在 Interface Builder 中有约束,请从card删除约束card试 - 您会发现它按预期工作。

When animating a view with constraints on it, first update the constraint constant, then inside your animation block, call self.view.layoutIfNeeded() .当动画带有约束的视图时,首先更新约束常量,然后在动画块中调用self.view.layoutIfNeeded() Animating properties directly will produce unexpected results, as you have seen in your example.正如您在示例中所见,直接对属性进行动画处理会产生意想不到的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM