为了加快按钮设置,我使用Grand Central dispatch如下.在vIEwDIDLoad中,我使用全局队列上的dispatch_async设置每个按钮图层(向基础图层添加形状和渐变图层),以便可以在不同的线程中呈现按钮.在块的末尾,CATextLayer被添加到渐变层.
dispatch_async(dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_HIGH,0),^{ CGRect base_bounds = CGRectMake(0,self.layer.bounds.size.wIDth,self.layer.bounds.size.height - self.layer.bounds.size.height * 0.10f); CGPoint move_point = CGPointMake(0.0f,base_bounds.size.height * 0.10f); self.layer.masksToBounds = NO; baseLayer = [CALayer layer]; baseLayer.cornerRadius = 10.0; baseLayer.shadowOffset = CGSizeMake(0.0f,2.0f); baseLayer.shadowOpacity = 1.5f; baseLayer.shadowcolor = [UIcolor blackcolor].CGcolor; baseLayer.shadowRadius = 2.5f; baseLayer.anchorPoint = CGPointMake(0.5f,0.5f); baseLayer.position = move_point; CAShapeLayer *shape = [CALayer layer]; shape.bounds = base_bounds; shape.cornerRadius = 10.0; shape.anchorPoint = CGPointMake(0.0f,0.0f); shape.position = move_point; shape.backgroundcolor = [UIcolor darkGraycolor].CGcolor; gradIEnt = [CAGradIEntLayer layer]; gradIEnt.anchorPoint = CGPointMake(0.0f,0.0f); gradIEnt.position = CGPointMake(0.0f,0.0f); gradIEnt.bounds = base_bounds; gradIEnt.cornerRadius = 10.0; gradIEnt.bordercolor = [UIcolor colorWithRed:0.72f green:0.72f blue:0.72f Alpha:1.0].CGcolor; gradIEnt.borderWIDth = 0.73; gradIEnt.colors = [NSArray arrayWithObjects: (ID)[UIcolor whitecolor].CGcolor,(ID)[UIcolor whitecolor].CGcolor,nil]; [baseLayer addSublayer:shape]; [baseLayer addSublayer:gradIEnt]; [self.layer addSublayer:baseLayer]; [textLayer setBounds:gradIEnt.bounds]; [textLayer setposition:CGPointMake(CGRectGetMIDX(textLayer.bounds),CGRectGetMaxY(textLayer.bounds) - 6)]; [textLayer setString:self.TitleLabel.text]; [textLayer setForegroundcolor:[UIcolor blackcolor].CGcolor]; [gradIEnt addSublayer:textLayer];});
这种方法将总时间减少到大约2-3秒.我想知道是否有人可以建议更快的方式来渲染按钮.请注意,我对丢弃图层使用的任何解决方案都不感兴趣.
先感谢您.
解决方法 也许我错过了这一点,但是你不会更好地覆盖UIbutton drawRect:方法并且在CoreGraphics(CG)中绘制的东西会被绘制得比秒快,你可以轻松地做渐变,文本,图像CG API.如果我理解正确,每个按钮有4层,同一视图中有30个按钮(120层)?如果是这样,我认为你不打算画这么多层(渲染/混合所有这些层将解释巨大的渲染时间).另一种可能性是为所有按钮设置4个大层. 总结以上是内存溢出为你收集整理的iphone – 使用图层和Grand Central Dispatch渲染UIButtons的最快方法?全部内容,希望文章能够帮你解决iphone – 使用图层和Grand Central Dispatch渲染UIButtons的最快方法?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)