2014-01-23 43 views
0

如何在Xcode ver中使用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"); 
    } 
} 

我需要認識到只有在標籤手勢。

Thx很多,夥計們。

回答

2

添加addGestureRecognizerself.view所需UILabel代替並分配UILabeluserInteractionEnabledYES。而已。

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]; 
+0

請注意,您可能必須在標籤上設置userInteractionEnabled = YES以便手勢識別器正常工作。因爲標籤通常不會響應觸摸事件。 –

+0

哦,我看到..我會更新我的答案。謝謝。 –