2012-04-30 20 views
3

我已在整个视图施加手势和我想与内self.view.I表视图交互应用了自定义gesture.that如下:当手势应用于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]; 
} 

运行图像如下:

enter image description here

回答

3

可以使用requireGestureRecognizerToFail:到能够识别在同一视图中的两个或多个手势识别器看到的示例代码here

编辑

,所以你可以参考这个one

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

参考this question更多

+0

感谢您的答复,但我不想承认在一个视图中的多个手势。我只是想在主视图上应用手势,而我的表视图是子视图应该工作。 – Warewolf

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]; 
+0

感谢您关注我的问题,但这不符合我的要求。请运行我的代码,您将更好地了解我的问题是。我希望我的应用程序的工作原样,但tableview也应该与它一起工作。 – Warewolf

+0

你可以给我你的电子邮件ID,我将发送应用程序。然后你将能够跑步。 – Warewolf

相关问题