ios UISwipeGestureRecognizer 的设置方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3319209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting direction for UISwipeGestureRecognizer
提问by sandisn
I want to add simple swipe gesture recognition to my view based iPhone project. Gestures in all directions (right, down, left, up) should be recognized.
我想向基于视图的 iPhone 项目添加简单的滑动手势识别。应识别所有方向(右、下、左、上)的手势。
It is stated in the docs for UISwipeGestureRecognizer:
它在 UISwipeGestureRecognizer 的文档中说明:
You may specify multiple directions by specifying multiple UISwipeGestureRecognizerDirection constants using bitwise-OR operands. The default direction is UISwipeGestureRecognizerDirectionRight.
您可以通过使用按位或操作数指定多个 UISwipeGestureRecognizerDirection 常量来指定多个方向。默认方向是 UISwipeGestureRecognizerDirectionRight。
However for me it doesn't work. When all four directions are OR'ed only left and right swipes are recognized.
但是对我来说它不起作用。当所有四个方向都进行 OR 运算后,只能识别左右滑动。
- (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.");
}
I fixed this with adding four recognizers to the view but I'm curious to know why didn't it work as advertised in docs?
我通过在视图中添加四个识别器来解决这个问题,但我很想知道为什么它不像文档中宣传的那样工作?
- (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.");
}
采纳答案by RupertP
Seems like there is a bug. You can specify the allowed direction(s) as you did. But when you try to access the actualdirection that triggered the swipe in the action selector method you still get the bit mask you originally set (for the allowed directions).
好像有bug。您可以像以前一样指定允许的方向。但是,当您尝试访问在操作选择器方法中触发滑动的实际方向时,您仍然会获得最初设置的位掩码(对于允许的方向)。
This means that checks for the actual direction will always fail when more than 1 direction is allowed.
You can see it for yourself quite easily when you output the value of 'direction' in the selector method (ie -(void)scrollViewSwiped:(UISwipeGestureRecognizer *)recognizer
).
这意味着当允许超过 1 个方向时,对实际方向的检查将始终失败。当您在选择器方法(即-(void)scrollViewSwiped:(UISwipeGestureRecognizer *)recognizer
)中输出 'direction' 的值时,您可以很容易地看到它。
Filed a bug report (#8276386) to Apple.
向 Apple 提交了错误报告 (#8276386)。
[Update] I got an answer from Apple saying that the behavior works as was intended.
[更新] 我从 Apple 那里得到了一个回答,说该行为按预期工作。
So for example in a table view you can swipe left or right in a table view cell to trigger 'delete' (this would have directions of the swipe gesture set to left and right)
因此,例如在表格视图中,您可以在表格视图单元格中向左或向右滑动以触发“删除”(这会将滑动手势的方向设置为向左和向右)
This means that the original workaround is the way it's supposed to be used. The direction property can only be used to get the gestures recognized correctly, but not in the method performed on a successful recognition to compare for the actual direction that triggered the recognition.
这意味着原始的解决方法是它应该使用的方式。方向属性只能用于获得正确识别的手势,而不能用于对成功识别执行的方法中来比较触发识别的实际方向。
回答by Lars
I noticed that left/right and up/down gestures work together in pairs, so you only need to specify two gesture recognizers. And the docs do seem to be wrong.
我注意到左/右和上/下手势成对工作,所以你只需要指定两个手势识别器。而且文档似乎确实是错误的。
回答by yunas
Well that sucks, I solved the problem by adding 2 gestures like Lars mentioned and that worked perfectly...
好吧,太糟糕了,我通过添加 Lars 提到的 2 个手势解决了这个问题,而且效果很好......
1) Left/Right 2) Up/Down
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];
回答by KarenAnne
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];
Now this is the didSwipe function
现在这是 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
}
}
回答by Grymm
If your using Xcode 4.2 you can add Gesture Recognizers @ the storyboard and then link the GUI Gesture Recognizers to IBActions.
如果您使用 Xcode 4.2,您可以在故事板中添加手势识别器,然后将 GUI 手势识别器链接到 IBActions。
You can find the Gesture Recognizers in the Object Library of the Utility Pane (The bottom of the Right pane).
您可以在实用程序窗格(右窗格底部)的对象库中找到手势识别器。
Then its just a matter of Control-dragging to the appropriate action.
然后它只是控制拖动到适当的动作的问题。
回答by tybro0103
If you want it to detect all four directions, you'll need to create four instances, as you did in your work-around.
如果您希望它检测所有四个方向,则需要创建四个实例,就像您在变通方法中所做的那样。
Here's Why:
The same instance of UISwipeGestureRecognizer that you create is the instance that gets passed to the selector as sender. So if you set it to recognize all four directions, it will return true for sgr.direction == xxx
where xxx is any one of the four directions.
原因如下:您创建的 UISwipeGestureRecognizer 的同一个实例是作为发送者传递给选择器的实例。因此,如果您将其设置为识别所有四个方向,则对于sgr.direction == xxx
其中 xxx 是四个方向中的任何一个方向,它将返回 true 。
Here's an alternative work-aroundthat involves less code (assumes ARC use):
这是一个涉及较少代码的替代解决方法(假设使用 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];
}
回答by Greg
Here is a code sample for UISwipeGestureRecognizer usage. Note comments.
这是 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 and swipeLeft are methods that you use to perform specific activities based on left or right swiping. For example:
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.
}
}
回答by bhavik
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
}
回答by Andrei
Swift 2.1
斯威夫特 2.1
I had to use the following
我不得不使用以下
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)
}
回答by William Wu
use this, it should be the bit operation
用这个,应该是位操作
gesture.direction & UISwipeGestureRecognizerDirectionUp ||
gesture.direction & UISwipeGestureRecognizerDirectionDown