如何使uipangeSturer识别器检查手指是否在按钮框架中?
我正在尝试制作一个应用程序,用户可以将手指拖到多个按钮的顶部,并为每个按钮获取一些操作。
有一个类似的问题尝试使用cgrectcontainspoint(button.frame,point)
它说它被button.frame.contains(point)
替换,但这似乎对我不起作用。 的链接
这是照片到目前为止所做
var buttonArray:NSMutableArray!
override func viewDidLoad()
{
super.viewDidLoad()
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureMethod(_:)))
a1.addGestureRecognizer(panGesture)
a2.addGestureRecognizer(panGesture)
}
@objc func panGestureMethod(_ gesture: UIPanGestureRecognizer) {
if gesture.state == UIGestureRecognizer.State.began {
buttonArray = NSMutableArray()
}
let pointInView = gesture.location(in: gesture.view)
if !buttonArray.contains(a1!) && a1.frame.contains(pointInView) {
buttonArray.add(a1!)
a1Tapped(a1)
}
else if !buttonArray.contains(a2!) && a2.frame.contains(pointInView) {
buttonArray.add(a2!)
a2Tapped(a2)
}
:代码确实运行良好,但是当我试图激活阻力时,什么都没有发生。有技巧吗?
I am trying to make an app where the user could drag a finger on top of multiple buttons and get some actions for each button.
There was a similar question from a while back but when I tried to use CGRectContainsPoint(button.frame, point)
it said it was replaced with button.frame.contains(point)
but this didn’t seem to work for me. Here is a link to the Photo
What I have done so far:
var buttonArray:NSMutableArray!
override func viewDidLoad()
{
super.viewDidLoad()
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureMethod(_:)))
a1.addGestureRecognizer(panGesture)
a2.addGestureRecognizer(panGesture)
}
@objc func panGestureMethod(_ gesture: UIPanGestureRecognizer) {
if gesture.state == UIGestureRecognizer.State.began {
buttonArray = NSMutableArray()
}
let pointInView = gesture.location(in: gesture.view)
if !buttonArray.contains(a1!) && a1.frame.contains(pointInView) {
buttonArray.add(a1!)
a1Tapped(a1)
}
else if !buttonArray.contains(a2!) && a2.frame.contains(pointInView) {
buttonArray.add(a2!)
a2Tapped(a2)
}
The code did run fine but when I tried to activate the drag nothing happened. Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要类似我假设的系统键盘的行为吗?
cgRectContainsPoint
未弃用:请参见 noreflowl noreferrer”> docs 。在Swift中,它像frage.contains()
一样。在处理矩形和点时,您必须确保首先将两者都转换为同一坐标系统。为此,您可以使用uiview上的转换/从方法:请参见(文档)。
在您的情况下。就像以下功能一样(首先翻译按钮帧,然后检查点是否在内部):
You want behavior similar to the system keyboard I assume?
CGRectContainsPoint
is not deprecated: See the docs. In Swift it's written likeframe.contains()
.When dealing with rects and points you have to make sure both are translated to the same coordinate system first. To do so you can use the convert to/from methods on UIView: See (the docs).
In your case smth. like the following should work (first translate button frames, then check if the point is inside):