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:同じビューで 2 つ以上のジェスチャ レコグナイザーを認識できるようにするために使用できます。サンプル コードはこちらを参照してください。

編集

あなたはこれを参照することができます

参考になる方法は- (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 に答える