手势识别器委托:如何实现触摸委托功能

时间:2012-07-01 10:34:16

标签: iphone uiview delegates uigesturerecognizer

我实际上是尝试在实现touchBegan:withEvent的customScrollView类中插入触摸委托函数(类型touchEnded:withEventgestureRecognizers)。

当我尝试将识别器对象的委托设置为self时,我的SDK会显示一条警告消息,指出“不兼容的类型ID”。

我知道GestureRecognizer的委托协议不包含这样的功能,但我不知道为了在我的自定义视图中使用上述功能,我应该触发哪个委托。

非常感谢您的回复

维克多 - 马里

这是我的代码:

@interface TapScrollView : UIScrollView {

  //  id<TapScrollViewDelegate> delegate;
    NSMutableArray *classementBoutons;
    int n;
    int o;
//UIImageView *bouton;

}
//@property (nonatomic, assign) id<TapScrollViewDelegate> delegate;
//@property (nonatomic, retain) UIImageView *bouton;

//@property (strong, nonatomic) UIPanGestureRecognizer *bouton01pan;

-(id)init;
-(void)initierScrollView;

-(void) createGestureRecognizers;
-(IBAction)handlePanGesture:(UIPanGestureRecognizer*)sender;


@end



#import "TapScrollView.h"


@implementation TapScrollView

//@synthesize bouton;

- (void)setUpBoutonView {
    // Create the placard view -- its init method calculates its frame based on its image
    //boutonHome *aBoutonMain = [[boutonHome alloc] init];
    //self.boutonMain = aBoutonMain;
    //[boutonMain setCenter:CGPointMake(200, 200)];
    //[self addSubview:boutonMain];
}

- (id) init 
{
    if (self = [super init])
    {
        NSLog(@"Classe TapScrollView initiée");
    }
    return self;
}


-(void)initierScrollView
{
    int i;
    for (i=0; i<6; i++) {

        UIImage *image = [UIImage imageNamed:@"back.png"];
        UIImageView *bouton = [[UIImageView alloc] initWithImage:image];
        [bouton setTag:i];
        [bouton setFrame:CGRectMake(72+20*i,10,62,55)];
        [classementBoutons insertObject:bouton atIndex:i];
        bouton.userInteractionEnabled = YES;

        UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
        recognizer.delegate = self; 
        [bouton addGestureRecognizer:recognizer];
        [self addSubview:bouton];
}
}
//- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
//{    
//  UITouch *touch = [touches anyObject];
//[super touchesBegan:touches withEvent:event];

  //  for (o=1; o<6; o++) {
  // if ([touch view] ==  [self viewWithTag:o]) 
  // {
  //   UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:[classementBoutons objectAtIndex:o] action:@selector(handlePanGesture:)];
   // [[classementBoutons objectAtIndex:o] addGestureRecognizer:recognizer];
   // bouton01 = [self viewWithTag:o];
   // }
   // }

    //CGPoint touchPoint = [touch locationInView:self];
    //[self animateFirstTouchAtPoint:touchPoint];
  //  return;
//}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (n=0; n<6; n++) {
        NSLog(@"touche cancelled");
        [[classementBoutons objectAtIndex:n] setFrame:CGRectMake((72+20)*n,10,62,55)];
    }


}

//- (id<TapScrollViewDelegate>) delegate {
//   return (id<TapScrollViewDelegate>)super.delegate;
//}

//- (void) setDelegate:(id<TapScrollViewDelegate>) aDelegate 
//{
//    super.delegate = aDelegate;
//}

#define GROW_ANIMATION_DURATION_SECONDS 0.15
#define SHRINK_ANIMATION_DURATION_SECONDS 0.15



-(IBAction)handlePanGesture:(UIPanGestureRecognizer*)recognizer
{
    NSLog(@"Mouvement ok");
    CGPoint translation = [recognizer translationInView:self];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self];

}
@end

1 个答案:

答案 0 :(得分:2)

尝试指定:

@interface TapScrollView : UIScrollView <UIGestureRecognizerDelegate> {

通过这种方式,你的警告应该会消失,虽然我不得不承认我还没有完全清楚你想要完成什么,所以可能还有其他问题。