Beautiful iOS animation clock

Source: Internet
Author: User

Beautiful iOS animation clock
1. Final

2. The implementation idea is to rotate around the center point by default in ios, because the anchor is at the midpoint of the layer by default. to rotate around the center point below, you need to change the position of the anchor of the layer. Set the position coordinate based on the anchor, which is the midpoint of the clock. Think about the second-hand rotation angle, how to know where the current second-hand is rotated, the current second-hand rotation angle = the current second * How many degrees per second.
1> calculate the second-to-second conversion degree 360*60 = 6
2> get the current number of seconds. Use the Calendar Object to get the date composition NSCalendar-> NSDateComponents-> get the current number of seconds every second, get the latest number of seconds, and update the clock.
The same way in minutes, the clock also needs to be rotated every minute, 60 Minutes-> 1 hour-> 30 ° = "30/60 .0 minute Hour hour. complete code
# Import ViewController. h // obtain the current year, month, day, hour, minute, and second # define CURRENTSEC [[NSCalendar currentCalendar] component: NSCalendarUnitSecond fromDate: [NSDate date] # define CURRENTMIN [[NSCalendar currentCalendar] component: [NSDate date] # define CURRENTHOUR [[NSCalendar currentCalendar] component: NSCalendarUnitHour fromDate: [NSDate date] # define CURRENTDAY [[NSCalendar currentCalendar] component: nscalendarunitfromdate: [NSDate date] # define CURRENTMONTH [[NSCalendar currentCalendar] component: NSCalendarUnitMonth fromDate: [NSDate date] # define CURRENTYEAR [[NSCalendar currentCalendar] component: NSCalendarUnitYear fromDate: [NSDate date] // converts degrees to radians # define ANGEL (x) x/180.0 * M_PI # define kPerSecondA ANGEL (6) # define kPerMinuteA ANGEL (6) # define kPerHourA ANGEL (30) # define kPerHourMinuteA ANGEL (0.5) @ interface ViewController () @ property (nonatomic, strong) UIImageView * imageClock; @ property (nonatomic, strong) CALayer * layerSec; @ property (nonatomic, strong) CALayer * layerMin; @ property (nonatomic, strong) CALayer * layerHour; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self. view addSubview: self. imageClock]; [self. imageClock. layer addSublayer: self. layerSec]; [self. imageClock. layer addSublayer: self. layerMin]; [self. imageClock. layer addSublayer: self. layerHour]; [self timeChange]; [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @ selector (timeChange) userInfo: nil repeats: YES]; // Do any additional setup after loading the view, typically from a nib .} -(void) timeChange {self. layerSec. transform = CATransform3DMakeRotation (CURRENTSEC * kPerSecondA, 0, 0, 1); self. layerMin. transform = CATransform3DMakeRotation (CURRENTMIN * kPerMinuteA, 0, 0, 1); self. layerHour. transform = CATransform3DMakeRotation (CURRENTHOUR * kPerHourA, 0, 0, 1); self. layerHour. transform = CATransform3DMakeRotation (CURRENTMIN * kPerHourMinuteA + CURRENTHOUR * kPerHourA, 0, 0, 1);}-(UIImageView *) imageClock {if (_ imageClock = nil) {_ imageClock = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ clock]; _ imageClock. frame = CGRectMake (100,100,200,200);} return _ imageClock;}-(CALayer *) layerSec {if (_ layerSec = nil) {_ layerSec = [CALayer layer]; _ layerSec. bounds = CGRectMake (0, 0, 2, 80); _ layerSec. backgroundColor = [UIColor redColor]. CGColor; _ layerSec. cornerRadius = 5; _ layerSec. anchorPoint = CGPointMake (0.5, 1); _ layerSec. position = CGPointMake (self. imageClock. bounds. size. width/2, self. imageClock. bounds. size. height/2);} return _ layerSec;}-(CALayer *) layerMin {if (_ layerMin = nil) {_ layerMin = [CALayer layer]; _ layerMin. bounds = CGRectMake (0, 0, 4, 60); _ layerMin. backgroundColor = [UIColor blackColor]. CGColor; _ layerMin. cornerRadius = 5; _ layerMin. anchorPoint = CGPointMake (0.5, 1); _ layerMin. position = CGPointMake (self. imageClock. bounds. size. width/2, self. imageClock. bounds. size. height/2);} return _ layerMin;}-(CALayer *) layerHour {if (_ layerHour = nil) {_ layerHour = [CALayer layer]; _ layerHour. bounds = CGRectMake (0, 0, 6, 40); _ layerHour. backgroundColor = [UIColor blackColor]. CGColor; _ layerHour. cornerRadius = 5; _ layerHour. anchorPoint = CGPointMake (0.5, 1); _ layerHour. position = CGPointMake (self. imageClock. bounds. size. width/2, self. imageClock. bounds. size. height/2);} return _ layerHour ;}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.