如何在Swift中移动图像?

adm*_*man 5 xcode ios swift

每次按下按钮时,我希望能够将对象(在我的情况下,图像"小狗")向上移动1个像素.我偶然发现了旧的Objective-C解决方案以及类似的Swift代码,但没有一个适合我的具体问题.我很想知道移动图像的简单方法.这是我的代码到目前为止我可以收集的内容(我希望它不必要地长,可以减少到一两行):

@IBAction func tapButton() {
UIView.animateWithDuration(0.75, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
            self.puppy.alpha = 1
            self.puppy.center.y = 0
            }, completion: nil)

        var toPoint: CGPoint = CGPointMake(0.0, 1.0)
        var fromPoint : CGPoint = CGPointZero

        var movement = CABasicAnimation(keyPath: "movement")
        movement.additive = true
        movement.fromValue =  NSValue(CGPoint: fromPoint)
        movement.toValue =  NSValue(CGPoint: toPoint)
        movement.duration = 0.3

        view.layer.addAnimation(movement, forKey: "move")
}
Run Code Online (Sandbox Code Playgroud)

Dha*_*esh 5

这样你就可以改变你的位置 imageView

@IBAction func tapButton() {
    UIView.animateWithDuration(0.75, delay: 0, options: .CurveLinear, animations: {
        // this will change Y position of your imageView center
        // by 1 every time you press button
        self.puppy.center.y -= 1  
    }, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

并从您的按钮操作中删除所有其他代码.