开发者

UISwipeGestureRecognizer location

I need to trigger a different event depending on whether the user swipes the top of the screen, the middle of the screen, or the bottom of the screen. I'm trying to figure out the best/easiest way to do this, since I'm pretty sure that there is no way to get the location from UISwipeGestureRecognizer.

The first option is to create my own swipe recognizer using the 'touches' methods. This seems like开发者_Go百科 it would be very difficult (trying to differentiate between a swipe and a drag, for example).

The second possibility is to get the location from one of the 'touches' methods (touchesBegan for example), and to somehow associate that with the swipe. Maybe set a timer in touchesBegan and then if the swipe recognizer fires within half-a-second or so I'll know that swipe was connected to that touch.

The third possibility I can think of is to lay 3 transparent subviews on top of my view and add a different swipe recognizer to each view. This seems like the best way to me except that transparent views don't recognize touch/swipe events. So how can I work around this?

Any suggestions? Thanks.


You could probably you use the locationOfTouch method of UISwipeGestureRecognizer.

CGPoint pt = [recognizer locationOfTouch:0 inView:view];

I believe this will give you the original x,y coordinates of the touch that initiated the gesture.


I used your third technic in one of my project. Transparent views DO recognizes swipe gestures. Here is the code I used, maybe you forget someting.

-(id) initWithPageList:(id<PageListDisplayerDelegate>) iDelegate {
    self = [super init];
    if (self) {
        // Custom initialization
        self.delegate = iDelegate;
        UISwipeGestureRecognizer* swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(viewSwiped:)];
        swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
        [self.view addGestureRecognizer:swipeRecognizer];
        [swipeRecognizer release];
    }
    return self;

}

-(void) viewSwiped:(UISwipeGestureRecognizer*) sender {
    NSLog(@"View Swiped");
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜