Note that there are some explanatory texts on larger screens.

plurals
  1. POFastest way to render UIButtons using layers and Grand Central Dispatch?
    primarykey
    data
    text
    <p>I have a grid of 30 UIButtons and potentially even more, subclassed to be rendered using layers: a base <code>CALayer</code>, a <code>CAShapeLayer</code>, a <code>CAGradientLayer</code> and a <code>CATextLayer</code>. I am trying to minimize the overall time required to render/display the buttons when the corresponding xib file is loaded. If I simply setup each button in turn in <code>viewDidLoad</code>, the time required for the view to appear is about 5-6 seconds, which is clearly too much.</p> <p>In order to speed-up the buttons setup, I am using Grand Central Dispatch as follows. In <em>viewDidLoad</em>, I setup each button layers using <code>dispatch_async</code> on the global queue (adding to the base layer the shape and the gradient layers), so that the buttons can be rendered in a different thread. A the end of the block, the CATextLayer is added to the gradient layer.</p> <pre><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ CGRect base_bounds = CGRectMake(0, 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]; }); </code></pre> <p>This approach reduce the overall time to about 2-3 seconds. I am wondering if anyone can suggest a faster way to render the buttons. Please note that I am not interested to any solution which discards the use of layers.</p> <p>Thank you in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload