UIActivityIndicatorView and iosuiactivity of IOS Custom Controls
Original blog, reprinted, please indicate the source
Blog.csdn.net/hello_hwc
Preface
This series is not designed to allow readers to take the past and use it directly. After a while, I will upload some files on github for use. This series is intended to provide some ideas for custom controls.
Content of this article
- Describes the process of implementing custom UIActivityIndicator.
Hope that readers can learn this article
2. Implementation Process
_shapeLayer = [CAShapeLayer layer]; _shapeLayer.bounds = CGRectMake(0, 0, 100,100); _shapeLayer.position = CGPointMake(50,50); _shapeLayer.strokeColor = [UIColor blueColor].CGColor; _shapeLayer.fillColor = [UIColor clearColor].CGColor; CGMutablePathRef path = CGPathCreateMutable(); _shapeLayer.lineWidth = 5.0;// _shapeLayer.backgroundColor = [UIColor purpleColor].CGColor; CGPathAddArc(path, nil,50, 50,45,0,2*M_PI,YES); _shapeLayer.path = path;
2. Use CAGradientLayer to define the gradient, and use the preceding circular path to intercept the gradient.
_ IndicatorLayer = [[CAGradientLayer alloc] init]; _ indicatorLayer. bounds = CGRectMake (0, 0,100,100); _ indicatorLayer. position = CGPointMake (50, 50); _ indicatorLayer. colors = @ [(id) [UIColor blueColor]. CGColor, (id) [UIColor greenColor]. CGColor, (id) [UIColor blueColor]. CGColor]; _ indicatorLayer. locations = @ [@ (0.25), @ (0.5), @ (0.75)]; _ indicatorLayer. startPoint = CGPointMake (0.0, 0.0); _ indicatorLayer. endPoint = CGPointMake (1.0, 1.0); _ indicatorLayer. masksToBounds = YES; [_ indicatorLayer setMask: self. shapeLayer]; // capture
3. Start the animation in the Start function and stop the animation.
-(void)start{ self.hidden = NO; [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear animations:^{ self.transform = CGAffineTransformMakeRotation(M_PI); } completion:^(BOOL finished) { }];}-(void)stop{ [self.indicatorLayer removeAllAnimations]; self.hidden = YES;}
4. Use
- (IBAction)start:(id)sender { [self.spinner start];}- (IBAction)stop:(id)sender { [self.spinner stop];}- (void)viewDidLoad { [super viewDidLoad]; self.spinner = [[WCActivityIndicicator alloc] init]; self.spinner.bounds = CGRectMake(0, 0, 100,100); self.spinner.center = self.view.center; [self.view addSubview:self.spinner]; // Do any additional setup after loading the view, typically from a nib.}
Finally, we will attach the complete demo code. Do not use it directly. I am not perfect in many places and I will only use it for learning.
WCActivityIndicicator. h
/// WCActivityIndicicator. h // WCActivityindicator /// Created by huangwenchen on 15/2/17. // Copyright (c) 2015 huangwenchen. all rights reserved. // # import <UIKit/UIKit. h> # import <QuartzCore/QuartzCore. h> @ interface WCActivityIndicicator: UIView @ property (strong, nonatomic) UIColor * color;-(instancetype) init;-(void) start;-(void) stop; @ end
WCActivityIndicicator. m
/// WCActivityIndicicator. m // WCActivityindicator /// Created by huangwenchen on 15/2/17. // Copyright (c) 2015 huangwenchen. all rights reserved. // # import "WCActivityIndicicator. h "@ interface comment () @ property (strong, nonatomic) Comment * indicatorLayer; @ property (strong, nonatomic) CAShapeLayer * shapeLayer; @ end @ implementation comment-(CAShapeLayer *) shapeLayer {If (! _ ShapeLayer) {_ shapeLayer = [CAShapeLayer layer]; _ shapeLayer. bounds = CGRectMake (0, 0,100,100); _ shapeLayer. position = CGPointMake (50, 50); _ shapeLayer. strokeColor = [UIColor blueColor]. CGColor; _ shapeLayer. fillColor = [UIColor clearColor]. CGColor; CGMutablePathRef path = CGPathCreateMutable (); _ shapeLayer. lineWidth = 5.0; // _ shapeLayer. backgroundColor = [UIColor purpleColor]. CGColor; CGPathAddAr C (path, nil, 50, 50, 45, 0, 2 * M_PI, YES); _ shapeLayer. path = path; // roundShape. path} return _ shapeLayer;}-(CAGradientLayer *) indicatorLayer {if (! _ IndicatorLayer) {_ indicatorLayer = [[CAGradientLayer alloc] init]; _ indicatorLayer. bounds = CGRectMake (0, 0,100,100); _ indicatorLayer. position = CGPointMake (50, 50); _ indicatorLayer. colors = @ [(id) [UIColor blueColor]. CGColor, (id) [UIColor greenColor]. CGColor, (id) [UIColor blueColor]. CGColor]; // color line_indicatorlayer. locations = @ [@ (0.25), @ (0.5), @ (0.75)]; _ indicatorLayer. startPoint = CGPointMake (0.0, 0.0); _ indicatorLayer. endPoint = CGPointMake (1.0, 1.0); _ indicatorLayer. masksToBounds = YES; [_ indicatorLayer setMask: self. shapeLayer];} return _ indicatorLayer;}/* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. -(void) drawRect :( CGRect) rect {// Drawing code} */-(void) start {self. hidden = NO; [UIView animateWithDuration: 1.0 delay: 0.0 options: UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear animations: ^ {self. transform = CGAffineTransformMakeRotation (M_PI);} completion: ^ (BOOL finished) {}];}-(void) stop {[self. indicatorLayer removeAllAnimations]; self. hidden = YES;}-(void) setUp {self. bounds = CGRectMake (0, 0,100,100); [self. layer addSublayer: self. indicatorLayer]; self. hidden = YES;}-(instancetype) init {if (self = [super init]) {[self setUp];} return self ;}@ end
Viewcontroller used
//// ViewController. m // WCActivityindicator /// Created by huangwenchen on 15/2/17. // Copyright (c) 2015 huangwenchen. all rights reserved. // # import "ViewController. h "# import" WCActivityIndicicator. h "@ interface ViewController () @ property (strong, nonatomic) WCActivityIndicicator * spinner; @ end @ implementation ViewController-(IBAction) start :( id) sender {[self. spinner start];}-(IBAction) stop :( id) sender {[self. spinner stop];}-(void) viewDidLoad {[super viewDidLoad]; self. spinner = [[WCActivityIndicicator alloc] init]; self. spinner. bounds = CGRectMake (0, 0,100,100); self. spinner. center = self. view. center; [self. view addSubview: self. spinner]; // Do any additional setup after loading the view, typically from a nib .} @ end