2017-05-08 95 views
2

只是一个快速的问题,我正在为我当前的应用程序设置设置动画背景图像。本质上,我想让我目前仍然背景图像从左到右滚动,在我的视图中不断地滚动;但我一直在拼命拼凑。从各种各样的其他来源,我已经包含这个功能,目前没有工作,我真的不知道如何或为什么。Swift - 动画UIImageView图像

func animate(_ image: UIImageView) { 
     UIView.animate(withDuration: 5, delay: 0, options: .curveLinear, animations: { 
      image.transform = CGAffineTransform(translationX: -100, y: 0) 
     }) { (success: Bool) in 
      image.transform = CGAffineTransform.identity 
      self.animate(image) 
     } 
    } 

任何帮助/援助非常感谢!

+0

你需要什么样的动画?清楚解释 –

+0

对不起,缺乏清晰度!我希望我的动画能够成为这件事的一部分; https://cdn.dribbble.com/users/279765/screenshots/1335470/marioscroll_real_real.gif – Jordy

回答

1

你并不需要使用CAffineTranform里面animateWithDuration,你可以简单的定义,如新的中心:

let oldCenter = image.center 
let newCenter = CGPoint(x: oldCenter.x - 100, y: oldCenter.y) 

UIView.animate(withDuration: 1, delay: 0, options: .curveLinear, animations: { 
    image.center = newCenter 
}) { (success: Bool) in 
    print("Done moving image") 
    } 

希望它能帮助。

+0

试图实施你的建议后,我建议调整代码如下:let oldCenter = background.center let newCenter = CGRect (origin:oldCenter.x - 100,size:oldCenter.y) UIView.animate(withDuration:5,delay:0,options:.curveLinear,animations:{ background.center = newCenter }){(success: Bool) print(“完成移动图像”) } 但是然后我给二进制运算符错误为“ - ”不能应用于输入“Int”或“CGFloat” – Jordy

+0

对不起,我的坏它的CGPoint不CGREct。现在试用CGPoint –

+0

动画中的确定进展。它慢慢地开始向左滑动,然后在图像中停止大约1/6。最重要的是,它似乎没有从图像的右侧重新出现;所以我要实现这个功能? – Jordy