Reputation: 53
I need to make a UIImageView animate up when the screen is tapped then back down when it hits the top.
This is the code I have got at the moment:
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
if (CGRectIntersectsRect(self.playerImagePhone.frame, self.floorImagePhone.frame)) {
UIView.animateWithDuration(0.4) {
self.playerImagePhone.center = CGPointMake(self.playerImagePhone.center.x, self.playerImagePhone.center.y - 50)
println("player jumped")
}
}
}
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
UIView.animateWithDuration(0.4) {
self.playerImagePhone.center = CGPointMake(self.playerImagePhone.center.x, self.playerImagePhone.center.y + 50)
println("player jumped")
}
}
This code is working to make it go up then back down but the problem is that it allows the user to hold their finger on the screen and the image will stay up. How can I make it go down as soon as the image is finished going up?
Thanks
Upvotes: 0
Views: 366
Reputation: 498
touchesBegan can be called often... it probably isn't a good idea to perform your animations in there. You'll probably want to update a seperate object maintaining the animation, then once the animation is done (once the duration is over) you run another animation to bring the image back down.
As an alternative you can use also use animateWithDuration:animations:completion: and calling the down animation within the completion block.
Upvotes: 1