2012-07-01 16 views
2

我實際上是試圖在實現gestureRecognizers的customScrollView類中插入觸摸代表功能(鍵入touchBegan:withEventtouchEnded:withEvent)。手勢識別器代表:如何實現觸摸代表功能

當我嘗試將Recognizer對象的委託設置爲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 

回答

2

嘗試指定:

@interface TapScrollView : UIScrollView <UIGestureRecognizerDelegate> { 

這樣,你的警告應該消失,儘管我不得不承認我並沒有完全清楚你正在努力實現的目標是的,所以可能還有其他問題。

+0

謝謝你的迴應。它的工作,雖然我沒有找到這樣的功能在代表協議的gestureRecognizers(也許它是其超級類的參考)。 –

+2

'touchesCancelled'及其家族屬於'UIResponder',所以它們已經定義了你的滾動視圖派生類。當然,你可以重寫它們,就像你已經做的那樣... – sergio