Original blog, reproduced please indicate the source
Http://blog.csdn.net/hello_hwc?viewmode=list
Today, when I answered a question to a foreign friend on StackOverflow, I encountered the problem of automatic layout of layer. Write it down here and share it with the people who need it.
Does layer support AutoLayout?
iOS Calayer so far does not support AutoLayout and does not support Autoresizingmask.
As an example,
If you want to draw a gradient color, as the background color. Define a configuration function
-(void) Setupcagradientlayer: (Cagradientlayer *) gradient{Uicolor*colorone = [UicolorColorwithred: ( -/255.0) Green: (135/255.0) Blue: ( Max/255.0) Alpha:1.0];Uicolor*colortwo = [UicolorColorwithred: ( $/255.0) Green: ( -/255.0) Blue: ( the/255.0) Alpha:1.0];Nsarray*colors = [NsarrayArraywithobjects: (ID) Colorone. Cgcolor, Colortwo. Cgcolor,Nil];NSNumber*stopone = [NSNumberNumberwithfloat:0.0];NSNumber*stoptwo = [NSNumberNumberwithfloat:1.0];Nsarray*locations = [NsarrayArraywithobjects:stopone, Stoptwo,Nil]; Gradient. Colors= colors; Gradient. Locations= Locations;}
If you don't use a picture as a background, a direct way is to draw with a layer
Very simple to draw
#import "ViewController.h" @interface viewcontroller ()@property(Strong,nonatomic) Cagradientlayer * gradient;@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload]; Self. Gradient= [Cagradientlayer layer]; [ SelfSetupcagradientlayer: Self. Gradient]; Self. Gradient. Frame= Self. View. Frame; [ Self. View. LayerAddsublayer: Self. Gradient];}
But, after the horizontal screen, I found the wrong
Rough Solutions
The most intuitive solution is to resize the layer when the bounds of the view is changed.
The code is simple, because when the bounds of the view changes, the corresponding controller calls Viewdidlayoutsubviews
So, add the following code to the Viewcontroller above,
-(void)viewDidLayoutSubviews{ [super viewDidLayoutSubviews]; self.gradient.frameself.view.bounds;}
The horizontal screen is normal
But, as we all know, iOS automatically generates an animation when you switch between the screens and the screen, and if you use a rough solution, you just change the frame after the animation is over, and you can still see the white space during the animation.
A better solution
Because iOS has better support for view, it's easy to use auto layout or autoresizingmask. Therefore, a better solution is to use view to handle.
Bind the layer to the view
Define a view to set the class of his layer to Cagradientclass
#import <UIKit/UIKit.h>@interface BackgrundView : UIView@end
#import "BackgrundView.h"@interface BackgrundView()@end@implementation BackgrundView+(Class)layerClass{ return [CAGradientLayer class];}@end
Then, using this view, set the Autoresizingmask
#import "ViewController.h" #import "BackgrundView.h" @interface viewcontroller ()@propertyBackgrundview * Backgroundview;@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload]; Self. Backgroundview= [[Backgrundview alloc] initWithFrame: Self. View. Frame]; Self. Backgroundview. Autoresizingmask= Uiviewautoresizingflexibleheight | Uiviewautoresizingflexiblewidth; [ Self. ViewAddsubview: Self. Backgroundview]; [ SelfSetupcagradientlayer: (cagradientlayer*) Self. Backgroundview. Layer]; }@end
Then look at the toggle animation (generated gif a bit watermark, understanding)
Welcome to my ios-sdk detailed column
Http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html
I wrote it myself. A drop-down refresh Library
Https://github.com/wenchenhuang/WCPullRefreshControl
Auto layout for IOS layer