防止同时点击 Imageviews

Posted

技术标签:

【中文标题】防止同时点击 Imageviews【英文标题】:Prevent simultaneous click on Imageviews 【发布时间】:2016-08-05 06:55:56 【问题描述】:

我有 3 个图像视图,我想防止同时点击图像视图。我怎样才能做到这一点?谁能帮帮我??

 for (int i=0; i <= [_images1 count]-1; i++)
    CGFloat xOrigin = i * self.view.frame.size.width/3;
    wordsImage = [[UIImageView alloc] init];

    [wordsImage setFrame:CGRectMake(xOrigin+20, self.view.frame.size.height/3,self.view.frame.size.width/3.5 , self.view.frame.size.height/5)];
    [wordsImage setImage:[UIImage imageNamed: [_images1 objectAtIndex:i]]];
    [self.view addSubview:wordsImage];
    [wordsImage setTag:i];
    wordsImage.userInteractionEnabled = YES;

    tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapGesture:index_image:)];
    tapGesture1.numberOfTapsRequired = 1;

    [tapGesture1 setDelegate:self];

    [wordsImage addGestureRecognizer:tapGesture1];

【问题讨论】:

只需设置用户交互模式 = no 用于选择图像 如何使用标签值设置 检查我的答案@Bharathi 【参考方案1】:

使用此方法来限制单击同一图像视图的顺序。我希望这会对你有所帮助。

    int  previousTag,curentTag,flag;
-(void)tapGesture:(id)sender
    UIGestureRecognizer *recognizer = (UIGestureRecognizer*)sender;
    UIImageView *imageView = (UIImageView *)recognizer.view;

    if(flag == 0)
        previousTag = imageView.tag;
        curentTag = 520;  // unequal value you will enter here
        flag = 1;
    
    else 
        curentTag = imageView.tag;
    

    if(previousTag != curentTag)
    
        [imageView setImage:[UIImage imageNamed:@"anyImage.png"]];
        previousTag = curentTag;
    


【讨论】:

【参考方案2】:

如果你想防止同时点击 Imageviews,你可以通过YES 设置exclusiveTouch

/* exclusiveTouch
    A Boolean value that indicates whether the receiver handles touch events exclusively.
    Setting this property to YES causes the receiver to block the delivery of touch events to other views in the same window. The default value of this property is NO.
*/
    for (int i=0; i <= [_images1 count]-1; i++)
        CGFloat xOrigin = i * self.view.frame.size.width/3;
        wordsImage = [[UIImageView alloc] init];

        [wordsImage setFrame:CGRectMake(xOrigin+20, self.view.frame.size.height/3,self.view.frame.size.width/3.5 , self.view.frame.size.height/5)];
        [wordsImage setImage:[UIImage imageNamed: [_images1 objectAtIndex:i]]];
        [self.view addSubview:wordsImage];
        [wordsImage setTag:i];
        wordsImage.userInteractionEnabled = YES;
        wordsImage.exclusiveTouch = YES;//Set this property

        tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapGesture:index_image:)];
        tapGesture1.numberOfTapsRequired = 1;

        [tapGesture1 setDelegate:self];

        [wordsImage addGestureRecognizer:tapGesture1];
    

我希望这会有所帮助

【讨论】:

【参考方案3】:

当您点击图像视图时,将一个 Bool 变量设置为 true 并管理该变量。

【讨论】:

以上是关于防止同时点击 Imageviews的主要内容,如果未能解决你的问题,请参考以下文章

如何在保持程序化行选择的同时防止用户点击行选择?

android 怎么防止两个开关同时点击

iOS中防止两个按钮同时点击的方法

在允许孩子捕捉触摸的同时防止父母被点击

防止按钮连续点击的一个新思路

如何防止在 UINavigationbar 上同时触摸按钮的双重动作?