Bharathi Bharathi - 4 months ago 11
iOS Question

Prevent simultaneous click on Imageviews

I have 3 image views and I would like to prevent simultaneous taps on image views. how can I do that? Can anyone help me??

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

HDT HDT
Answer

If you want to prevents simultaneous click on Imageviews, you can set exclusiveTouch by 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];
    }

I hope this would be helpful

Comments