[英]Limit Gesture Recognizer to Only One Specific UIView?
我在myViewController
上有一個名為myView
的UIView。 我有一個名為swipeLeft
(下面的代碼)的UIGestureRecognizer,用於檢測用戶何時向其滑動。
問題是: myViewController
在整個屏幕上識別相同的手勢並執行另一個操作。 所以我希望我的應用程序在myViewController
這個特定區域中swipeLeft
時執行myMethod
,該區域是myView
。
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(myMethod:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delaysTouchesBegan = YES;
[self.myView addGestureRecognizer:swipeLeft];
更多細節:我正在使用RESideMenu和myViewController
是正確的菜單,因此當它可見時, myViewController
的整個視圖會識別所有方向的滑動。 我想在這個特定的UIView myView
更改識別器。
謝謝!
首先,您需要將滑動手勢添加到視圖控制器頭文件中。
@property (strong, nonatomic) UISwipeGestureRecognizer *swipeLeft;
如果您查看DEMORootViewController.m,您將看到此調用:
self.rightMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"rightMenuViewController"];
這將調用你的awakeFromNib,這是你第一次做某事的機會。 在這里,您將創建滑動手勢。 但是,您無法將其添加到視圖中,因為此時您的插座尚未設置。 第一次設置它們是在viewDidLoad中,因此您可以在其中將手勢添加到視圖中。 所以將它添加到視圖控制器實現文件中
- (void)awakeFromNib
{
self.swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(myMethod:)];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.swipeView addGestureRecognizer:self.swipeLeft];
}
- (void)myMethod:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"Did swipe") ;
}
最后,每當我們的swipeLeft手勢發生時,您都需要告訴RESideMenu.m中的平移手勢失敗。 這樣做的方法是在第220行將以下代碼添加到RESideMenu.m。這是在viewDidLoad方法的末尾。
if ([self.rightMenuViewController isKindOfClass:[DEMOVC class]]) {
DEMOVC *rightVC = (DEMOVC *)self.rightMenuViewController;
if (rightVC.swipeLeft) {
[panGestureRecognizer requireGestureRecognizerToFail:rightVC.swipeGesture];
} } }
這假設您的自定義VC稱為DEMOVC。 您還需要將自定義VC導入RESideMenu.m,如下所示:
#import "DEMOVC.h"
如果這對您有用,請告訴我,如果還有別的,我可以幫助您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.