繁体   English   中英

向几个UIView添加一个手势识别器

[英]adding one gesture recognizer to a few UIView

所以我有以下代码:

UITapGestureRecognizer *showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[self.storyImageView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[self.storyTitleLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[self.storyImageFailedLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[self.storyImageFailedTextView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];

看来,这仅适用于一个UIView ,这是最后添加的一个UIView 。换句话说, UITapGestureRecognizer且它的视图是一对一的关系。 这个对吗? 我该如何解决? 我是否必须为每个单独创建一个UITapGestureRecog

是的,一个UIView只能有一个UITapRecogniser 对于不同的观点,您必须采用不同的识别器,尽管它们的动作可以相同。
另请参阅链接。

尝试这个,

UITapGestureRecognizer *showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];

[self.storyImageView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];

showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[self.storyTitleLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];

showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[self.storyImageFailedLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];

showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[self.storyImageFailedTextView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];

我认为您只需将手势识别器添加到包含storyImageViewstoryTitleLabel等作为其子视图的视图中。

您可以使用此代码将Same UITapGestureRecognizer添加到多个视图。

步骤是:

  1. 首先,我们使用标签创建三个视图
  2. 然后我们创建NSMutableArray并将此视图添加到数组
  3. 之后,添加UITapGestureRecognizer进行查看
  4. 在UITapGestureRecognizer方法中,我们检查视图的标记以区分被点击的视图。

这是步骤的代码:

-(Void)viewDidLoad {
    [super viewDidLoad];

    //First create three View
    UIView  *view1 = [[UIView alloc] initWithFrame: CGRectMake (5 , 171, 152, 152)];    
    view1.tag = 1;    //add tag to view
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: view1];

    UIView  *  view2 = [[UIView alloc] initWithFrame: CGRectMake ( 163, 171,  152, 152)];   
    view2.tag = 2;   //add tag to view
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: view2];

    UIView * view3 = [[UIView alloc] initWithFrame: CGRectMake ( 5, 330,  152, 152)];    
    view2.tag = 3;    //add tag to view
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: view2];

    //Now create mutable array to hold our view
    NSMutableArray * ary=[[NSMutableArray alloc] init];
    [ary addObject:view1];
    [ary addObject:view2];
    [ary addObject:view3];


    //now we add tap gesture to view
    for (UIView *view in ary) {
        UITapGestureRecognizer * answerDoubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(answerDoubleTapped:)];
        answerDoubleTapGesture.numberOfTapsRequired = 2;
        [answer4View addGestureRecognizer:answerDoubleTapGesture];
    }
   }

-(void)answerDoubleTapped:(UITapGestureRecognizer *)recognizer {
    //Check which view is tapped
    switch (recognizer.view.tag) {
        case 1:
        {
            NSLog(@"First View Tapped");
            break;
        }case 2:
        {
            NSLog(@"Second View Tapped");
            break;
        }case 3:
        {
            NSLog(@"Third View Tapped");
            break;
        }case 4:
        {
            NSLog(@"Forth View Tapped");
            break;
        }default:
        {
            break;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM