简体   繁体   English

UIImageView与持续时间以及动画图像一起动画

[英]UIImageView animate with duration, together with animation images

I just started iOS development and am learning to Swift and Xcode. 我刚刚开始iOS开发,正在学习Swift和Xcode。 I have an UIImageView object in a view controller and would like to move this UIImageView across the screen while it is toggling between two images. 我在视图控制器中有一个UIImageView对象,并希望在两个图像之间切换时在屏幕上移动此UIImageView。 Here's the code: 这是代码:

@IBOutlet weak var anImageView: UIImageView!

@IBAction func beginAnimation(sender: AnyObject) {

    let imgListArray:[AnyObject] = [UIImage(named: "img1.png")!, UIImage(named: "img2.png")!]
    anImageView.animationImages = imgListArray
    anImageView.animationDuration = 0.5
    anImageView.image = UIImage(named: "img1.png")
    anImageView.startAnimating()

    UIView.animateWithDuration(4.0, animations: {
        self.anImageView.center = CGPoint(x: 600, y: 600)
        }, completion: {finished in
            self.anImageView.center = CGPoint(x:80,y:80)})
}

The problem is with the co-ordinates. 问题出在坐标上。 The UIImageView starts off-screen and does not end at (80,80). UIImageView从屏幕外开始,但不以(80,80)结尾。 The toggling is fine. 切换很好。

I guess I have to transform the co-ordinates to the superview(?) space, but my attempts to do this have not been successful. 我想我必须将坐标转换为superview(?)空间,但是我做的尝试没有成功。 For example, I tried the convertPoint function, but was not successful. 例如,我尝试了convertPoint函数,但未成功。

Any guidance is highly appreciated! 任何指导是高度赞赏!

Thanks. 谢谢。

You have not specified clearly what you want to do, so let's concentrate on what this code does do: 您尚未明确指定执行的操作,因此让我们集中精力处理此代码的作用

UIView.animateWithDuration(4.0, animations: {
    self.anImageView.center = CGPoint(x: 600, y: 600)
}, completion: {finished in
    self.anImageView.center = CGPoint(x:80,y:80)
})

That code says: 该代码说:

  1. No matter where it is now, start the image view moving from that point towards (600,600) (which, unless you are on an iPad, will be offscreen). 无论现在在哪里,都应从该点开始向(600,600)移动图像视图(除非您在iPad上,否则它将处于屏幕外)。

  2. When it gets there, make it jump from (600,600) to (80,80). 当它到达那里时,使其从(600,600) 跳到 (80,80)。

That's what you are saying. 那就是你的意思。 So if that isn't what you want, don't say that! 因此,如果那不是您想要的,请不要这么说!

Unchecking auto layout option solved the problem for now. 取消选中“自动布局”选项即可解决此问题。 But the right approach is to leave auto layout on and animate constraints. 但是正确的方法是保留自动布局并设置约束动画。 Will post a solution once I get it working. 一旦我开始运作,便会发布解决方案。

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

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