CALayer動畫---使用CAShapeLayer製作類似小視頻按鈕動畫,
廢話少說,直接上代碼。工程如所示。由於對程式進行了封裝,所以在主控制器中,只需要給出該customview的frame即可,顯示圖形的半徑等於給出frame的寬度的一半。
例如: CustomView *customView = [[CustomView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; 也就是在位置為(0, 0)處建立出一個半徑為100/2=50的view。
圖1 工程
1 CustomView.m檔案中實現的代碼
@implementation CustomView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
//重新設定view的大小 設定為以width為邊長的正方形地區
self.frame = CGRectMake(frame.origin.x
, frame.origin.y, frame.size.width, frame.size.width);
self.layer.cornerRadius = self.frame.size.width/2;
self.layer.masksToBounds = YES;
//初始角度
angle = 0;
//添加按鍵
_button = [UIButton buttonWithType:UIButtonTypeSystem];
_button.frame = CGRectMake(0, 0, self.frame.size.width, frame.size.width);
_button.backgroundColor = [UIColor greenColor];
[self addSubview:_button];
//添加顯示文字的lable
_lable = [[UILabel alloc]initWithFrame:_button.frame];
//文字置中
_lable.textAlignment = NSTextAlignmentCenter;
_lable.numberOfLines = 0;
_lable.lineBreakMode = NSLineBreakByWordWrapping;
_lable.text = @"按住拍";
[self addSubview:_lable];
//添加按鍵不同事件執行的方法
[_button addTarget:self action:@selector(cameraButtonTouchDown:) forControlEvents:UIControlEventTouchDown];
[_button addTarget:self action:@selector(cameraButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//繪圖
[self initLayout];
//定時器設定
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(isButtonClicked) userInfo:nil repeats:YES];
//暫停定時器
[_timer setFireDate:[NSDate distantFuture]];
}
return self;
}
- (void)initLayout{
_shapeLayer = [CAShapeLayer layer];
_shapeLayer.strokeColor = [UIColor redColor].CGColor;
_shapeLayer.fillColor = [UIColor clearColor].CGColor;
_shapeLayer.frame = _button.frame;
_shapeLayer.lineWidth = 5.0f;
[self.layer addSublayer:_shapeLayer];
_bPath = [[UIBezierPath alloc]init];
//畫圓弧
[_bPath addArcWithCenter:_button.center radius:_button.frame.size.width/2 startAngle:0 endAngle:angle*M_PI/180 clockwise:YES];
_shapeLayer.path = _bPath.CGPath;
}
#pragma mark 按鍵被觸摸按下 立刻執行方法內的程式
- (void)cameraButtonTouchDown:(UIButton *)sender{
[_timer setFireDate:[NSDate distantPast]];
isClicked = YES;
}
#pragma mark 按鍵按下後執行的方法 注意:isClicked = NO 只有在按鍵彈起後才會生效
- (void)cameraButtonClicked:(UIButton *)sender{
isClicked = NO;
}
#pragma mark 定時器方法 重繪角度等
- (void)isButtonClicked{
[_shapeLayer removeFromSuperlayer];
[_bPath removeAllPoints];
if (isClicked) {
if (angle < 360) {
angle = angle + 5;
//重繪
[self initLayout];
_lable.text = [NSString stringWithFormat:@"已載入%%%ld",angle*100/360];
}else{
//關閉定時器
[_timer setFireDate:[NSDate distantFuture]];
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"拍攝完成" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles: @"確定", nil];
[alertView show];
//回到初始狀態
angle = 0;
_lable.text = @"按住拍";
[self initLayout];
}
}else{
//關閉定時器
[_timer setFireDate:[NSDate distantFuture]];
//回到初始狀態
angle = 0;
_lable.text = @"按住拍";
[self initLayout];
}
}
@end
2 ViewController.m檔案中實現的代碼
#import "ViewController.h"
#import "CustomView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CustomView *customView = [[CustomView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
customView.center = self.view.center;
[self.view addSubview:customView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
3 模擬器運行結果