Reputation: 21
I just started iOS development and am learning to Swift and 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. 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). 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. For example, I tried the convertPoint function, but was not successful.
Any guidance is highly appreciated!
Thanks.
Upvotes: 1
Views: 918
Reputation: 21
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.
Upvotes: 0
Reputation: 534903
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:
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).
When it gets there, make it jump from (600,600) to (80,80).
That's what you are saying. So if that isn't what you want, don't say that!
Upvotes: 1