Can you attach a UIGestureRecognizer to multiple views?

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


        
相关标签:
12条回答
  • 2020-11-22 15:05

    What about re write (recreate) your GestureRecognize every time that you add a gesture recognizer pointing to the same func. In below case it works. I am using IBOutletCollection

    Swift 2:

    @IBOutlet var topicView: [UIView]!
    
    override func viewDidLoad() {
            for view in self.topicView as [UIView] {
            view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "viewClicked:"))
        }
    }
    
    func viewClicked(recognizer: UITapGestureRecognizer) {
        print("tap")
    }
    
    0 讨论(0)
  • 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]];
    
    0 讨论(0)
  • 2020-11-22 15:08

    if you have fixed view I suggest you doing something like this

    [self.view1 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTapTap:)]];
    [self.view2 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTapTap:)]];
    

    that way will reduce multiple different useless variable

    0 讨论(0)
  • 2020-11-22 15:10

    I got around it by using the below.

    for (UIButton *aButton in myButtons) {
    
                UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
                longPress.minimumPressDuration=1.0;
                [aButton addGestureRecognizer:longPress];
                [longPress release];
    
    }
    

    Then in my handleLongPress method I just set a UIButton equal to the view of the gesture recognizer and branch what I do based upon that button

    - (void)handleLongPress:(UILongPressGestureRecognizer*)gesture {
        if ( gesture.state == UIGestureRecognizerStateEnded ) {
            UIButton *whichButton=(UIButton *)[gesture view];
            selectedButton=(UIButton *)[gesture view];
        ....
    }
    
    0 讨论(0)
  • 2020-11-22 15:11

    I know this is an old post but I figured something similar and hopefully it's useful someone else. I simply stored my imageViews in an array and assigned it to to the same gesture recognizer in a function to set up each image view.

    In my viewDidLoad():

    imageViewList = [imageView, imageView2, imageView3]
    setupImageViews(imageViews: imageViewList)
    

    Function to setup image views:

    func setupImageViews(imageViews: [UIImageView]) {
        
        for imageView in imageViews {
            let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
            imageView.isUserInteractionEnabled = true
            imageView.addGestureRecognizer(tapGestureRecognizer)
            
            //set up image to be displayed with the right aspect
            imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
            imageView.contentMode = .scaleAspectFit // OR .scaleAspectFill
            imageView.clipsToBounds = true
        }
    }
    

    And in the action selector imageTapped(), you can have corresponding code for whichever image view tapped.

    @objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
    {
        switch tapGestureRecognizer.view {
        case imageView:
            print("tapped Image View 1") //add your actions here
        case imageView2:
            print("tapped Image View 2") //add your actions here
        case imageView3:
            print("tapped Image View 3") //add your actions here
        default:
            print("Tap not detected")
        }
        _ = tapGestureRecognizer.view as! UIImageView
        //additional code...
    }
    
    0 讨论(0)
  • 2020-11-22 15:12

    For Swift 3 in case anyone requires this: Based on Bhavik Rathod Answer above.

     func setGestureRecognizer() -> UIPanGestureRecognizer {
    
            var panRecognizer = UIPanGestureRecognizer()
    
            panRecognizer = UIPanGestureRecognizer (target: self, action: #selector(pan(panGesture:)))
            panRecognizer.minimumNumberOfTouches = 1
            panRecognizer.maximumNumberOfTouches = 1
            return panRecognizer
        }
    
            ///set the recognize in multiple views
            view1.addGestureRecognizer(setGestureRecognizer())
            view2.addGestureRecognizer(setGestureRecognizer())
    
    0 讨论(0)
提交回复
热议问题