开发者

Recognize that in which direction a control dragged

开发者 https://www.devze.com 2023-02-11 12:31 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消