Examples of Several Methods of Setting Fillet in iOS

  • 2021-11-29 08:40:24
  • OfStack

Preface

Rounded corners (RounderCorner) are a very common view effect, which is softer and more beautiful and easy to accept than right angles. However, many people are not clear about the correct way and principle of setting rounded corners. Setting rounded corners will bring about a certain performance loss, and how to improve performance is another topic that needs to be discussed emphatically. I looked up some existing information, and gained a lot, but also found some misleading mistakes.

1. Use the layer property


layer.backgroundColor = [UIColor cyanColor].CGColor; //  Add a background color to the layer  
layer.contents = (id)[UIImage imageNamed:@"view_BG.png"].CGImage; //  Add a background image to the layer  
layer.cornerRadius = 8; //  Set the border of the layer to round foot  
layer.masksToBounds = YES; //  Hide boundary  
layer.borderWidth = 5; //  Add to the layer 1 Colored border  
layer.borderColor = [UIColor colorWithRed:0.52 green:0.09 blue:0.07 alpha:1].CGColor; 
layer.shadowOffset = CGSizeMake(0, 3); //  Set the offset of shadows  
layer.shadowRadius = 10.0; //  Set the radius of the shadow  
layer.shadowColor = [UIColor blackColor].CGColor; //  Set the color of the shadow to black  
layer.shadowOpacity = 0.9; //  Set the opacity of shadows 

2. Set fillets using drawings


/**  Set a circular picture ( Put it into the classification for use ) */
- (UIImage *)cutCircleImage { 
  UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0); //  Get context 
  CGContextRef ctr = UIGraphicsGetCurrentContext(); //  Set a circle  
  CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height); 
  CGContextAddEllipseInRect(ctr, rect); //  Cutting  
  CGContextClip(ctr); //  Draw a picture on it  
  [self drawInRect:rect]; 
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
  UIGraphicsEndImageContext(); 
  return image;
}

3. Create a new diagram from another mask diagram

First, we need an mask graph, and then combine this mask graph with the original graph to get a new graph with rounded corners. The efficiency is similar to that of method 1, and synthesizing new maps is equivalent to plotting in off-screen. The advantage of this method is that it can be controlled by mask diagram instead of fillet.

Summary

If efficiency is desired (for example, to increase the number of scrolling frames of table and view), method 2 is used more often. To be convenient, nature is method 1. If the required special shape UIBezierPath object cannot be formed, consider method 3.

Summarize


Related articles: