为UISwipeGestureRecognizer设置方向
我想添加简单的滑动手势识别到我的视图为基础的iPhone项目。 应该识别所有方向(右,下,左,上)的手势。
它在UISwipeGestureRecognizer的文档中有说明:
您可以通过使用按位或操作数指定多个UISwipeGestureRecognizerDirection常量来指定多个方向。 默认的方向是UISwipeGestureRecognizerDirectionRight。
但对我来说,这是行不通的。 当所有四个方向都“或”时,只能识别左右滑动。
- (void)viewDidLoad { UISwipeGestureRecognizer *recognizer; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; [super viewDidLoad]; } -(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { NSLog(@"Swipe received."); }
我解决了这个问题,在视图中增加了四个识别器,但是我很好奇,为什么它不能像文档中的广告一样工作?
- (void)viewDidLoad { UISwipeGestureRecognizer *recognizer; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; [super viewDidLoad]; } -(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { NSLog(@"Swipe received."); }
似乎有一个错误。 你可以像你一样指定允许的方向。 但是当你尝试访问在动作select器方法中触发滑动的实际方向时,你仍然可以获得原来设置的位掩码(对于允许的方向)。
这意味着当允许超过1个方向时,检查实际的方向将总是失败。 当你在select器方法中输出'direction'的值时(比如-(void)scrollViewSwiped:(UISwipeGestureRecognizer *)recognizer
),你可以很容易地看到它。
向苹果提交了一个错误报告(#8276386)。
[更新]我从苹果公司得到了一个答案,说这种行为是按照预期进行的。
因此,例如在表格视图中,您可以在表格视图单元格中向左或向右滑动以触发“删除”(这会将滑动手势的方向设置为左侧和右侧)
这意味着原来的解决方法是它应该被使用的方式。 方向属性只能用于获取正确识别的手势,而不能在成功识别的方法上进行比较以确定触发识别的实际方向。
我注意到左/右和上/下手势成对协作,所以你只需要指定两个手势识别器。 而文档似乎是错的。
那么糟透了,我解决了这个问题,增加了两个手势,如拉尔斯提到,并完美地工作…
1)左/右2)上/下
UISwipeGestureRecognizer *swipeLeftRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; [swipeLeftRight setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft )]; [self.view addGestureRecognizer:swipeLeftRight]; UISwipeGestureRecognizer *swipeUpDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; [swipeUpDown setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown )]; [self.view addGestureRecognizer:swipeUpDown];
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; [self.view addGestureRecognizer:recognizer]; [recognizer release]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; [self.view addGestureRecognizer:recognizer]; [recognizer release];
现在这是didSwipe函数
- (void) didSwipe:(UISwipeGestureRecognizer *)recognizer{ if([recognizer direction] == UISwipeGestureRecognizerDirectionLeft){ //Swipe from right to left //Do your functions here }else{ //Swipe from left to right //Do your functions here } }
如果您使用Xcode 4.2,则可以在故事板中添加手势识别器,然后将GUI手势识别器链接到IBActions。
您可以在实用程序窗格的对象库(右窗格的底部)中find手势识别器。
然后它只是一个Control的问题 – 拖动到适当的行动。
如果你想要检测所有的四个方向,你需要创build四个实例,就像你在工作中一样。
原因如下:您创build的UISwipeGestureRecognizer的同一实例是作为发件人传递给select器的实例。 所以如果你把它设置为识别所有的四个方向,它将返回sgr.direction == xxx
真,其中xxx是四个方向中的任何一个。
这是一个替代的解决方法 ,涉及较less的代码(假定使用ARC):
for(int d = UISwipeGestureRecognizerDirectionRight; d <= UISwipeGestureRecognizerDirectionDown; d = d*2) { UISwipeGestureRecognizer *sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; sgr.direction = d; [self.view addGestureRecognizer:sgr]; }
以下是UISwipeGestureRecognizer用法的代码示例。 注释注释。
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //add gesture recognizer. The 'direction' property of UISwipeGestureRecognizer only sets the allowable directions. It does not return to the user the direction that was actaully swiped. Must set up separate gesture recognizers to handle the specific directions for which I want an outcome. UISwipeGestureRecognizer *gestureRight; UISwipeGestureRecognizer *gestureLeft; gestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];//direction is set by default. gestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];//need to set direction. [gestureLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)]; //[gesture setNumberOfTouchesRequired:1];//default is 1 [[self view] addGestureRecognizer:gestureRight];//this gets things rolling. [[self view] addGestureRecognizer:gestureLeft];//this gets things rolling. }
swipeRight和swipeLeft是您用于执行基于左侧或右侧滑动的特定活动的方法。 例如:
- (void)swipeRight:(UISwipeGestureRecognizer *)gesture { NSLog(@"Right Swipe received.");//Lets you know this method was called by gesture recognizer. NSLog(@"Direction is: %i", gesture.direction);//Lets you know the numeric value of the gesture direction for confirmation (1=right). //only interested in gesture if gesture state == changed or ended (From Paul Hegarty @ standford U if ((gesture.state == UIGestureRecognizerStateChanged) || (gesture.state == UIGestureRecognizerStateEnded)) { //do something for a right swipe gesture. } }
UISwipeGestureRecognizer *Updown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleGestureNext:)]; Updown.delegate=self; [Updown setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp]; [overLayView addGestureRecognizer:Updown]; UISwipeGestureRecognizer *LeftRight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleGestureNext:)]; LeftRight.delegate=self; [LeftRight setDirection:UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight]; [overLayView addGestureRecognizer:LeftRight]; overLayView.userInteractionEnabled=NO; -(void)handleGestureNext:(UISwipeGestureRecognizer *)recognizer { NSLog(@"Swipe Recevied"); //Left //Right //Top //Bottom }
嗯,奇怪,这对我来说是完美的,我做同样的事情
认为你应该试试看
UIGestureRecognizerDelegate方法
- (BOOL)gestureRecognizerShouldBegin:(UISwipeGestureRecognizer *)gestureRecognizer { // also try to look what's wrong with gesture NSLog(@"should began gesture %@", gestureRecognizer); return YES; }
在日志中你必须看到像这样的东西:
应该开始做手势; target = <(action = actionForUpDownSwipeGestureRecognizer :, target =)>; 方向=上,下,左,右>
使用这个,应该是位操作
gesture.direction & UISwipeGestureRecognizerDirectionUp || gesture.direction & UISwipeGestureRecognizerDirectionDown
Swift 2.1
我不得不使用以下
for var x in [ UISwipeGestureRecognizerDirection.Left, UISwipeGestureRecognizerDirection.Right, UISwipeGestureRecognizerDirection.Up, UISwipeGestureRecognizerDirection.Down ] { let r = UISwipeGestureRecognizer(target: self, action: "swipe:") r.direction = x self.view.addGestureRecognizer(r) }
这让我疯狂。 我终于想出了一个可靠的方法来拥有多个swipeGestureRecognizers。
如果您的“动作”select器的名称在多个swipeGestureRecognizers中是相同的,则iOS中会出现一个错误。 如果你只是不同的命名,例如handleLeftSwipeFrom和handleRightSwipeFrom,一切正常。
UISwipeGestureRecognizer *recognizer; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release];