IOS第18天(4,核心動畫,時鐘效果,定時器,圖片旋轉角度,CALayer 錨點,擷取當前,小時,秒,分)

來源:互聯網
上載者:User

標籤:

****

#import "HMViewController.h"// 每秒秒針轉6度#define perSecendA 6// 每分鐘分針轉6度#define perMinuteA 6// 每小時時針轉6度#define perHourA 30// 每分鐘時針轉6度#define perMinuteHourA 0.5#define angle2radian(x) ((x) / 180.0 * M_PI)@interface HMViewController (){    CALayer *_second;    CALayer *_minute;    CALayer *_hour;}@property (weak, nonatomic) IBOutlet UIView *redView;@property (weak, nonatomic) IBOutlet UIImageView *clockView;@end@implementation HMViewController- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    // 1.添加秒針    [self addSecond];        // 2.添加分針    [self addMintue];        // 3.添加時針    [self addHour];           // 建立定時器    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(update) userInfo:nil repeats:YES];            [self update];}- (void)addHour{    CGFloat imageW = _clockView.bounds.size.width;    CGFloat imageH = _clockView.bounds.size.height;        // 1.添加時針    CALayer *hour = [CALayer layer];        // 2.設定錨點    hour.anchorPoint = CGPointMake(0.5, 1);        // 3.設定位置    hour.position = CGPointMake(imageW * 0.5, imageH * 0.5);        // 4.設定尺寸    hour.bounds = CGRectMake(0, 0, 4, imageH * 0.5 - 50);        // 5.紅色    hour.backgroundColor = [UIColor blackColor].CGColor;        hour.cornerRadius = 4;        // 添加到圖層上    [_clockView.layer addSublayer:hour];        _hour = hour;}// 添加分針- (void)addMintue{    CGFloat imageW = _clockView.bounds.size.width;    CGFloat imageH = _clockView.bounds.size.height;        // 1.添加分針    CALayer *minute = [CALayer layer];        // 2.設定錨點    minute.anchorPoint = CGPointMake(0.5, 1);        // 3.設定位置    minute.position = CGPointMake(imageW * 0.5, imageH * 0.5);        // 4.設定尺寸    minute.bounds = CGRectMake(0, 0, 2, imageH * 0.5 - 30);        // 5.紅色    minute.backgroundColor = [UIColor blueColor].CGColor;        // 添加到圖層上    [_clockView.layer addSublayer:minute];        _minute = minute;}// 添加秒針- (void)addSecond{    CGFloat imageW = _clockView.bounds.size.width;    CGFloat imageH = _clockView.bounds.size.height;        // 1.添加秒針    CALayer *second = [CALayer layer];        // 2.設定錨點    second.anchorPoint = CGPointMake(0.5, 1);        // 3.設定位置    second.position = CGPointMake(imageW * 0.5, imageH * 0.5);        // 4.設定尺寸    second.bounds = CGRectMake(0, 0, 1, imageH * 0.5 - 20);        // 5.紅色    second.backgroundColor = [UIColor redColor].CGColor;        // 添加到圖層上    [_clockView.layer addSublayer:second];        _second = second;}- (void)update{    // 擷取秒數    // 擷取日曆對象    NSCalendar *calendar = [NSCalendar currentCalendar];        // 擷取日期組件    NSDateComponents *compoents = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];        // 擷取秒數    CGFloat sec = compoents.second;        // 擷取分鐘    CGFloat minute = compoents.minute;        // 擷取小時    CGFloat hour = compoents.hour;        // 算出秒針旋轉多少°    CGFloat secondA = sec * perSecendA;        // 算出分針旋轉多少°    CGFloat minuteA = minute * perMinuteA;        // 算出時針旋轉多少°    CGFloat hourA = hour * perHourA;    hourA += minute * perMinuteHourA;        // 旋轉秒針    _second.transform = CATransform3DMakeRotation(angle2radian(secondA), 0, 0, 1);        // 旋轉分針    _minute.transform = CATransform3DMakeRotation(angle2radian(minuteA), 0, 0, 1);        // 旋轉時針    _hour.transform = CATransform3DMakeRotation(angle2radian(hourA), 0, 0, 1);}@end

 

IOS第18天(4,核心動畫,時鐘效果,定時器,圖片旋轉角度,CALayer 錨點,擷取當前,小時,秒,分)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.