我使用self.bound来获得drawRect方法中的UIView大小。但是现在,在XCode 9中,我收到了这样的警告:
主线程检查器:在后台线程上调用的UI API:-UIView边界
在drawRect方法中获得视图大小的正确方法是什么?
发布于 2017-09-04 01:13:25
如下所示:
override func drawRect(rect: CGRect)
let bds = DispatchQueue.main.sync {
return self.bounds
}
// ....
}但是,drawRect最初是在后台线程上被调用的,这是一个不好的迹象--除非您使用的是CATiledLayer或其他内置架构。你首先应该担心的是这个。
发布于 2017-09-04 16:28:57
我终于找到了解决这个问题的办法。我现在正在重写layoutSubviews of UIView以保持类成员中的视图界限:
- (void)layoutSubviews
{
[super layoutSubviews];
m_SelfBounds = self.bounds;
}之后,我只在drawRect方法中使用drawRect。
发布于 2019-04-02 00:00:00
使用Grand Central Dispatch的“屏障”功能,允许并发读取操作,同时在写入过程中阻塞这些操作:
class MyTileView: UIView
{
var drawBounds = CGRect(x: 0, y: 0, width: 0, height: 0)
let drawBarrierQueue = DispatchQueue(label: "com.example.app",
qos: .userInteractive, // draw operations require the highest priority threading available
attributes: .concurrent,
target: nil)
override func layoutSubviews() {
drawBarrierQueue.sync(flags: .barrier) { // a barrier operation waits for active operations to finish before starting, and prevents other operations from starting until this one has finished
super.layoutSubviews();
self.drawBounds = self.bounds
// do other stuff that should hold up drawing
}
}
override func draw(_ layer: CALayer, in ctx: CGContext)
{
drawBarrierQueue.sync {
// do all of your drawing
ctx.setFillColor(red: 1.0, green: 0, blue: 0, alpha: 1.0)
ctx.fill(drawBounds)
}
}
}https://stackoverflow.com/questions/46028952
复制相似问题