我需要通过PanGestureRecognizer拖拽UIView (我知道怎么做),但我想不出来,怎么做有限制。需要一些填充从顶部,如果有冲突与四个方面(左,右,顶部(这里是填充)和底部的设备)停止拖动,你不能超过-或1px填充在顶部,无论如何。:)
我尝试了这个:https://github.com/andreamazz/UIView-draggable,但是如果我通过cagingArea设置了区域限制,iPad (空气)是滞后的。而且移动也不顺畅,我觉得原生PanGestureRecognizer是最好的,只需要限制区域,你知道我该怎么做吗?:)
我在用Swift写东西。我还找到了一些相关的主题,比如这个-> Use UIPanGestureRecognizer to drag UIView inside limited area,但我不知道insideDraggableArea在做什么?
非常感谢程序员们!
发布于 2015-08-14 01:15:08
我在我的项目中遇到了同样的问题,试试这个
1)初始化PanGesture
let panRec = UIPanGestureRecognizer()2)在您的UIView中添加PanGesture
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)
}
}发布于 2015-08-14 01:52:24
你必须使用UIPanGestureRecognizer吗?将视图添加到视图控制器,并检查用户交互是否已启用。我认为你应该标记视图并使用touchesMoved方法。
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
if touch.view.tag == 2 {
if self.yourView.center.y <= CGFloat(230) { //if you want use limitation drag
var touchLocation = touch.locationInView(self.view)
self.yourView.center.y = touchLocation.y
self.yourView.center.x = touchLocation.x
}
}
}
}发布于 2020-11-20 00:27:34
这对我来说很有效:
guard let view = UIApplication.shared.windows.last else { return }
view.addSubview(customView)https://stackoverflow.com/questions/31991808
复制相似问题