gpt4 book ai didi

ios - 在 cell.content 中拖放包含自定义 UIView 的 UITableViewCell

转载 作者:行者123 更新时间:2023-11-28 07:01:12 25 4
gpt4 key购买 nike

如题,我是这方面的新手,想在UITableView中实现自定义拖放,而每个 UITableViewCell content (cell.content?) 将包含自定义 UIView .例如。 Row1-cell-CustomUIView1,Row2-cell-CustomUIView2。哪个定制UIView添加到哪一行是基于用户对该行的选择和单击按钮。现在假设您有 4 个自定义 UIViews ,即被添加到 UITableView 中的 4 行/单元格中以及如何实现这些 CustomUIView 的拖放操作?请注意,每个自定义 UIView 可能与典型的表格单元格不同。即自定义 UIView1 布局/设计不同于自定义 UIView2 . swift 实现,有什么想法吗?

最佳答案

这可能对您有所帮助,将 UIPanGestureRecognizer 添加到 mainImgView 以处理拖动:

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
}
}

关于ios - 在 cell.content 中拖放包含自定义 UIView 的 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32002510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com