生活随笔
收集整理的這篇文章主要介紹了
IOS开发基础之时钟的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS開發基礎之時鐘的實現
源碼在我的主頁資源下。
#import "ViewController.h"@interface ViewController
()@property(nonatomic
,weak
)CALayer
*second
;@end@implementation ViewController
- (void)viewDidLoad
{[super viewDidLoad
];CALayer
*clock
= [[CALayer alloc
] init
];clock
.bounds
= CGRectMake(0, 0, 200, 200);clock
.position
= CGPointMake(160, 200);clock
.contents
=(__bridge id
)([UIImage imageNamed
:@"appleWatcher"].CGImage
);clock
.cornerRadius
= 100;clock
.masksToBounds
=YES
;CALayer
*second
= [[CALayer alloc
] init
];second
.bounds
= CGRectMake(0, 0, 2, 100);second
.position
= clock
.position
;second
.backgroundColor
= [UIColor redColor
].CGColor
;second
.anchorPoint
= CGPointMake(0.5, 0.8);[self.view
.layer addSublayer
:clock
];[self.view
.layer addSublayer
:second
];self.second
= second
;CADisplayLink
*link
= [CADisplayLink displayLinkWithTarget
:self selector
:@selector(timeChange
)];[link addToRunLoop
:[NSRunLoop mainRunLoop
] forMode
:NSDefaultRunLoopMode
];[self timeChange
];
}-(void)timeChange
{CGFloat angle
= 2*M_PI
/60;NSDate
*date
= [NSDate date
];NSCalendar
*cal
= [NSCalendar currentCalendar
];CGFloat time2
= [cal component
:NSCalendarUnitSecond fromDate
:date
];self.second
.affineTransform
= CGAffineTransformMakeRotation(time2
*angle
);
}@end
總結
以上是生活随笔為你收集整理的IOS开发基础之时钟的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。