Can you attach a UIGestureRecognizer to multiple views?

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


        
相关标签:
12条回答
  • 2020-11-22 14:49

    Well if someone does not want to code for adding gesture view for multiple buttons like kwalker has answered above, and want to do it via Interface Builder this may help you.

    1) You can add Long Press gesture Recognizer from Object Library like you add other objects like UIButtons and UILabels.

    enter image description here Initially what I ended up using was I took only one

    2) Set referencing outlets to UIButton and sent actions with File's Owner.

    enter image description here

    Note: If you have multiple UIButton or any other object you will need separate gesture recognizer for each of them. For more details please refer to this question of mine.Getting wrong UIButton tag on Long press gesture recognizer

    0 讨论(0)
  • 2020-11-22 14:52

    You could create a generic extension on view to add gesture recognizers easily. This is just an example but it could look like this

    extension UIView {
    
        func setGestureRecognizer<Gesture: UIGestureRecognizer>(of type: Gesture.Type, target: Any, actionSelector: Selector, swipeDirection: UISwipeGestureRecognizer.Direction? = nil, numOfTaps: Int = 1) {
        let getRecognizer = type.init(target: target, action: actionSelector)
    
        switch getRecognizer {
        case let swipeGesture as UISwipeGestureRecognizer:
            guard let direction = swipeDirection else { return }
            swipeGesture.direction = direction
            self.addGestureRecognizer(swipeGesture)
        case let tapGesture as UITapGestureRecognizer:
            tapGesture.numberOfTapsRequired = numOfTaps
            self.addGestureRecognizer(tapGesture)
        default:
            self.addGestureRecognizer(getRecognizer)
        }
      }
    
    }
    

    To add a 2 tap recognizer on a view you would just call:

    let actionSelector = #selector(actionToExecute)
    view.setGestureRecognizer(of: UITapGestureRecognizer.self, target: self, actionSelector: actionSelector, numOfTaps: 2)
    

    You could also easily add a swipe recognizer

    view.setGestureRecognizer(of: UISwipeGestureRecognizer.self, target: self, actionSelector: actionSelector, swipeDirection: .down)
    

    and so on. Just remember that the target must be linked to the selector.

    0 讨论(0)
  • 2020-11-22 14:54

    No you should not attach gesture recognizers to more than one view.

    There is this explicit information in the Apple documentation:

    Gesture Recognizers Are Attached to a View

    Every gesture recognizer is associated with one view. By contrast, a view can have multiple gesture recognizers, because a single view might respond to many different gestures. For a gesture recognizer to recognize touches that occur in a particular view, you must attach the gesture recognizer to that view.

    Event Handling Guide for iOS - Gesture Recognizers Apple Developer Library

    While as others mention they might work in some cases it is clearly against the documentation and could change in any future iOS version.

    What you can do is add separate gesture recognisers to the views you want to monitor and they can share a common action.

    0 讨论(0)
  • 2020-11-22 14:58

    A UIGestureRecognizer is to be used with a single view. I agree the documentation is spotty. That UIGestureRecognizer has a single view property gives it away:

    view

    The view the gesture recognizer is attached to. (read-only)

    @property(nonatomic, readonly) UIView *view

    Discussion You attach (or add) a gesture recognizer to a UIView object using the addGestureRecognizer: method.

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

    You can do it using this code my views which are imageviews in the xib.

    - (void)viewDidLoad
    {
        firstIV.tag = 501;
        secondIV.tag = 502;
        thirdIV.tag = 503;
        forthIV.tag = 504;
    
        [self addTapGesturetoImageView: firstIV];
        [self addTapGesturetoImageView: secondIV];
        [self addTapGesturetoImageView: thirdIV];
        [self addTapGesturetoImageView: forthIV];
    }
    
    -(void)addTapGesturetoImageView:(UIImageView*)iv
    {
        iv.userInteractionEnabled = YES;
        UITapGestureRecognizer * textfielBGIVTapGasture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textfielBGIVTapped:)];
        textfielBGIVTapGasture.numberOfTapsRequired = 1;
        [iv addGestureRecognizer:textfielBGIVTapGasture];
    }
    
    - (void)textfielBGIVTapped:(UITapGestureRecognizer *)recognizer {
        int tag = recognizer.view.tag-500;
        switch (tag) {
            case 1:
            {
                //firstIV tapped;
                break;
            }
            case 2:
            {
                //secondIV tapped;
                break;
            }
            case 3:
            {
                //thirdIV tapped;
                break;
            }
            case 4:
            {
                //forthIV tapped;
                break;
            }
            default: {
                break;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 15:04

    Override class by '<UIScrollViewDelegate>'

    And use this method in .m class:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
        return YES;
    }
    

    This method will help you to enable multiple swipe on a single view..

    0 讨论(0)
提交回复
热议问题