Imagine that i drag an ImageView in TouchesMoved,how can i recognize that in which direction i drag开发者_如何学Pythonged it from code ?
You can store a temporary Point from touchesBegan
. Add a iVar in "YourImageView" interface.
@interface YourImageView : UIImageView
{
CGPoint previousPt;
//Other iVars.
}
@end
@implementation YourImageView
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
previousPt = [[touches anyObject] locationInView:self.view];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
const CGPoint p = [[touches anyObject] locationInView:self.view];
if (previousPt.x > p.x)
{
//Dragged right to left
}
else if (previousPt.x < p.x)
{
//Dragged left to right
}
else
{
//no move
}
//Do the same thing for y-direction
}
I would extract a method out of it. But I hope you got the idea.
If you subtract the old position from the new position of the ImageView, then you will get a direction vector. If you want a unit vector, just normalize it.
精彩评论