gpt4 book ai didi

swift - 尝试创建拖放到 TableView 。 swift

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

我一直在尝试使用本指南创建拖放操作 http://www.freshconsulting.com/create-drag-and-drop-uitableview-swift/

这是我在viewdidload中的代码

let longpress = UILongPressGestureRecognizer(target: self, action: "longPressGestureRecognized:")

longpress.minimumPressDuration = 1.0

table.addGestureRecognizer(longpress)

还有函数

   func longPressGestureRecognized(gestureRecognizer: UILongPressGestureRecognizer) {


func snapshopOfCell(inputView: UIView) -> UIView {
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0.0)
inputView.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext() as UIImage
UIGraphicsEndImageContext()
let cellSnapshot : UIView = UIImageView(image: image)
cellSnapshot.layer.masksToBounds = false
cellSnapshot.layer.cornerRadius = 0.0
cellSnapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0)
cellSnapshot.layer.shadowRadius = 5.0
cellSnapshot.layer.shadowOpacity = 0.4
return cellSnapshot
}

let longPress = gestureRecognizer as UILongPressGestureRecognizer

let state = longPress.state

var locationInView = longPress.locationInView(table)

var indexPath = table.indexPathForRowAtPoint(locationInView)

struct My {

static var cellSnapshot : UIView? = nil

}
struct Path {

static var initialIndexPath : NSIndexPath? = nil

}

switch state {
case UIGestureRecognizerState.Began:
if indexPath != nil {
Path.initialIndexPath = indexPath
let cell = table.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
My.cellSnapshot = snapshopOfCell(cell)
var center = cell.center

My.cellSnapshot!.center = center

My.cellSnapshot!.alpha = 0.0

table.addSubview(My.cellSnapshot!)

UIView.animateWithDuration(0.25, animations: { () -> Void in
center.y = locationInView.y

My.cellSnapshot!.center = center

My.cellSnapshot!.transform = CGAffineTransformMakeScale(1.05, 1.05)

My.cellSnapshot!.alpha = 0.98

cell.alpha = 0.0

}, completion: { (finished) -> Void in

if finished {

cell.hidden = true


}



})



}


case UIGestureRecognizerState.Changed:
var center = My.cellSnapshot!.center
center.y = locationInView.y
My.cellSnapshot!.center = center
if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) {
swap(&cellContent[indexPath!.row], &cellContent[Path.initialIndexPath!.row])
table.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!)
Path.initialIndexPath = indexPath
}

default:
let cell = table.cellForRowAtIndexPath(Path.initialIndexPath!) as UITableViewCell!
cell.hidden = false
cell.alpha = 0.0
UIView.animateWithDuration(0.25, animations: { () -> Void in
My.cellSnapshot!.center = cell.center
My.cellSnapshot!.transform = CGAffineTransformIdentity
My.cellSnapshot!.alpha = 0.0
cell.alpha = 1.0
}, completion: { (finished) -> Void in
if finished {
Path.initialIndexPath = nil
My.cellSnapshot!.removeFromSuperview()
My.cellSnapshot = nil


}
})

}

}

问题是长按单元格时它会崩溃。

错误代码:longPressGestureRecognized:]: 无法识别的选择器发送到实例 0x79a3b110'

其他一切正常,如删除单元格内容等。

提前致谢!

最佳答案

请再次检查函数“func longPressGestureRecognized(gestureRecognizer: UILongPressGestureRecognizer)”的放置位置我测试了你的代码,一切正常。我可能怀疑你把它放在其他功能中。

关于swift - 尝试创建拖放到 TableView 。 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28929291/

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