iOS學習筆記-074.CALayer04——隱式動畫 – iPhone手機開發 iPhone軟體開發教學課程

CALayer04——隱式動畫

一、基本介紹

每一個UIView內部都默認關聯著一個CALayer,我們可用稱這個Layer為Root Layer(根層)

所有的非Root Layer,也就是手動創建的CALayer對象,都存在著隱式動畫

什麼是隱式動畫?
當對非Root Layer的部分屬性進行修改時,默認會自動產生一些動畫效果
而這些屬性稱為Animatable Properties(可動畫屬性)

列舉幾個常見的Animatable Properties:

bounds:用於設置CALayer的寬度和高度。修改這個屬性會產生縮放動畫

backgroundColor:用於設置CALayer的背景色。修改這個屬性會產生背景色的漸變動畫

position:用於設置CALayer的位置。修改這個屬性會產生平移動畫

可以通過動畫事務(CATransaction)關閉默認的隱式動畫效果

[CATransaction begin];
[CATransaction setDisableActions:YES];
//[CATransaction setAnimationDuration:5];
self.myview.layer.position = CGPointMake(10, 10);
[CATransaction commit];

二、時鐘案例

2.1 需要完成的效果圖

這裡寫圖片描述

2.2 代碼

//
//  ViewController.m
//  03_UIView67_時鐘
//
//  Created by 杞文明 on 17/6/11.
//  Copyright ? 2017年 杞文明. All rights reserved.
//

#import "ViewController.h"

//每一秒旋轉的度數
#define perSecA 6

//每一分旋轉的度數
#define perMinA 6

//每一小時旋轉的度數
#define perHourA 30

//每一分,時針旋轉的度數
#define perMinHour 0.5


#define angle2Rad(angle) ((angle) / 180.0 * M_PI)

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *clockView;

/** 當前的秒針 */
@property (nonatomic, weak)   CALayer *secL;
/** 當前的分針 */
@property (nonatomic, weak)   CALayer *minL;
/** 當前的針針 */
@property (nonatomic, weak)   CALayer *hourL;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initHourPointer];
    [self initMinPointer];
    [self initSecPointer];

    //創建定時器
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];

    [self timeChange];
}

//初始化時針
-(void)initHourPointer{
    _hourL = [self createPointer:CGSizeMake(4, 50) withColor:[UIColor blueColor]];
}

//初始化分針
-(void)initMinPointer{
    _minL = [self createPointer:CGSizeMake(3, 70) withColor:[UIColor blackColor]];
}

//初始化秒針
-(void)initSecPointer{
    _secL = [self createPointer:CGSizeMake(1, 80) withColor:[UIColor redColor]];
}

//創建指針
-(CALayer*)createPointer:(CGSize)size  withColor: (UIColor*) backColor{
    CALayer * pointer = [CALayer layer];
    pointer.bounds = CGRectMake(0, 0, size.width, size.height);
    pointer.backgroundColor = backColor.CGColor;
    pointer.anchorPoint = CGPointMake(0.5, 0.9);
    pointer.position = CGPointMake(_clockView.bounds.size.width*0.5, _clockView.bounds.size.height*0.5);
    [_clockView.layer addSublayer:pointer];
    return pointer;
}

//時間改變
-(void)timeChange{
    //獲取時間
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *cmp = [cal components:(NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond) fromDate:[NSDate date]];
    NSInteger curSec = cmp.second;
    NSInteger curMin = cmp.minute;
    NSInteger curHour = cmp.hour;

    NSLog(@"                    %ld : %ld : %ld",curHour,curMin,curSec);

    //計算秒針的位置
    CGFloat secA = curSec * perSecA;
    _secL.transform = CATransform3DMakeRotation(angle2Rad(secA), 0, 0, 1);

    //計算分鐘位置
    CGFloat minA = curMin * perMinA;
    _minL.transform = CATransform3DMakeRotation(angle2Rad(minA), 0, 0, 1);

    //計算時鐘位置
    CGFloat hourA = curHour * perHourA + curMin * perMinHour;
    _hourL.transform = CATransform3DMakeRotation(angle2Rad(hourA), 0, 0, 1);
}

@end

發佈留言