2012-06-24 38 views
0

I,在ScrollView中使用GestureRecognizer激活UIImageView

我正在嘗試將一個手勢識別器實現到ScrollView中。

我首先創建了一個自定義的ScrollView,其中集成了ImageView對象。

當用戶點擊ImageView時,通常PanGestureRecognizer激活並且ImageView對象跟隨屏幕上的移動。

我已閱讀並遵循關於手勢識別器和Raywenderlich博客(這是非常完善的)的說明。

如果有人有什麼是我的代碼所缺少的一個線索,我會很樂意看它提前

謝謝。這裏是我的代碼

#import <Foundation/Foundation.h> 
#import "mainInterface03.h" 
#import <QuartzCore/QuartzCore.h> 
#import "boutonHome.h" 
#import "DragGestureRecognizer.h" 

@class boutonHome; 
@class DragGestureRecognizer; 

@interface TapScrollView : UIScrollView { 

    // id<TapScrollViewDelegate> delegate; 
    NSMutableArray *classementBoutons; 
    int n; 
    int o; 
    UIView *bouton01; 

} 

@property (nonatomic, retain) UIView *bouton01; 

@property (retain, nonatomic) IBOutletCollection(UIButton) NSMutableSet* buttons; 

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

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


@end 

m.file

#import "TapScrollView.h" 


@implementation TapScrollView 


@synthesize bouton01; 


- (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*i+20,10,62,55)]; 
     [classementBoutons insertObject:bouton atIndex:i]; 
     [self addSubview:bouton]; 
     } 

     UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:bouton01 action:@selector(handlePanGesture:)]; 
     [bouton01 addGestureRecognizer:recognizer]; 

} 
- (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]) 
    { 
    bouton01 = [self viewWithTag:o]; 
    } 
    } 

    return; 
} 



-(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 
+0

你沒有告訴我們什麼是不工作... – Mundi

+0

抱歉,這裏的圖像瀏覽是在這裏,但我不能移動它們。 –

回答

0

我不知道,如果這種設置能夠正常工作。實質上,您將分配任何已被觸摸的視圖到攜帶手勢識別器的bouton01。看起來有點令人費解,而且你的代碼效率也不高。

看起來,當你打電話給[super touchesBegan:touches withEvent:event];時,它會向上傳遞視圖層次結構。只有在此之後,才能將作業分配到bouton01。所以bouton01從來沒有收到觸摸事件似乎是合乎邏輯的。

事實上,正是由於這種奇怪的方法來遍歷視圖並將其分配給具有識別器的錯誤出現的視圖。我建議在安裝過程中將相同的識別器分配給所有相關視圖。

+0

謝謝我找到了解決方案。 –