国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

CALayer動畫---使用CAShapeLayer制作類似微信小視頻按鈕動畫

2019-11-14 18:38:25
字體:
來源:轉載
供稿:網友

廢話少說,直接上代碼。工程截圖如下圖所示。由于對程序進行了封裝,所以在主控制器中,只需要給出該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 模擬器運行結果截圖

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西乌| 建水县| 阆中市| 额敏县| 阿瓦提县| 新安县| 个旧市| 惠安县| 镇雄县| 宝清县| 读书| 六安市| 修武县| 华容县| 罗源县| 巴林左旗| 泽州县| 黑水县| 惠安县| 灌阳县| 冕宁县| 沂水县| 景宁| 泗阳县| 靖西县| 焦作市| 分宜县| 岳阳县| 象山县| 中超| 东明县| 和顺县| 柞水县| 遵义县| 自治县| 奉节县| 富民县| 沙湾县| 敦煌市| 定襄县| 留坝县|