将Gesture应用于self.view时,如何使表视图起作用? [英] How can we able to make table view work when Gesture is applied to self.view?

查看:41
本文介绍了将Gesture应用于self.view时,如何使表视图起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在整个视图上应用了手势,并且想在self.view中与表格视图进行交互.我已经应用了自定义手势,如下所示:

  #import"TouchEvent.h"#import< UIKit/UIGestureRecognizerSubclass.h>@implementation TouchEvent@synthesize xInc = _inc;@synthesize prev = _prev;@synthesize diff = _diff;-(void)touchesBegan:(NSSet *)touch withEvent:(UIEvent *)event{[self setState:UIGestureRecognizerStateBegan];}-(void)touchesCancelled:(NSSet *)touch withEvent:(UIEvent *)event{[self setState:UIGestureRecognizerStateCancelled];}-(void)touchesMoved:(NSSet *)用事件触动:(UIEvent *)事件{UITouch * touch = [触摸anyObject];//轻拍可以稍微移动,但我们不感兴趣//轻按一下.我们想要更多的运动.因此,如果检测到轻拍//使识别器失败.如果([自我状态] == UIGestureRecognizerStatePossible){[self setState:UIGestureRecognizerStateBegan];} 别的 {[self setState:UIGestureRecognizerStateChanged];}UIView * view = [自我视图];CGPoint接触点= [触摸locationInView:view];CGPoint prevPoint = [触摸previousLocationInView:view];如果(prevPoint.x< touchpoint.x)[self setDiff:(touchpoint.x-prevPoint.x)];别的[self setDiff:(prevPoint.x-touchpoint.x)];NSLog(@差异为:%f",self.diff);NSLog(@"x is:%f",touchpoint.x);[self setXInc:touchpoint.x];[self setPrev:prevPoint.x];}-(void)touchesEnded:(NSSet *)touch withEvent:(UIEvent *)event{[self setState:UIGestureRecognizerStateEnded];}@结尾 

我正在这样申请

 -(void)viewDidLoad{NSArray * ary = [NSArray arrayWithObjects:@朋友",@消息",@聊天",@新订阅源",@照片",@注释",nil];array = ary;gestur = [[TouchEvent alloc] initWithTarget:self动作:@selector(SLideTheView :)];[self.view addGestureRecognizer:gestur];[super viewDidLoad];//加载视图后进行其他任何设置,通常是从笔尖进行.}-(无效)moveIt:(CGFloat)x_pos{[UIView beginAnimations:nil context:NULL];[UIView setAnimationDuration:0.5];[UIView setAnimationDelegate:self];[viewU setFrame:CGRectMake(x_pos,0,320,460)];[UIView commitAnimations];}-(void)SLideTheView:(TouchEvent *)手势{CGPoint pt = [手势位置InView:viewU];如果(pt.x> 13& pt.y> 5& pt.x< 29& pt.y< 23){[self slideView];}别的{如果(gesture.state == UIGestureRecognizerStateEnded){如果(viewU.frame.origin.x!= 0){如果(gesture.prev< gesture.xInc){[self moveIt:270];}别的[self moveIt:0];}}别的{如果(viewU.frame.origin.x!= 0){如果(gesture.prev< gesture.xInc)x = viewU.frame.origin.x + gesture.diff;别的x = viewU.frame.origin.x-gesture.diff;[viewU setFrame:CGRectMake(x,0,320,460)];}}}}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{label.text = [array objectAtIndex:[indexPath row]];[self slideView];} 

正在运行的图像如下:

解决方案

您可以使用 requireGestureRecognizerToFail: 能够在同一视图上识别两个或多个手势识别器,请参见示例代码此处

修改

,因此您可以参考此 一个

要引用的方法是 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

请参见此问题以获取更多信息

I have applied gesture on whole view and I want to interact with table view within self.view.I have applied custom gesture.that is as follows:

    #import "TouchEvent.h"
#import <UIKit/UIGestureRecognizerSubclass.h>

@implementation TouchEvent
@synthesize xInc=_inc;
@synthesize prev=_prev;
@synthesize diff=_diff;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 [self setState:UIGestureRecognizerStateBegan];
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
 [self setState:UIGestureRecognizerStateCancelled];
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
 UITouch *touch = [touches anyObject];

 // A tap can have slight movement, but we're not interested
 // in a tap. We want more movement. So if a tap is detected
 // fail the recognizer. 

 if ([self state] == UIGestureRecognizerStatePossible) {
  [self setState:UIGestureRecognizerStateBegan];
 } else {
  [self setState:UIGestureRecognizerStateChanged];
 }

 UIView *view = [self view];
 CGPoint touchpoint = [touch locationInView:view];
 CGPoint prevPoint=[touch previousLocationInView:view];


 if (prevPoint.x<touchpoint.x) 
  [self setDiff:(touchpoint.x-prevPoint.x)];
 else
  [self setDiff:(prevPoint.x-touchpoint.x)];

 NSLog(@"difference is: %f",self.diff);
 NSLog(@"x is: %f",touchpoint.x);
 [self setXInc:touchpoint.x];
 [self setPrev:prevPoint.x];

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
 [self setState:UIGestureRecognizerStateEnded];

}
@end

and I m applying like this

- (void)viewDidLoad
{
 NSArray *ary=[NSArray arrayWithObjects:@"Friends",@"Message",@"Chats",@"New Feeds",@"Photos",@"Notes", nil];
 array=ary;

 gestur=[[TouchEvent alloc] initWithTarget:self action:@selector(SLideTheView:)];

 [self.view addGestureRecognizer:gestur];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}



-(void)moveIt:(CGFloat)x_pos
{
 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration:0.5];
 [UIView setAnimationDelegate:self];

 [viewU setFrame:CGRectMake(x_pos, 0, 320, 460)];

 [UIView commitAnimations];

}

-(void)SLideTheView:(TouchEvent*)gesture
{

 CGPoint pt=[gesture locationInView:viewU];

      if (pt.x>13&&pt.y>5&&pt.x<29&&pt.y<23) 
      {
       [self slideView];
      }
      else
      {
         if (gesture.state==UIGestureRecognizerStateEnded) 
         {
          if (viewU.frame.origin.x!=0) {
           if (gesture.prev<gesture.xInc) 
           {
            [self moveIt:270];
           }
           else
            [self moveIt:0];
          }
         }
         else
         {
          if (viewU.frame.origin.x!=0) 
          {
           if (gesture.prev<gesture.xInc) 
            x=viewU.frame.origin.x+gesture.diff; 
           else
            x=viewU.frame.origin.x-gesture.diff;

           [viewU setFrame:CGRectMake(x, 0, 320, 460)];
          }
         }

      }

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 label.text=[array objectAtIndex:[indexPath row]];
 [self slideView];
}

the running image is as follows:

解决方案

you can use requireGestureRecognizerToFail: to able to recognize two or more gesture recognizers on same view see example code here

edit

so you can refer this one

the method to be referenced is - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

refer this question for more

这篇关于将Gesture应用于self.view时,如何使表视图起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆