ios – 如何在Swift中移动图像?
发布时间:2020-12-14 17:57:27 所属栏目:百科 来源:网络整理
导读:每次按下按钮时,我希望能够将对象(在我的情况下,图像“小狗”)向上移动1个像素.我偶然发现了旧的Objective-C解决方案以及类似的 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") } 解决方法
这样您就可以更改imageView的位置
@IBAction func tapButton() { UIView.animateWithDuration(0.75,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) } 并从您的按钮操作中删除所有其他代码. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |