2

touchBegan:withEvent我实际上是在尝试在touchEnded:withEvent实现gestureRecognizers.

当我尝试将识别器对象的委托设置为 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
4

1 回答 1

2

尝试指定:

@interface TapScrollView : UIScrollView <UIGestureRecognizerDelegate> {

这样,您的警告应该消失了,尽管我不得不承认我还没有完全清楚您要完成的工作,因此可能还有其他问题。

于 2012-07-01T10:37:26.533 回答