帧突然改变而不是动画[iOS]

Swa*_*wal 6 animation uiview uianimation ios swift

我的应用程序中有一个滚动视图的视图(百分比计算器).我试图动画视图飞出然后从左边再次飞入,虽然有不同的内容,按一下按钮.这是我的代码:

func next()
{
    UIView.animateWithDuration(0.5, animations: {
        self.mainView.frame = CGRectMake(-500, self.view.bounds.height/2-self.mainHeight/2, self.mainWidth, self.mainHeight)
        self.mainView.center.x -= 600
    }) { (success) in
        self.mainView.center.x += 1200    
    }

    UIView.animateWithDuration(0.5, animations: {
        self.mainView.frame = CGRectMake(self.view.bounds.width/2-self.mainWidth/2, self.view.bounds.height/2-self.mainHeight/2, self.mainWidth, self.mainHeight)
    }) { (success) in
        print("Animation Successful!")
        self.drawView()
    }
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么,但动画没有发生,视图完全消失,我"Animation Successful"在日志中看到.为了清楚起见,我以编程方式创建了视图.

这是viewDidLoad()中的代码; 调用时视图显示正确:

    mainHeight = self.view.bounds.height * 0.85
    mainWidth = self.view.bounds.width * 0.85
    let viewHeight = self.view.bounds.height
    let viewWidth = self.view.bounds.width

    mainView.frame = CGRectMake(viewWidth/2-mainWidth/2, viewHeight/2-mainHeight/2, mainWidth, mainHeight)
    mainView.layer.cornerRadius = 8
    mainView.layer.masksToBounds = true
    mainView.backgroundColor = getColor(0, green: 179, blue: 164)
    self.view.addSubview(mainView)

    scrollView.frame = CGRectMake(0, 0, mainWidth, mainHeight)
    scrollView.contentSize = CGSizeMake(mainWidth, 800)
    self.mainView.addSubview(scrollView)

    contentView.frame = CGRectMake(0, 0, mainWidth, 800);
    contentView.backgroundColor = getColor(0, green: 179, blue: 164)
    contentView.layer.masksToBounds = true
    contentView.clipsToBounds = true
    self.scrollView.addSubview(contentView)
Run Code Online (Sandbox Code Playgroud)

ped*_*uan 11

self.layoutViewIfNeeded()在框架/中心更改之前放置.此外,您可能需要将下一个动画放在下一个动画的完成处理程序中.

func next() {
    UIView.animateWithDuration(0.5, animations: {
        self.view.layoutIfNeeded() // add this
        self.mainView.frame = CGRectMake(-500, self.view.bounds.height/2-self.mainHeight/2, self.mainWidth, self.mainHeight)
        self.mainView.center.x -= 600

    }) { (success) in
        self.view.layoutIfNeeded() // add this
        self.mainView.center.x += 1200

        // your next animation within a completion handler of previous animation
        UIView.animateWithDuration(0.5, animations: {

            self.mainView.frame = CGRectMake(self.view.bounds.width/2-self.mainWidth/2, self.view.bounds.height/2-self.mainHeight/2, self.mainWidth, self.mainHeight)

        }) { (success) in

            print("Animation Successful!")
            self.drawView()
        }
    }
}   
Run Code Online (Sandbox Code Playgroud)

我还没有测试过,可能需要一些更改或行排序.


Off*_*Bad 8

在Swift 3中,我必须设置框架,然后调用layoutIfNeeded()

例如,带有spring的动画:

UIView.animate(withDuration: 0.3, delay: 0.1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
        self.subView.frame = CGRect(x: 0, y: 0, width: 500, height: 200)
        self.view.layoutIfNeeded()
    }) { (_) in
        // Follow up animations...
    }
Run Code Online (Sandbox Code Playgroud)