HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 使用图层和Grand Central Dispatch渲染UIButtons的最快方法?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个30个UIButtons的网格,可能甚至更多,使用图层渲染子类:基础CALayer,CAShapeLayer,CAGradientLayer和CATextLayer.我正在尝试最小化在加载相应的xib文件时渲染/显示按钮所需的总时间.如果我只是在viewDidLoad中依次设置每个按钮,则视图显示所需的时间约为5-6秒,这显然太多了.

为了加快按钮设置,我使用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 set@R_674_10495@ng: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的最快方法?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。