Triggering a UIButton#39;s method when user drags finger into button#39;s area?(当用户将手指拖入按钮区域时触发 UIButton 的方法?)
问题描述
我想要一个在以下条件下触发其方法的按钮:
- 当用户点击按钮时
- 当用户按下按钮并将他/她的手指拖出按钮区域时
- 当用户将手指从按钮区域外拖到按钮区域内时
基本上,无论何时触摸按钮的任何部分,无论触摸的来源如何,我都希望触发该方法,但希望通过操作 UIButton 属性来完成此操作,如 感谢您的建议! 关于: 我最近做了类似的事情.这是我在 Swift 中的代码. (在本例中,您继承了 UIButton,例如:touchedUpInside 等.p>class FadingButton: UIButton)<代码>...//用户点击按钮,然后移动手指.覆盖 func touchesMoved(_ touches: Set
我希望它对某人有所帮助.
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 的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当用户将手指拖入按钮区域时触发 UIButton 的方法?
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
