【发布时间】:2018-02-02 16:08:25
【问题描述】:
我正在尝试使用 tableView:editActionsForRowAt:,按钮带有图像而不是通常的文本。 但也存在一些挑战。有人可以帮我吗?
首先是我已经在做的工作——在 SOF 的一些宝贵帮助下——
这是我想要得到的理想状态:
相关代码如下。上面的第一种情况是在 xShift 和 xShiftInc 都设置为 0.0 时获得的。 第二种情况是通过将 xShift 初始化为 30.0 并将 xShiftInc 初始化为 20.0 获得的。
图形我得到了我想要的结果,但问题是按钮的触摸区域没有像图像一样移动。换句话说,在第二种情况下,如果我触摸垃圾箱,它会打印“upBtn touch!”。 让“rmvBtn感动!”打印我需要触摸垃圾桶的左侧。
另外,我对使用 46.0 作为单元格的高度不太满意,我想要一个更通用的参数(rowHeigh 等于 -1,所以不能使用)。但这是一个不同的问题。
func tableView(_ tableView: UITableView,
editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
var patternImg:UIImage?, xShift = CGFloat(30.0)//CGFloat(0.0)//
let xShiftInc = CGFloat(20.0)//CGFloat(0.0)//
let downBtn = UITableViewRowAction(style: .normal, title: "") {
action, index in
print("downBtn touched!")
}
patternImg = swipeCellImage(named: "DownIcn", side: 46.0, horizOffSet: xShift)
downBtn.backgroundColor = UIColor(patternImage: patternImg!)
let upBtn = UITableViewRowAction(style: .normal, title: "") {
action, index in
print("upBtn touched!")
}
xShift += xShiftInc
patternImg = self.swipeCellImage(named: "UpIcn", side: 46.0, horizOffSet: xShift)
upBtn.backgroundColor = UIColor(patternImage: patternImg!)
let rmvBtn = UITableViewRowAction(style: .destructive, title: "") {
action, index in
print("rmvBtn touched!")
}
xShift += xShiftInc
patternImg = swipeCellImage(named: "TrashIcn", side: 46.0, horizOffSet: xShift)
rmvBtn.backgroundColor = UIColor(patternImage: patternImg!)
return [downBtn,upBtn,rmvBtn]
}
func swipeCellImage(named name: String, side: CGFloat, horizOffSet: CGFloat) -> UIImage? {
let theImage = UIImage(named: name)
UIGraphicsBeginImageContextWithOptions(CGSize(width: UIScreen.main.bounds.width,
height: side), false, UIScreen.main.scale)
let context = UIGraphicsGetCurrentContext()
context!.setFillColor(UIColor.clear.cgColor)
theImage?.draw(in: CGRect(x: horizOffSet, y: 0, width: side, height: side))
let resultImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resultImage
}
【问题讨论】:
-
我更新了我的回答。如果您满意,请打勾并点赞。
标签: ios swift uitableview uitableviewrowaction