ios 您可以将 UIGestureRecognizer 附加到多个视图吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4747238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:36:28  来源:igfitidea点击:

Can you attach a UIGestureRecognizer to multiple views?

iosobjective-cuigesturerecognizer

提问by kubi

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

In the above code only taps on view2are recognized. If I comment out the third line then taps on view1are recognized. If I'm right and you can only use a gesture recognizer once, I'm not sure if this is a bug or it just needs some more documentation.

在上面的代码中,只能view2识别点击。如果我注释掉第三行,则view1可以识别点击。如果我是对的并且您只能使用一次手势识别器,我不确定这是一个错误还是只需要更多文档。

回答by TomSwift

A UIGestureRecognizeris to be used with a single view. I agree the documentation is spotty. That UIGestureRecognizerhas a single viewproperty gives it away:

AUIGestureRecognizer将与单个视图一起使用。我同意文档是参差不齐的。这UIGestureRecognizer有一个单一的view属性给它扔掉:

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.

看法

手势识别器附加到的视图。(只读)

@property(nonatomic, readonly) UIView *view

讨论 使用 addGestureRecognizer: 方法将手势识别器附加(或添加)到 UIView 对象。

回答by kwalker

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

然后在我的 handleLongPress 方法中,我只是设置一个 UIButton 等于手势识别器的视图,并根据该按钮分支我所做的事情

- (void)handleLongPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
        UIButton *whichButton=(UIButton *)[gesture view];
        selectedButton=(UIButton *)[gesture view];
    ....
}

回答by George Asda

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

对于 Swift 3,以防万一有人需要:基于上面的 Bhavik Rathod 回答。

 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())

回答by Bhavik Rathod

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

我们可以做这样的事情,很简单

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

1)在您的控制器中创建如下函数(此函数将返回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

2) 现在在多个视图中设置此识别器

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

回答by Joseph Lord

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

不,您不应将手势识别器附加到多个视图。

There is this explicit information in the Apple documentation:

Apple 文档中有此明确信息:

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 RecognizersApple Developer Library

iOS 事件处理指南 - 手势识别器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.

正如其他人提到的那样,它们在某些情况下可能会起作用,但这显然违反了文档,并且可能会在任何未来的 iOS 版本中更改。

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

您可以做的是将单独的手势识别器添加到您要监视的视图中,它们可以共享一个通用操作。

回答by rohan-patel

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

好吧,如果有人不想为多个按钮添加手势视图(如kwalker上面已经回答),并且想通过 Interface Builder 来完成,这可能会对您有所帮助。

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

1) 您可以从对象库中添加长按手势识别器,就像添加其他对象(如 UIButtons 和 UILabels)一样。

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

在此处输入图片说明最初我最终使用的是我只拿了一个

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

2) 将引用出口设置为UIButton文件所有者并发送操作。

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

注意:如果您有多个 UIButton 或任何其他对象,您将需要为每个对象使用单独的手势识别器。有关更多详细信息,请参阅我的这个问题。在长按手势识别器上获取错误的 UIButton 标签

回答by Raynaldio Limarga

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

这样会减少多个不同的无用变量

回答by Martin

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:

要在视图上添加 2 tap 识别器,您只需调用:

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.

等等。请记住,目标必须链接到选择器。

回答by AnkitRox

Override class by '<UIScrollViewDelegate>'

通过“ <UIScrollViewDelegate>”覆盖类

And use this method in .m class:

并在 .m 类中使用此方法:

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

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

此方法将帮助您在单个视图上启用多次滑动。

回答by febaisi

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

每次添加指向相同功能的手势识别器时,如何重新编写(重新创建)您的 GestureRecognize。在下面的情况下它有效。我正在使用 IBOutletCollection

Swift 2:

斯威夫特 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")
}