iOS : 还有其他方法可以将图像放在 UITableView 上,UITableView 可以正常工作吗?
iOS : Any Other way to put an image over a UITableView, the UITableView works normally?
iOS : 目标是将图像放在 UITableView 上,UITableView 正常工作。
一般来说,在UITableView上放一张图片,UITableView的触摸事件是
切断。
Here is the scene, there is a piano score app. The music notes line by line is shown by a UITableView.
Then user could put a canvas over the notes list, they could draw over the score.
I need to put the custom drawing over the piano score UITableView, and the UITableView works normally.
这是我的想法:
将绘图视图放在单元格下方的 tableView 上。
每个单元格都有一个绘图视图的引用。
每个单元格的顶部还有一个 canvas 切片视图。
关键方法是CALayer
的open func render(in ctx: CGContext)
相关代码如下:
class RenderV: UIView {
var renderer: UIView?
var index = -1
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext()
else { return }
var f: CGSize = .zero
if let v = renderer{
f = v.frame.size
}
let w = frame.size.width
let h = frame.size.height
context.translateBy(x: w/2 - f.width/2, y: h * CGFloat(index) - f.height)
renderer?.layer.render(in: context)
}
}
canvas 用于 UITableViewCell:
class TableViewCell: UITableViewCell {
var renderer: UIView?{
didSet{
guard idx > 0 else {
return
}
contain.index = idx
contain.renderer = renderer
contain.setNeedsDisplay()
}
}
@IBOutlet weak var contain: RenderV!
var idx = -1
}
调用代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cel = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
cel.idx = 6 - indexPath.row
cel.renderer = img
return cel
}
我的解决方案不直观,需要计算。
有更好的主意吗?
这是一个简单的演示效果:
我不确定我是否完全理解问题所在。我的理解是,您在 UITableView
上呈现的视图阻止了您想要传递给 table 的触摸事件,对吧?
如果是这样,只需设置视图的 isUserInteractionEnabled = false
。这将使事件能够穿透视图并被 UITableView
.
捕获
iOS : 目标是将图像放在 UITableView 上,UITableView 正常工作。
一般来说,在UITableView上放一张图片,UITableView的触摸事件是 切断。
Here is the scene, there is a piano score app. The music notes line by line is shown by a UITableView.
Then user could put a canvas over the notes list, they could draw over the score.
I need to put the custom drawing over the piano score UITableView, and the UITableView works normally.
这是我的想法:
将绘图视图放在单元格下方的 tableView 上。
每个单元格都有一个绘图视图的引用。
每个单元格的顶部还有一个 canvas 切片视图。
关键方法是CALayer
的open func render(in ctx: CGContext)
相关代码如下:
class RenderV: UIView {
var renderer: UIView?
var index = -1
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext()
else { return }
var f: CGSize = .zero
if let v = renderer{
f = v.frame.size
}
let w = frame.size.width
let h = frame.size.height
context.translateBy(x: w/2 - f.width/2, y: h * CGFloat(index) - f.height)
renderer?.layer.render(in: context)
}
}
canvas 用于 UITableViewCell:
class TableViewCell: UITableViewCell {
var renderer: UIView?{
didSet{
guard idx > 0 else {
return
}
contain.index = idx
contain.renderer = renderer
contain.setNeedsDisplay()
}
}
@IBOutlet weak var contain: RenderV!
var idx = -1
}
调用代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cel = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
cel.idx = 6 - indexPath.row
cel.renderer = img
return cel
}
我的解决方案不直观,需要计算。
有更好的主意吗?
这是一个简单的演示效果:
我不确定我是否完全理解问题所在。我的理解是,您在 UITableView
上呈现的视图阻止了您想要传递给 table 的触摸事件,对吧?
如果是这样,只需设置视图的 isUserInteractionEnabled = false
。这将使事件能够穿透视图并被 UITableView
.