Swift UIGestureRecogniser关注手指 [英] Swift UIGestureRecogniser follow finger

查看:316
本文介绍了Swift UIGestureRecogniser关注手指的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift制作iOS8应用。我希望用户能够使用手势来显示界面的某些部分。因此,例如,用户将他们的手指向上滑动,他们滑动手指的视图移开,按照他们的手指显示下面的另一个视图。

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.

我是什么'd like是一种手势,可以提供类似于通知框的结果,您可以从屏幕顶部向下拉。我一直在查看文档,我似乎无法找到合适的手势。

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.

我看到一个名为UISwipeGestureRecogniser,但唯一的问题是,它没有'当你向上/向下滑动手指时,它会按照你的手指运行。

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.

这是文档页面:
https://developer.apple.com/documentation/uikit/uigesturerecognizer

推荐答案

您正在寻找 UIPanGestureRecognizer 。您将找到Apple文档此处

You're looking for the UIPanGestureRecognizer. You'll find the Apple Documentation here.

这是一个用手指移动视图的示例处理程序。在 Interface Builder 中,将 UIPanGestureRecognizer 添加到您希望能够拖动的视图中。将委托设置为 ViewController 。将操作设置为此操作:

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:

Swift 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:

@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)
    }
}

当然,您可以通过编程方式添加 UIPanGestureRecognizer

Of course, you can add the UIPanGestureRecognizer programmatically:

viewDidLoad 对于 ViewController ,创建识别器并将其添加到您想要拖动的视图中:

In viewDidLoad for your ViewController, create the recognizer and add it to the view you want to be able to drag:

    let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
    self.someDraggableView.addGestureRecognizer(gestureRecognizer)

这篇关于Swift UIGestureRecogniser关注手指的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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