简体   繁体   中英

IOS: UISwipeGestureRecognizer

I have this code:

UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
[recognizer setNumberOfTouchesRequired:1];
[n16 addGestureRecognizer:recognizer];
[n17 addGestureRecognizer:recognizer];

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer{
NSLog(@"SWIPE");
}

How can I know what view happens gesture? views are n16 and n17

I am not sure if you can register the same UIGestureRecognizer instance to different views, but if you could, I think UIGestureRecognizer.view property is what you are looking for.

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIGestureRecognizer_Class/Reference/Reference.html#//apple_ref/occ/cl/UIGestureRecognizer

So, you should be able to do something like this. (again, I am not sure if you can attach different UIGestureRecognizer instance to different views...)

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer{
    if(gestureRecognizer.view == n16)
    {
        // specific operation to n16
    } 
    else if(gestureRecognizer.view == n17)
    {
        // specific operation to n17
    }
}

Like this:

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer {
    NSLog(@"SWIPE");
    UIView *vw = [gestureRecognizer view]; // this is the view that generated the
        // gesture - either n16 or n17
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM