星空网 > 软件开发 > 操作系统

CALayer动画

废话少说,直接上代码。工程截图如下图所示。由于对程序进行了封装,所以在主控制器中,只需要给出该customview的frame即可,显示图形的半径等于给出frame的宽度的一半。

例如: CustomView *customView = [[CustomView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; 也就是在位置为(0, 0)处创建出一个半径为100/2=50的view。

CALayer动画images/loading.gif' data-original="http://images0.cnblogs.com/blog2015/687421/201507/250023381475204.png" />

       图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 模拟器运行结果截图

CALayer动画

 

CALayer动画




原标题:CALayer动画

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

2023年斋月营销策略概述:https://www.kjdsnews.com/a/1322118.html
2023年斋月倒计时,您准备好了吗?:https://www.kjdsnews.com/a/1322119.html
传统品牌转型难?亚马逊助中国品牌走向上亿美金的国际市场:https://www.kjdsnews.com/a/1322120.html
Ozon Global:90%以上滑雪装备订单来自中国:https://www.kjdsnews.com/a/1322121.html
新一轮“经济和金融灾难”,随时由美国引爆?!:https://www.kjdsnews.com/a/1322122.html
如何管理企业对vat的合规性-跨税云:https://www.kjdsnews.com/a/1322123.html
出境旅游预订须知:https://www.vstour.cn/a/365175.html
九寨沟景区地图(详细指南和攻略):https://www.vstour.cn/a/365176.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流