我需要通过 PanGestureRecognizer
拖动 UIView(我知道该怎么做),但我不知道如何限制它。需要从顶部填充一些,如果与设备的四个侧面之一(左、右、顶部(这里是填充)和底部)发生碰撞,则停止拖动并且您不能超过 - 或像顶部那样的 1px 填充, 任何。 :)
我试过这个:https://github.com/andreamazz/UIView-draggable但如果我通过 cagingArea 设置限制区域,iPad (Air) 会滞后。还有移动不流畅,我觉得原生PanGestureRecognizer最好,只需要限制区域,你知道我该怎么做吗? :)
我正在用 Swift 编写。并且还找到了一些相关主题,比如这个 -> Use UIPanGestureRecognizer to drag UIView inside limited area但我不知道 insideDraggableArea
在做什么?..
非常感谢程序员!
最佳答案
我在项目中遇到的相同问题, 试试这个,
1) 初始化PanGesture
let panRec = UIPanGestureRecognizer()
2) 将 PanGesture 添加到您的 UIView
override func viewDidLoad() {
....
....
panRec.addTarget(self, action: "draggedView:")
yourview.addGestureRecognizer(panRec)
yourview.userInteractionEnabled = true
....
....
}
3) 设置对 draggedView 函数的限制
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)
}
}
关于ios - 如何允许在某些区域拖动 UIView (PanGesture ..),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31991808/