Can you attach a UIGestureRecognizer to multiple views?

前端 未结 12 883
忘了有多久
忘了有多久 2020-11-22 14:08
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTapTap:)];
[self.view1 addGestureRecognizer:tapGesture];         


        
12条回答
  •  情话喂你
    2020-11-22 15:06

    We can do something Like this, it's easy and simple

    1) create function as below in your controller (this function will return GestureRecognizer)

    -(UITapGestureRecognizer*)setRecognizer{
         UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openProfile)];
         [gestureRecognizer setNumberOfTapsRequired:1];
         return gestureRecognizer;
    }
    

    2) now set this recognizer in multiple views

    [self.view1 addGestureRecognizer:[self setRecognizer]]; 
    [self.view2 addGestureRecognizer:[self setRecognizer]];
    

提交回复
热议问题