如何允许在某个区域拖动 UIView (PanGesture ..) [英] How to allow drag UIView (PanGesture..) just in some area

查看:27
本文介绍了如何允许在某个区域拖动 UIView (PanGesture ..)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 PanGestureRecognizer 拖动 UIView(我知道怎么做),但我不知道如何限制.需要从顶部填充一些,并且如果与四个侧面之一(左,右,顶部(这里是填充)和设备底部)发生碰撞,请停止拖动并且您不能超过 - 或顶部的 1px 填充, 任何.:)

I need drag UIView via PanGestureRecognizer (I know how to do), but I can't figure out, how to make it with limitation. Need some padding from top and also if there is collision with one of four sides (left, right, top (here is the padding) and bottom of device) stop the drag and you can't over - or 1px padding like on the top, whatever. :)

我试过这个:https://github.com/andreamazz/UIView-draggable 但是如果我通过 cagingArea 设置有限制的区域,iPad (Air) 就会滞后.移动也不流畅,我认为原生的 PanGestureRecognizer 是最好的,只需要限制区域,你知道我该怎么做吗?:)

I tried this one: https://github.com/andreamazz/UIView-draggable but if I set the area with limitation via cagingArea, iPad (Air) is lagged. Also the moving is not smooth, I think the native PanGestureRecognizer is the best, need just the limitation area, do you know how I can do that please? :)

我正在用 Swift 写作.并且还找到了一些相关的话题,比如这个 -> 使用 UIPanGestureRecognizer 拖拽有限区域内的 UIView 但我不知道 insideDraggableArea 在做什么?...

I'm writing in Swift. And also found some related topics, like this one -> Use UIPanGestureRecognizer to drag UIView inside limited area but I don't know what insideDraggableArea doing?..

非常感谢程序员!

推荐答案

我在项目中遇到的同样问题,试试这个,

Same problem that I faced in my project, Try this,

1) 初始化 PanGesture

1) Init PanGesture

let panRec = UIPanGestureRecognizer()

2) 将 PanGesture 添加到您的 UIView

2) Add PanGesture to your UIView

override func viewDidLoad() {
....
....
panRec.addTarget(self, action: "draggedView:")
yourview.addGestureRecognizer(panRec)
yourview.userInteractionEnabled = true
....
....
}

3) 设置对 draggedView 功能的限制

3) Set your limitation on draggedView function

func draggedView(sender:UIPanGestureRecognizer){
        println("panning")

        var translation = sender.translationInView(self.view)

        println("the translation x:\(translation.x) & y:\(translation.y)")

        //sender.view.
        var tmp=sender.view?.center.x  //x translation
        var tmp1=sender.view?.center.y //y translation

        //set limitation for x and y origin
        if(translation.x <= 100 && translation.y <= 50 )
         {
        sender.view?.center=CGPointMake(tmp!+translation.x, tmp1!+translation.y)
         sender.setTranslation(CGPointZero, inView: self.view)
         }
}

这篇关于如何允许在某个区域拖动 UIView (PanGesture ..)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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