繁体   English   中英

Swift3中的UIView动画

[英]UIView Animation in Swift3

我正在尝试将UIImageView移至顶部,底部,左侧和右侧。 动画应为4 4倍。 中心图像朝上移动,然后向左移动,再向右移动。 如何使用UIAnimation swift 3实现此目的?

UIView.animate(withDuration: 0.1, 
               delay:0, 
               animations: {}, 
               completion: {completion in})

如何使用动画将图像移到顶部,左侧,右侧和底部?

为此,您应该使用关键帧动画,因为它允许您指定动画的顺序。 以下是一个基本示例:

let animationDuration = 2.0
let position: CGFloat = 50.0
let viewToAnimate = UIImageView()

UIView.animateKeyframes(withDuration: animationDuration, delay: 0.0, options: .calculationModeLinear, animations: {
    UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1/3, animations: {
        viewToAnimate.frame.origin.y = viewToAnimate.frame.origin.y + position
    })
    UIView.addKeyframe(withRelativeStartTime: 1/4, relativeDuration: 1/4, animations: {
        viewToAnimate.frame.origin.y = viewToAnimate.frame.origin.y - (position * 2)
    })
    UIView.addKeyframe(withRelativeStartTime: 2/4, relativeDuration: 1/4, animations: {
        viewToAnimate.frame.origin.x = viewToAnimate.frame.origin.x - position
    })
    UIView.addKeyframe(withRelativeStartTime: 3/4, relativeDuration: 1/4, animations: {
        viewToAnimate.frame.origin.x = viewToAnimate.frame.origin.x + (position * 2)
    })
}, completion: { completed in

})
UIView.animate(withDuration: 1, delay:0, animations: {
        //top
        self.imageView.frame.origin.y = self.imageView.frame.origin.y - 100
    }, completion: {completion in
        UIView.animate(withDuration: 1, delay:0, animations: {
            //bottom
            self.imageView.frame.origin.y = self.imageView.frame.origin.y + 100
        }, completion: {completion in
            UIView.animate(withDuration: 1, delay:0, animations: {
                //left
                self.imageView.frame.origin.x = self.imageView.frame.origin.x - 100

            }, completion: {completion in
                UIView.animate(withDuration: 1, delay:0, animations: {

                }, completion: {completion in
                    //right
                    self.imageView.frame.origin.x = self.imageView.frame.origin.x + 100
                })
            })
        })

    })
UIView.animate(withDuration: 1, delay:0, animations: {
        //top
        self.tempVW.frame.origin.y = self.tempVW.frame.origin.y - 100
    }, completion: {completion in
        UIView.animate(withDuration: 1, delay:0, animations: {
            //left
            self.tempVW.frame.origin.x = self.tempVW.frame.origin.x - 100

        }, completion: {completion in
            UIView.animate(withDuration: 1, delay:0, animations: {
                //bottom
                self.tempVW.frame.origin.y = self.tempVW.frame.origin.y + 100

            }, completion: {completion in
                UIView.animate(withDuration: 1, delay:0, animations: {                        //right
                     self.tempVW.frame.origin.x = self.tempVW.frame.origin.x + 100
                }, completion: {completion in


                })
            })
        })

    })

暂无
暂无

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

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