// // HYCircleLoadingView.h // HYCircleLoadingViewExample // // Created by Shadow on 14-3-7. // Copyright (c) 2014年 Shadow. All rights reserved. // #import <UIKit/UIKit.h> /*! * @brief 圆形转圈圈加载等待视图 * @author huangyibiao */ @interface HYBCircleLoadingView : UIView // 线宽 // default is 1.0f @property (nonatomic, assign) CGFloat lineWidth; // 线的颜色 // default is [UIColor lightGrayColor] @property (nonatomic, strong) UIColor *lineColor; // 是否添加动画 // default is YES @property (nonatomic, readonly) BOOL isAnimating; // 开始、结束动画效果 - (void)startAnimation; // 结束动画的时候会移除掉 - (void)stopAnimation; @end
// // HYCircleLoadingView.m // HYCircleLoadingViewExample // // Created by Shadow on 14-3-7. // Copyright (c) 2014年 Shadow. All rights reserved. // #import "HYBCircleLoadingView.h" #define kAngelWithDegree(degree) (2 * M_PI / 360 * degree) @interface HYBCircleLoadingView () // 角度 // 0.0 - 1.0 @property (nonatomic, assign) CGFloat angel; @end @implementation HYBCircleLoadingView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; } return self; } - (id)init { return [self initWithFrame:CGRectZero]; } - (void)setAngel:(CGFloat)angel { _angel = angel; [self setNeedsDisplay]; return; } - (void)startAnimation { if (self.isAnimating) { [self stopAnimation]; [self.layer removeAllAnimations]; } _isAnimating = YES; self.angel = kAngelWithDegree(30); [self startRotateAnimation]; return; } - (void)stopAnimation { _isAnimating = NO; [self stopRotateAnimation]; return; } - (void)startRotateAnimation { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.fromValue = @(0); animation.toValue = @(2 * M_PI); animation.duration = 0.9f; animation.repeatCount = INT_MAX; [self.layer addAnimation:animation forKey:@"keyFrameAnimation"]; [UIView animateWithDuration:0.3f animations:^{ self.alpha = 1.0; } completion:nil]; return; } - (void)stopRotateAnimation { [UIView animateWithDuration:0.3f animations:^{ self.alpha = 0; } completion:^(BOOL finished) { _angel = 0; [self.layer removeAllAnimations]; }]; return; } - (void)drawRect:(CGRect)rect { if (self.angel <= 0) { _angel = 0; } CGFloat lineWidth = 1.f; UIColor *lineColor = [UIColor blueColor]; if (self.lineWidth) { lineWidth = self.lineWidth; } if (self.lineColor) { lineColor = self.lineColor; } CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, lineWidth); CGContextSetStrokeColorWithColor(context, lineColor.CGColor); CGContextAddArc(context, CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds), CGRectGetWidth(self.bounds)/2-lineWidth, kAngelWithDegree(120), kAngelWithDegree(120) + kAngelWithDegree(330) * self.angel, 0); CGContextStrokePath(context); return; } @end
时间: 2024-10-02 23:25:32