3

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

    #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

我正在申请这样

- (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];
}

运行图像如下:

在此处输入图像描述

4

2 回答 2

3

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

编辑

所以你可以参考这个

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

参考这个问题了解更多

于 2012-04-30T09:17:00.570 回答
1
//add gesture to mapview-for single touch
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SLideTheView:)];
[self.view addGestureRecognizer:tap];
tap.numberOfTapsRequired = 1;


//add another gesture to terminate first one
UITapGestureRecognizer *secondGest= [[UITapGestureRecognizer alloc] init];
[tableview addGestureRecognizer:secondGest]; 
[tap requireGestureRecognizerToFail:secondGest];
[secondGest release];
[tap release];
于 2012-04-30T09:31:01.280 回答