防止同时单击Imageviews

时间:2016-08-05 06:55:57

标签: ios objective-c uiimageview uitapgesturerecognizer

我有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];
}

3 个答案:

答案 0 :(得分: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;
    }

}

答案 1 :(得分:0)

当您点按图片视图时,将Bool变量设置为true并管理该变量。

答案 2 :(得分:0)

如果您想阻止同时点击图片浏览,可以按exclusiveTouch设置YES

/* 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];
    }

我希望这会有所帮助