首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找出哪个按钮是按下的,而长按压和抽头不同。斯威夫特

找出哪个按钮是按下的,而长按压和抽头不同。斯威夫特
EN

Stack Overflow用户
提问于 2020-04-06 21:14:20
回答 2查看 133关注 0票数 0

所以我有一个长而短的按钮的听众,但我需要知道什么按钮被按下。是否有可能找出哪个按钮按下了龙头和长功能,或者我需要为每个按钮做两个功能?

代码语言:javascript
复制
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tap))
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
longGesture.minimumPressDuration = 0.5
smallOption.addGestureRecognizer(tapGesture)
mediumOption.addGestureRecognizer(tapGesture)
largeOption.addGestureRecognizer(tapGesture)
smallOption.addGestureRecognizer(longGesture)
mediumOption.addGestureRecognizer(longGesture)
largeOption.addGestureRecognizer(longGesture)

@objc func tap(_ sender: UIGestureRecognizer){
    print("short-press")
}
@objc func long(_ sender: UIGestureRecognizer){
    print("long-press")
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-06 22:36:44

主要问题是,在这种情况下,这两个手势将只添加到largeOption按钮!为了澄清,只为一个组件添加了手势,在您的示例中,应该只为最新的组件(即largeOption)添加该手势:

代码语言:javascript
复制
smallOption.addGestureRecognizer(tapGesture) <-- skipped
mediumOption.addGestureRecognizer(tapGesture) <-- skipped
largeOption.addGestureRecognizer(tapGesture) <-- added
smallOption.addGestureRecognizer(longGesture) <-- skipped
mediumOption.addGestureRecognizer(longGesture) <-- skipped
largeOption.addGestureRecognizer(longGesture) <-- added

从逻辑上讲,这可能是你问题的答案:

是否有可能找出哪个按钮按下了龙头和长函数,或者我需要为每个按钮执行两个函数?

您需要为每个按钮添加两个手势,因为特定的手势只能添加到一个视图中。

但是,除了@objc func tap(_ sender: UIGestureRecognizer)@objc func long(_ sender: UIGestureRecognizer)现有的操作方法之外,您不必声明新的操作方法。您可以做的是检查sender的视图。例如:

假设我们为每个按钮手动添加了两个手势:

代码语言:javascript
复制
// gestures:
let smallOptionTapGesture = UITapGestureRecognizer(target: self, action: #selector(tap))
let smallOptionLongGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
smallOptionLongGesture.minimumPressDuration = 0.5

let mediumOptionTapGesture = UITapGestureRecognizer(target: self, action: #selector(tap))
let mediumOptionLongGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
mediumOptionLongGesture.minimumPressDuration = 0.5

let largeOptionTapGesture = UITapGestureRecognizer(target: self, action: #selector(tap))
let largeOptionLongGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
largeOptionLongGesture.minimumPressDuration = 0.5

// adding them:
smallOption.addGestureRecognizer(smallOptionTapGesture)
mediumOption.addGestureRecognizer(mediumOptionTapGesture)
largeOption.addGestureRecognizer(largeOptionTapGesture)

smallOption.addGestureRecognizer(smallOptionLongGesture)
mediumOption.addGestureRecognizer(mediumOptionLongGesture)
largeOption.addGestureRecognizer(largeOptionLongGesture)

因此,你可以做的是:

代码语言:javascript
复制
@objc func tap(_ sender: UIGestureRecognizer) {
    // an example of how you could check the button
    if sender.view == smallOption {
        print("small short-press")
    } else if sender.view == mediumOption {
        print("medium short-press")
    } else if sender.view == largeOption {
        print("large short-press")
    }
}
@objc func long(_ sender: UIGestureRecognizer) {
    // you could apply the same above approach here
}

另一个选项是分别为每个按钮创建操作方法。

票数 1
EN

Stack Overflow用户

发布于 2020-04-06 21:45:05

对于每个按钮,您不需要为相同的手势提供两个函数。将方法定义更改为接受UIButton (在您的情况下),而不是UIGestureRecognizer。然后,通过直接验证标签或类型来检查发送者.

代码语言:javascript
复制
@objc func tap(_ sender: UIButton){
  switch sender {
  case button1: // or by tags of buttons
  case button2:
  ...
  default: break
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61069082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档