当用户将手指拖动到按钮区域时触发UIButton的方法吗? [英] Triggering a UIButton's method when user drags finger into button's area?

查看:71
本文介绍了当用户将手指拖动到按钮区域时触发UIButton的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个在以下情况下触发其方法的按钮:

I want to have a button that triggers its method under the following conditions:

  • 当用户点击按钮时
  • 当用户按下按钮并将其手指拖出按钮区域时
  • 当用户将手指从按钮区域的外部拖动到按钮区域的内部

基本上,无论何时触摸按钮的任何部分,我都希望触发该方法,但希望通过操纵UIButton属性(例如touchedUpInside等)来实现此目的.

Basically, anytime any part of the button is touched, regardless of origin of touch, I want the method triggered, but want to accomplish this by manipulating UIButton properties, like touchedUpInside and such.

感谢您的建议!

推荐答案

关于:

  • 当用户按下按钮并将其手指拖出按钮区域时
  • When the user presses the button and drags his/her finger out of the button's area

我最近做了类似的事情.这是我在 Swift 中的代码.

I recently have done something similar. Here is my code in Swift.

(在此示例中,您将UIButton子类化,例如:class FadingButton: UIButton)

(In this example you subclassed UIButton, like: class FadingButton: UIButton)

...

// The user tapped the button and then moved the finger around.
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)

    // Get the point to which the finger moved
    if let point: CGPoint = touches.first?.location(in: self) {

        // If the finger was dragged outside of the button...
        if ((point.x < 0 || point.x > bounds.width) ||
            (point.y < 0 || point.y > bounds.height)) {

            // Do whatever you need here
        }
    }
}
...

我希望它能对某人有所帮助.

I hope it helps someone.

这篇关于当用户将手指拖动到按钮区域时触发UIButton的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆