2个同时具有不同方向的各种UIPanGestureRecognizers?
2 various UIPanGestureRecognizers with different directions simultaneously?
可以上下左右平移
识别器 1 仅处理垂直平移,识别器 2 仅处理水平平移。它们都与同一个委托对象链接。
识别器 2 完全独立于第一个识别器(除了只能从所描述的 4 个中选择一个方向)。
那么如何防止其中一个与另一个同时执行呢?
-(void)initPanGestures{
UIPanGestureRecognizer *panGestureRecognizer1 = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePan1Gesture:)];
UIPanGestureRecognizer *panGestureRecognizer2 = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePan2Gesture:)]
}
-(void)handlePan1Gesture:(UIPanGestureRecognizer *)sender{
CGPoint velocity = [sender velocityInView:sender.view];
switch(sender.state) {
case UIGestureRecognizerStateBegan:
if(fabs(velocity.y) > fabs(velocity.x)){
panGestureRecognizer2.enabled=YES;
panGestureRecognizer1.enabled=NO;
}
break;
case UIGestureRecognizerStateCancelled:
panGestureRecognizer1.enabled=YES;
break;
case UIGestureRecognizerStateEnded:{
panGestureRecognizer1.enabled=YES;
}
break;
}
if(velocity.x > 0)
{
NSLog(@"gesture went right");
}
else if(velocity.x < 0)
{
NSLog(@"gesture went left");
}
}
-(void)handlePan2Gesture:(UIPanGestureRecognizer *)sender{
CGPoint velocity = [sender velocityInView:sender.view];
switch(sender.state) {
case UIGestureRecognizerStateBegan:
if(fabs( velocity.x) > fabs(velocity.y)){
panGestureRecognizer2.enabled=NO;
panGestureRecognizer1.enabled=YES;
}
break;
case UIGestureRecognizerStateCancelled:
panGestureRecognizer2.enabled=YES;
break;
case UIGestureRecognizerStateEnded:
panGestureRecognizer2.enabled=YES;
break;
}
if(velocity.y > 0)
{
NSLog(@"gesture went down");
}
else if(velocity.y < 0)
{
NSLog(@"gesture went up");
}
}
此外,对于两个不同的手势,您必须实现委托方法并识别 gestures.Fro 示例,我同时使用了两个手势长和平移手势
#pragma mark - UIGestureRecognizerDelegate methods
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if ([panGestureRecognizer1 isEqual:gestureRecognizer]) {
return [panGestureRecognizer2 isEqual:otherGestureRecognizer];
}
if ([panGestureRecognizer2 isEqual:gestureRecognizer]) {
return [panGestureRecognizer1 isEqual:otherGestureRecognizer];
}
return NO;
}
可以上下左右平移
识别器 1 仅处理垂直平移,识别器 2 仅处理水平平移。它们都与同一个委托对象链接。
识别器 2 完全独立于第一个识别器(除了只能从所描述的 4 个中选择一个方向)。
那么如何防止其中一个与另一个同时执行呢?
-(void)initPanGestures{
UIPanGestureRecognizer *panGestureRecognizer1 = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePan1Gesture:)];
UIPanGestureRecognizer *panGestureRecognizer2 = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePan2Gesture:)]
}
-(void)handlePan1Gesture:(UIPanGestureRecognizer *)sender{
CGPoint velocity = [sender velocityInView:sender.view];
switch(sender.state) {
case UIGestureRecognizerStateBegan:
if(fabs(velocity.y) > fabs(velocity.x)){
panGestureRecognizer2.enabled=YES;
panGestureRecognizer1.enabled=NO;
}
break;
case UIGestureRecognizerStateCancelled:
panGestureRecognizer1.enabled=YES;
break;
case UIGestureRecognizerStateEnded:{
panGestureRecognizer1.enabled=YES;
}
break;
}
if(velocity.x > 0)
{
NSLog(@"gesture went right");
}
else if(velocity.x < 0)
{
NSLog(@"gesture went left");
}
}
-(void)handlePan2Gesture:(UIPanGestureRecognizer *)sender{
CGPoint velocity = [sender velocityInView:sender.view];
switch(sender.state) {
case UIGestureRecognizerStateBegan:
if(fabs( velocity.x) > fabs(velocity.y)){
panGestureRecognizer2.enabled=NO;
panGestureRecognizer1.enabled=YES;
}
break;
case UIGestureRecognizerStateCancelled:
panGestureRecognizer2.enabled=YES;
break;
case UIGestureRecognizerStateEnded:
panGestureRecognizer2.enabled=YES;
break;
}
if(velocity.y > 0)
{
NSLog(@"gesture went down");
}
else if(velocity.y < 0)
{
NSLog(@"gesture went up");
}
}
此外,对于两个不同的手势,您必须实现委托方法并识别 gestures.Fro 示例,我同时使用了两个手势长和平移手势
#pragma mark - UIGestureRecognizerDelegate methods
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if ([panGestureRecognizer1 isEqual:gestureRecognizer]) {
return [panGestureRecognizer2 isEqual:otherGestureRecognizer];
}
if ([panGestureRecognizer2 isEqual:gestureRecognizer]) {
return [panGestureRecognizer1 isEqual:otherGestureRecognizer];
}
return NO;
}