Can you attach a UIGestureRecognizer to multiple views?

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


        
12条回答
  •  情书的邮戳
    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.

提交回复
热议问题