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

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

问题描述

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

  • 当用户点击按钮时
  • 当用户按下按钮并将他/她的手指拖出按钮区域时
  • 当用户将手指从按钮区域外拖到按钮区域内时

基本上,无论何时触摸按钮的任何部分,无论触摸的来源如何,我都希望触发该方法,但希望通过操作 UIButton 属性来完成此操作,如 touchedUpInside 等.p>

感谢您的建议!

解决方案

关于:

<块引用>
  • 当用户按下按钮并将他/她的手指拖出按钮区域时

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

(在本例中,您继承了 UIButton,例如:class FadingButton: UIButton)

<代码>...//用户点击按钮,然后移动手指.覆盖 func touchesMoved(_ touches: Set, with event: UIEvent?) {super.touchesMoved(touches, with: event)//获取手指移动到的点if let point: CGPoint = touches.first?.location(in: self) {//如果手指被拖到按钮之外...if ((point.x <0 || point.x > bounds.width) ||(point.y < 0 || point.y > bounds.height)) {//在这里做任何你需要的事情}}}...

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

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

  • When the user taps the button
  • When the user presses the button and drags his/her finger out of the button's area
  • When the user drags his/her finger from outside the button's area to inside the button's area

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.

Thanks for the suggestions!

解决方案

Regarding:

  • When the user presses the button and drags his/her finger out of the button's area

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

(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天全站免登陆