Derek
Derek

Reputation: 9943

iOS: Simple Physics for a UIView

I have a UIView that a user drags around via setting its center in the touchesMoved: method. When the user lets go, I want the UIView to fall off the screen according to how fast and what direction they were moving it in.

Do I need to somehow create a vector by comparing the UIView's last center point to it's new center point? And subtract a fixed amount to the vector's y value every so often with a NSTimer?

Upvotes: 1

Views: 499

Answers (1)

Legolas
Legolas

Reputation: 12335

Yes, you will need to a decent amount of physics in calculating

  1. Speed of swipe
  2. Direction of swipe

You will need to use the touchesMoved method along with a timer to track the amount of time for the swipe and also the co-ordinates for the new location of the object. This should be fairly straight forward. Once you are done finding those you can simply add a UIAnimation for the object to move to its new place.

~ A suggestion:

I would suggest that you have a look at Cocos2D and integrate the library with you app. You will not need to implement touch delegate methods and compute things yourself ~ there are libraries for that :) It has a lot of libraries especially for moving objects (or sprites if you wish to call them that way) and you have animation method like easeInEaseOut, etc.. that can be impacted on the moving object. If you are developing a game of some sort, have a look at chipMunk engine in Cocos2D as well.

Upvotes: 1

Related Questions