Four ways to set rounded corners for iOS

Source: Internet
Author: User

First, set the Cornerradius of Calayer

The Cornerradius property affects the background color of the layer display and the foreground box border, which does not work on the contents of the layer. Therefore, an image of Imgview (type Uiimageview) is not empty, and setting the Cornerradius of Imgview.layer is not visible because the image is the Imgview.layer part of contents.

In this case, the Maskstobounds property of the layer is set to Yes to draw the fillet effect correctly. However, Cornerradius>0,maskstobounds=yes will trigger off-screen rendering of the GPU, which can affect performance when multiple off-screen rendering is triggered on one screen. By ticking the Instruments->core animation->color offscreen-rendered Yellow, you can see that the on-screen rendering of the off-screen render will be rendered yellow. The cost of off-screen rendering is expensive, and Apple is aware of the performance issues, so the iOS9 system will not produce off-screen rendering anywhere in the future. For example, setting a fillet on a PNG image in Uiimageview does not trigger off-screen rendering.

    1. To set a fillet for a view that is empty contents

      25; //UILabel设置backgroundColor的行为被更改,不再是设定layer的背景色而是为contents设置背景色 label.layer.backgroundColor = aColor label.layer.cornerRadius = 5
    2. To set a fillet for a view that is not empty contents

       imageView.image = [UIImage imageNamed:@"img"]; imageView.image.layer.cornerRadius = 5; imageView.image.layer.masksToBounds = YES;
Second, set the mask of Calayer

By setting the Mask property of the View.layer, you can either cover the other layer on the view or set the fillet, but mask also triggers off-screen rendering.

There are two ways to generate a mask, one is generated by the picture, the transparency of the picture affects the transparency of the view drawing, the picture mask transparency is 1 of the portion of the view is drawn to the transparency of 0, whereas the picture mask transparency of 0 of the portion of the view is drawn to a transparency of 1. The second is generated by Bezier curves, and the shape part of the curve described in view is drawn.

// 通过图片生成遮罩,UIImage *maskImage = [UIImage imageNamed:@"someimg"];CALayer *mask = [CALayer new];mask.frame = CGRectMake(0, 0, maskImage.size.width, maskImage.size.height);mask.contents = (__bridge id _Nullable)(maskImage.CGImage);view.layer.mask = mask;//通过贝塞尔曲线生成CAShapeLayer *mask = [CAShapeLayer new];mask.path = [UIBezierPath bezierPathWithOvalInRect:view.bounds].CGPath;view.layer.mask = mask;
Third, redraw the view with rounded corners through the core graphics

Using the CPU to redraw a rounded view to achieve a rounded effect can greatly increase the burden on the CPU, and the equivalent of a copy of the view increases the memory overhead. However, as far as display performance is concerned, the high frame rate can be maintained because there is no off-screen rendering. The following example draws a circular picture, and there is no essential difference between drawing other uiview. The redraw process can be handled by a background thread.

@implementationUIImage (Circleimage)-(UIImage *) Drawcircleimage {CGFloat side = MIN (Self.size.width,Self.size.height);Uigraphicsbeginimagecontextwithoptions (Cgsizemake (side, side),False, [UIScreen Mainscreen].scale);Cgcontextaddpath (Uigraphicsgetcurrentcontext (), [Uibezierpath Bezierpathwithovalinrect:CGRectMake (0,0, side, side)]. Cgpath);Cgcontextclip (Uigraphicsgetcurrentcontext ());CGFloat Marginx =-(Self.size.width-side)/2.F;CGFloat Marginy =-(Self.size.height-side)/2.F; [Self Drawinrect:CGRectMake (Marginx, Marginy,self.size.width, self.size.height)]; cgcontextdrawpath (uigraphicsgetcurrentcontext (), Kcgpathfillstroke); uiimage *output =  Uigraphicsgetimagefromcurrentimagecontext (); uigraphicsendimagecontext (); return output;}  @end //call the following dispatch_ Async (Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^{uiimage *img = [[uiimage imagenamed:@" Image.png "] drawcircleimage]; dispatch_async (Dispatch_get_main_queue (), ^{view.image = img;});    
Iv. by blending layers

This method is to overlay a partially transparent view on the view where you want to add a fillet, only to obscure the fillet portion. The transparency of layer blending is handled in the opposite way as mask. Although this method is the optimal solution, there is no off-screen rendering, no additional CPU calculation, but the application scope is limited.

Summarize
    1. In scenarios where you can use a blend layer occlusion, use the fourth method first.
    2. Even non-iOS9 system, the first method in the comprehensive performance is still stronger than the latter two, more than iOS9 because there is no off-screen rendering is preferred.
    3. Method Two and method three because of the use of Bezier curves, can be used to deal with complex fillets. But the former sacrifice frame rate, the latter need a lot of computation and increase some memory, need the actual situation of their own choice.

OBJECTIVE-C implementation of the above four methods

Four ways to set rounded corners for iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.