简体   繁体   中英

UIImageView animate with duration, together with animation images

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.

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).

  2. 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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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