轉圈圈定製加載中視圖
// // 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
最後更新:2017-04-03 05:39:57