0

我正在开发应用程序,其中子视图四个UIVIEW添加TapGestureRecognition. 但选择器仅适用于第四个视图。谁能告诉我我的逻辑哪里错了。

这是代码在viewdidload我动态创建四个视图中。

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

1 回答 1

0

手势识别器只能附加到一个视图。不幸的是,您在文档中找不到这个,但您可能会注意到识别器类有一个 view 属性,它是一个指针,用于查看附加的手势。

于 2013-08-22T10:05:59.073 回答