TapGestureRecognition on multiple uiview in single uiviewcontroller

142 Views Asked by At

I am developing application in which subview four UIVIEW adding TapGestureRecognition. but selector is working for fourth view only. Can anyone tell me where my logic is wrong.

here is code In viewdidload I create four view dynamically.

 Where imageframe and contentarea is Uiview:
contentarea addsubview imageframe.
self.view addsubview contentarea

rect =CGRectMake(0,0 , 160, 230);
        view1 = [[UIView alloc]initWithFrame:rect];
        view1.backgroundColor=[UIColor greenColor];
        view1.tag=viewtag;
        [view1.layer setBorderColor: [[UIColor darkGrayColor] CGColor]];
        [view1.layer setBorderWidth: 0.5];
        [self.imageFrame addSubview:view1];

        rect =CGRectMake(161,0, 159, 230);
        view2 = [[UIView alloc]initWithFrame:rect];
        view2.tag=viewtag+1;
        view2.backgroundColor=[UIColor blueColor];
        [view2.layer setBorderColor: [[UIColor darkGrayColor] CGColor]];
        [view2.layer setBorderWidth: 0.5];
        [self.imageFrame addSubview:view2];

        rect =CGRectMake(0,231 , 160, 230);
        view3 = [[UIView alloc]initWithFrame:rect];
        view3.tag=viewtag+2;
        view3.backgroundColor=[UIColor redColor];
        [view3.layer setBorderColor: [[UIColor darkGrayColor] CGColor]];
        [view3.layer setBorderWidth: 0.5];
        [self.contentArea addSubview:view3];

        rect =CGRectMake(161,231 , 169, 230);
        view4 = [[UIView alloc]initWithFrame:rect];
        view4.tag=viewtag+3;
        view4.backgroundColor=[UIColor redColor];
        [view4.layer setBorderColor: [[UIColor darkGrayColor] CGColor]];
        [view4.layer setBorderWidth: 0.5];
        [self.contentArea addSubview:view4];


     UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];

        [singleTap setNumberOfTapsRequired:1];
        [singleTap setNumberOfTouchesRequired:1];
        [view1 addGestureRecognizer:singleTap];
        [view2 addGestureRecognizer:singleTap];

        [view3 addGestureRecognizer:singleTap];

        [view4 addGestureRecognizer:singleTap];
1

There are 1 best solutions below

0
On

Gesture recognizers can be attached to only one view. Unfortunately, you can't find this in documentation, but you may notice that recognizer class has a view property, which is a pointer to view the gesture is attached to.