如何在Xcode版本中使用UISwipeGestureRecognizer
限制手势识别的范围。 5?
这是一些代码:
- (void)viewDidLoad {
[super viewDidLoad];
// ...
[UIApplication sharedApplication].idleTimerDisabled = NO;
[UIApplication sharedApplication].idleTimerDisabled = YES;
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeUp];
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeDown];
// ...
}
// ...
- (void)didSwipe:(UISwipeGestureRecognizer*)swipe{
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"Swipe Left");
} else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"Swipe Right");
} else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"Swipe Up");
} else if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"Swipe Down");
}
}
我只需要识别标签中的手势即可。
谢谢,伙计们。
最佳答案
将addGestureRecognizer
添加到所需的UILabel
而不是self.view
并将UILabel
userInteractionEnabled
分配为YES
。而已。
self.myLabel.userInteractionEnabled = YES;
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.myLabel addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.myLabel addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[self.myLabel addGestureRecognizer:swipeUp];
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[self.myLabel addGestureRecognizer:swipeDown];
关于ios - 建立手势识别领域,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21317821/