每次按下按钮时,我希望能够将对象(在我的情况下,图像“小狗”)向上移动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") }