Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best and fastest way to round UIView's corners?

I'm currently working on a rewrite of a tweak (runs inside SpringBoard itself) and so (of cause) smoothness, speed and efficiency are some of the most important things for me as even the slightest bit of unnecessary lag will take away from the UX.

So my question is how can I round the views corners with the minimal amount of lag.

The obvious one is:

view.layer.cornerRadius = value;
view.layer.masksToBounds = YES;

However I've heard that setting a CALayer mask with layer.mask is faster? And if so which of these 2 solutions is best?: https://stackoverflow.com/a/4930166/458205

This code uses masking however the mask layer is using cornerRadius as well so is this actually any faster?

CALayer *maskLayer = [CALayer layer];
maskLayer.cornerRadius = radius;
// set the mask
self.view.layer.mask = maskLayer;

Or would Solution 1, of the above link, or this answer be more effecient?

I know I'm referencing another question several times, however that question is about masking only 2 corners (which throws up a few different solutions) however I'm asking for the most efficient way to have 0.6 screen size views scroll as smoothly as possible with rounded corners (like this image). CardSwitcher Screenshot

like image 363
Kyle Howells Avatar asked Dec 26 '11 01:12

Kyle Howells


People also ask

How do you round UIView corners?

If you start with a regular UIView it has square corners. You can give it round corners by changing the cornerRadius property of the view's layer . and smaller values give less rounded corners. Both clipsToBounds and masksToBounds are equivalent.

How do you round the top corner of UIView in Swift?

You can set the cornerRadius property of any UIView to have its edges rounded, but by default that rounds all corners at the same time.

How do you set corner radius for UIView in storyboard?

Select the view that you want to round and open its Identity Inspector. In the User Defined Runtime Attributes section, add the following two entries: Key Path: layer. cornerRadius , Type: Number, Value: (whatever radius you want)


2 Answers

You need to set the rasterizationScale as well as view.layer.shouldRasterize.

I typically do this:

view.layer.shouldRasterize = YES;
view.layer.rasterizationScale = [[UIScreen mainScreen] scale];

If you don't set the rasterizationScale it will always default to a scale of 1 (non-retina in this case) for rasterization.

For me this usually puts my scroll performance on par with the UITableView's.

like image 197
James Jones Avatar answered Sep 22 '22 21:09

James Jones


The first snippet is more efficient because the rounded corner is a mask by itself. to increase performance of rounded corners use: view.layer.shoudRasterize = YES;

like image 32
Hamidreza Vakilian Avatar answered Sep 19 '22 21:09

Hamidreza Vakilian