我试图让我的用户将图像从UICollectionView拖出到主视图上。但是,我在从集合视图中删除所述图像视图并从主视图添加的阶段遇到问题。问题是这似乎停止了对平移的跟踪,因此用户必须拖动对象一次以使其进入主视图并再次将其移动到所需位置。我不知道如何解决这个问题,这是我的代码的工作原理:
在cellForItemAt中创建图像视图后
let panRec = UIPanGestureRecognizer()
panRec.addTarget(self, action:#selector(willDraggedView(_:)))
imageDupe.addGestureRecognizer(panRec)
imageDupe.isUserInteractionEnabled = true
切换视图
func willDraggedView(_ sender:UIPanGestureRecognizer){
if (sender.state == UIGestureRecognizerState.began) {
if (sender.view?.superview != self.view) {
sender.view?.removeFromSuperview()
let orig = viewToOrigin[sender.view as! UIImageView]
sender.view?.frame = CGRect(x: orig!.x, y: orig!.y + screenWidth+45, width: (sender.view?.frame.width)!, height: (sender.view?.frame.height)!)
self.view.addSubview(sender.view!)
}
}
draggedView(sender)
}
实际平移
func draggedView(_ sender:UIPanGestureRecognizer){
let translation = sender.translation(in: self.view)
let tmp=sender.view?.center.x //x translation
let tmp1=sender.view?.center.y //y translation
sender.view?.layer.cornerRadius = 0
sender.view?.center=CGPoint(x:tmp!+translation.x, y:tmp1!+translation.y)
sender.setTranslation(CGPoint.zero, in: self.view)
self.view.bringSubview(toFront: sender.view!)
}
这是我正在阅读的日志
2016-11-07 09:06:40.555 ScriptBuilder[93937:18223071] unexpected nil window in __dispatchPreprocessedEventFromEventQueue, _windowServerHitTestWindow: <UIWindow: 0x7f9013405cd0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x608000053290>; layer = <UIWindowLayer: 0x60800003d7c0>>