2013-10-02 68 views
1

我有以下的代碼添加到UITapGestureRecognizer一個UIImageView:的UIImageView GestureRecognizer不工作

UIImageView *circleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"red_circle"]]; 
    [circleView setUserInteractionEnabled:YES]; 

    [circleView setFrame:CGRectMake(20 + 60 * ([self.tasks count] - 1), self.bounds.size.height - 300, 44, 44)]; 

    // register gestures 
    [self registerGestureRecognizer:circleView]; 

    [self addSubview:circleView]; 


-(void) registerGestureRecognizer:(UIImageView *) circleView 
{ 
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)]; 
    tapGestureRecognizer.numberOfTapsRequired = 1; 
    tapGestureRecognizer.delegate = self; 
    [circleView addGestureRecognizer:tapGestureRecognizer]; 
} 

-(void) tapped:(UITapGestureRecognizer *) recognizer 
{ 
    NSLog(@"tapped!"); 
} 

但是,當我觸碰圖像的挖掘方法不會被調用!我錯過了什麼。

包含UIImageView的視圖被添加到UIScrollView。

更新:這裏是增加了一個TaskView到的UIScrollView

代碼
-(TaskView *) createTaskView 
{ 
    TaskView *taskView = [[TaskView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

    taskView.userInteractionEnabled = YES; 

    taskView.backgroundColor = [UIColor clearColor]; 

    [taskView.textView setDelegate:self]; 

    return taskView; 
} 

-(void) initializeScrollViewWithTasks 
{ 
    for(int day = 1; day <= 7; day++) 
    { 
     TaskView *taskView = [self createTaskView]; 
     taskView.tag = day; 
     // [_taskViews addObject:taskView]; 
     [self.scrollView addSubview:taskView]; 
    } 
} 

SOLUTION:

完全是我的錯!將項目添加到UIScrollView時,我正在做一些瘋狂的事情。修復!

+0

適合我。您可能需要在'self'上啓用用戶交互,並且可能會查看鏈條。 – Kevin

+0

UIImageView包含在UIView中,UIView添加到UIScrollView中。 –

+0

circleView.userInteractionEnabled = YES;並確保沒有攔截輕擊事件的圖像視圖的UIView前端 –

回答

1

您需要將您的UIImageView的屬性userInteractionEnabled設置爲YES。替換你的代碼是這樣的:

-(void) registerGestureRecognizer:(UIImageView *) circleView 
{ 
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)]; 
    tapGestureRecognizer.delegate = self; 
    tapGestureRecognizer.numberOfTapsRequired = 1; 
    circleView.userInteractionEnabled=YES; 
    [circleView addGestureRecognizer:tapGestureRecognizer]; 
} 

-(void) tapped:(UITapGestureRecognizer *) recognizer 
{ 
    NSLog(@"tapped!"); 
} 
+0

沒有setter方法'setIsUserInteractionEnabled:'分配給屬性 –

+0

看我的代碼:'circleView.userInteractionEnabled = YES;' –

+0

謝謝!只是做了這個改變,但仍然沒有成功。 –

相關問題