ios Swift UIGestureRecogniser 跟随手指
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25503537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift UIGestureRecogniser follow finger
提问by Francis
I'm making an iOS8 app using Swift. I'd like the user to be able to use gestures to reveal certain parts of the interface. So for example, the user slides their finger up and the view they slid their finger up moves out of the way, following their finger to reveal another view underneath.
我正在使用 Swift 制作一个 iOS8 应用程序。我希望用户能够使用手势来显示界面的某些部分。例如,用户向上滑动他们的手指,他们向上滑动手指的视图移开,跟随他们的手指以显示下面的另一个视图。
What I'd like is a gesture to give a result similar to the notification box that you can pull down from the top of the screen. I've been looking at the documentation and I can't seem to find a suitable gesture.
我想要的是一个手势来给出类似于通知框的结果,你可以从屏幕顶部下拉。我一直在查看文档,但似乎找不到合适的手势。
I saw one called UISwipeGestureRecogniser, but the only problem is, it doesn't follow your finger, it simply runs a function when I slide my finger up / down.
我看到了一个叫做 UISwipeGestureRecogniser 的,但唯一的问题是,它不会跟随你的手指,它只是在我向上/向下滑动手指时运行一个函数。
Here's the documentation page: https://developer.apple.com/documentation/uikit/uigesturerecognizer
这是文档页面:https: //developer.apple.com/documentation/uikit/uigesturerecognizer
回答by vacawama
You're looking for the UIPanGestureRecognizer. You'll find the Apple Documentation here.
您正在寻找UIPanGestureRecognizer。您可以在此处找到 Apple 文档。
Here's a sample handler that will move a view with your finger. In Interface Builder, add a UIPanGestureRecognizer
to a view that you want to be able to drag. Set the delegate to your ViewController
. Set the action to this action:
这是一个示例处理程序,它将用您的手指移动视图。在Interface Builder 中,将 a 添加UIPanGestureRecognizer
到您希望能够拖动的视图。将委托设置为您的ViewController
. 将动作设置为此动作:
Swift 2.X:
斯威夫特 2.X:
@IBAction func handlePan(gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .Began || gestureRecognizer.state == .Changed {
let translation = gestureRecognizer.translationInView(self.view)
// note: 'view' is optional and need to be unwrapped
gestureRecognizer.view!.center = CGPointMake(gestureRecognizer.view!.center.x + translation.x, gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPointMake(0,0), inView: self.view)
}
}
Swift 3:
斯威夫特 3:
@IBAction func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view)
// note: 'view' is optional and need to be unwrapped
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
}
}
Of course, you can add the UIPanGestureRecognizer
programmatically:
当然,您可以通过UIPanGestureRecognizer
编程方式添加:
In viewDidLoad
for your ViewController
, create the recognizer and add it to the view you want to be able to drag:
在viewDidLoad
您的 中ViewController
,创建识别器并将其添加到您希望能够拖动的视图中:
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
self.someDraggableView.addGestureRecognizer(gestureRecognizer)