【问题标题】:selector function not called when adding list of image views with tap gesture recognizer使用点击手势识别器添加图像视图列表时未调用选择器函数
【发布时间】:2014-01-05 08:09:45
【问题描述】:

我无法通过点击手势创建 ImageView 列表。当我创建手势时,不会调用选择器函数。当我创建只有一个 imageView 函数被调用。知道为什么吗?都是大滚动视图的子视图。

这是我的代码:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self     action:@selector(imageTaped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
for(int k=0;k<MyList.count;k++){
    for(int i=0;i<listSize;i++){
        UIImageView *clickForDetail =[[UIImageView alloc]initWithFrame:CGRectMake(i*HELLO_WIDTH,k*LIST_ROW_HEIGH ,HELLO_WIDTH, LIST_ROW_HEIGH)];
        clickForDetail.backgroundColor = [UIColor clearColor];
        clickForDetail.tag = tag;
        clickForDetail.userInteractionEnabled = YES;
        [clickForDetail addGestureRecognizer:singleTap];

        [myScroll addSubview:clickForDetail];
        tag++;
    }
}

和选择器功能:

-(void)imageTaped: (UITapGestureRecognizer *)recognizer
{
    NSLog(@"single Tap on imageview");
}

是否有可能以某种方式获取被点击的 ImageView 的标签?

【问题讨论】:

    标签: ios objective-c uitableview memory uiscrollview


    【解决方案1】:

    您需要为每个视图对象设置不同的点击手势

    for(int k=0;k<MyList.count;k++){
        for(int i=0;i<listSize;i++){
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self     action:@selector(imageTaped:)];
    singleTap.numberOfTapsRequired = 1;
    singleTap.numberOfTouchesRequired = 1;
            UIImageView *clickForDetail =[[UIImageView alloc]initWithFrame:CGRectMake(i*HELLO_WIDTH,k*LIST_ROW_HEIGH ,HELLO_WIDTH, LIST_ROW_HEIGH)];
            clickForDetail.backgroundColor = [UIColor clearColor];
            clickForDetail.tag = tag;
            clickForDetail.userInteractionEnabled = YES;
            [clickForDetail addGestureRecognizer:singleTap];
    
            [epgScroll addSubview:clickForDetail];
            tag++;
        }
    }
    
    
    -(void)imageTaped: (UITapGestureRecognizer *)recognizer
    {
        NSLog(@"single Tap on imageview");
    UIImageView *selectedTextView = (UIImageView *)recognizer.view;
    }
    

    【讨论】:

    • 是否可以通过其标签检测到哪个图像视图被点击?
    • 谢谢!这就是我所需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多