IOS CALayer (1), ioscalayer
For the quality of an app, the first thing we should judge is the app interface, and the creation of the interface is based on the graphic processing. When it comes to graphic processing, we have to mentionQuartz2D, CALayer.
In iOS systems, what you can see is basically a UIView, such as a button, a text tag, a text input box, and an icon. These are all uiviews.
In fact, the reason why UIView can be displayed on the screen is because it has an internal layer.
When a UIView object is created, a layer (CALayer object) is automatically created in the UIView, which can be accessed through the layer attribute of the UIView. When UIView needs to be displayed on the screen, it will call the drawRect: Method for drawing, and all content will be drawn on its own layer. After the drawing is complete, the system copies the layer to the screen and displays the UIView.
In other words, UIView itself does not have the display function. It only has the display function at its internal layer.
First, we will show the imageView without any operations.
/// ViewController. m // CX-CALayer (1) /// Created by ma c on 16/3/19. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (50, 50, self. view. frame. size. width-100,400)]; imageView. image = [UIImage imageNamed: @ "nvshen.jpg"]; [self. view addSubview: imageView];} @ end
Set the shadow effect
/// ViewController. m // CX-CALayer (1) /// Created by ma c on 16/3/19. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (50, 50, self. view. frame. size. width-100,400)]; imageView. image = [UIImage imageNamed: @ "nvshen.jpg"]; // sets the shadow color imageView. layer. shadowColor = [UIColor orang EColor]. CGColor; // sets the shadow offset imageView. layer. shadowOffset = CGSizeMake (5, 5); // sets the shadow transparency. 1 indicates opacity. ImageView. layer. shadowOpacity = 0.5; [self. view addSubview: imageView];} @ end
Set the effect of rounded corners
/// ViewController. m // CX-CALayer (1) /// Created by ma c on 16/3/19. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (50, 50, self. view. frame. size. width-100,400)]; imageView. image = [UIImage imageNamed: @ "nvshen.jpg"]; // set the shadow color // imageView. layer. shadowColor = [UIColor o RangeColor]. CGColor; // set the shadow offset // imageView. layer. shadowOffset = CGSizeMake (5, 5); // sets the shadow transparency. 1 indicates opacity. // ImageView. layer. shadowOpacity = 0.5; // set the radius of the rounded corner to imageView. layer. cornerRadius = 10; // enable the view to support rounded imageView. layer. masksToBounds = YES; // set masksToBounds to YES. [Self. view addSubview: imageView] ;}@ end
Set the border effect
/// ViewController. m // CX-CALayer (1) /// Created by ma c on 16/3/19. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (50, 50, self. view. frame. size. width-100,400)]; imageView. image = [UIImage imageNamed: @ "nvshen.jpg"]; // set the shadow color // imageView. layer. shadowColor = [UIColor o RangeColor]. CGColor; // set the shadow offset // imageView. layer. shadowOffset = CGSizeMake (5, 5); // sets the shadow transparency. 1 indicates opacity. // ImageView. layer. shadowOpacity = 0.5; // set the radius of the rounded corner. // imageView. layer. cornerRadius = 10; // enable the view to support rounded corners // imageView. layer. masksToBounds = YES; // set masksToBounds to YES. // Set the Border width to imageView. layer. borderWidth = 5; // set the border color to imageView. layer. borderColor = [UIColor orangeColor]. CGColor; [self. view addSubview: imageView];} @ end
Set the rotation effect
/// ViewController. m // CX-CALayer (1) /// Created by ma c on 16/3/19. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (50, 50, self. view. frame. size. width-100,400)]; imageView. image = [UIImage imageNamed: @ "nvshen.jpg"]; // set the shadow color // imageView. layer. shadowColor = [UIColor o RangeColor]. CGColor; // set the shadow offset // imageView. layer. shadowOffset = CGSizeMake (5, 5); // sets the shadow transparency. 1 indicates opacity. // ImageView. layer. shadowOpacity = 0.5; // set the radius of the rounded corner. // imageView. layer. cornerRadius = 10; // enable the view to support rounded corners // imageView. layer. masksToBounds = YES; // set masksToBounds to YES. //// Set the Border Width // imageView. layer. borderWidth = 5; // set the border color // imageView. layer. borderColor = [UIColor orangeColor]. CGColor; // you can specify the Rotation Angle and rotation axis x y z imageView. layer. transform = CATransform3DMakeRotation (M_PI_4, 0, 0, 1); [self. view addSubview: imageView];} @ end