我是新手,标题是,想要实现自定义拖动和放大器放入UITableView
,而每个UITableViewCell
内容(cell.content?)将包含自定义UIView
。例如。 Row1-cell-CustomUIView1,Row2-cell-CustomUIView2。哪个自定义UIView
添加到哪一行基于该行上的用户选择和按钮单击。现在假设您有4个自定义UIViews
,即在UITableView
中添加到4行/单元格中,如何实现这些CustomUIView
的拖放?注意,每个自定义UIView可能与典型的表格单元格不同。即自定义UIView1布局/设计与自定义UIView2
不同。快速实施,任何想法?
答案 0 :(得分:1)
这可以帮助你,在mainImgView中添加一个UIPanGestureRecognizer来处理拖动:
var draggableImage : UIImageView!
var mainImgView : UIImageView!
var trashImgView : UIImageView!
var dragStartPositionRelativeToCenter : CGPoint?
var panGesture : UIPanGestureRecognizer!
panGesture = UIPanGestureRecognizer(target: self, action: "handleDrag:")
mainImgView.addGestureRecognizer(panGesture)
func handleDrag(nizer: UIPanGestureRecognizer!) {
if nizer.state == UIGestureRecognizerState.Began {
self.draggableImage = UIImageView(image: mainImgView.image)
self.draggableImage.frame = CGRectMake(0, 0, 100, 100)
self.draggableImage.center = mainImgView.center
self.draggableImage.contentMode = .ScaleAspectFit
self.draggableImage.userInteractionEnabled = true
self.draggableImage.alpha = 0.5
self.view.addSubview(self.draggableImage)
self.view.bringSubviewToFront(sself.draggableImage)
let locationInView = nizer.locationInView(self.draggableImage.superview)
dragStartPositionRelativeToCenter = CGPoint(x: locationInView.x - self.draggableImage.center.x, y: locationInView.y - self.draggableImage.center.y)
self.draggableImage.layer.shadowOffset = CGSize(width: 0, height: 20)
self.draggableImage.layer.shadowOpacity = 0.3
self.draggableImage.layer.shadowRadius = 6
return
}
if nizer.state == UIGestureRecognizerState.Changed {
let locationInView = nizer.locationInView(self.draggableImage.superview)
UIView.animateWithDuration(0.1) {
self.draggableImage.center = CGPoint(x: locationInView.x - self.dragStartPositionRelativeToCenter!.x,
y: locationInView.y - self.dragStartPositionRelativeToCenter!.y)
}
var checkAddToBag : Bool = CGRectContainsRect(self.draggableImage.frame, self.trashImgView.frame)
if (checkAddToBag == true){
self.trashImgView.layer.borderColor = UIColor.redColor().CGColor
self.trashImgView.layer.borderWidth = 1.0
} else {
self.trashImgView.layer.borderColor = UIColor.clearColor().CGColor
self.trashImgView.layer.borderWidth = 0.0
}
return
}
if nizer.state == UIGestureRecognizerState.Ended {
var checkAddToBag : Bool = CGRectContainsRect(self.draggableImage.frame, self.trashImgView.frame)
self.trashImgView.layer.borderColor = UIColor.clearColor().CGColor
self.trashImgView.layer.borderWidth = 0.0
dragStartPositionRelativeToCenter = nil
self.draggableImage.removeFromSuperview()
self.draggableImage = nil
return
}
}