iOS animations, nice clocks

Source: Internet
Author: User

1. Ultimately

2. Realization of Ideas
    1. The default is to rotate around the center point in iOS, because the anchor point defaults to the midpoint of the layer, and you need to change the position of the layer's anchor point to go around the center point of the bottom.
    2. Sets the position coordinate according to the anchor point, which is the midpoint of the clock.
    3. Think about the angle of the second hand rotation, how to know the current second hand rotation, the current second rotation angle = The current number of seconds * How many degrees per second.
      1> calculate the number of seconds to go °360 * 60 = 6
      2> gets the current number of seconds, through the Calendar object, gets the date component Nscalendar, nsdatecomponents, gets the current number of seconds
    4. Every second, get the latest number of seconds, update the clock.
      • Minutes the same way
      • The clock is the same
    5. Every minute, the clock also needs to rotate, 60 minutes, 1 hours-> 30°== "Every minute 30/60.0 a minute turn 0.5°
    6. Tip the end of the hour and second needles to set the fillet radius
2. Complete code
#import"ViewController.h"Get the current month and day time of year # define Currentsec[[Nscalendar Currentcalendar] component:nscalendarunitsecond fromdate:[nsdate Date]]#define Currentmin[[Nscalendar Currentcalendar] component:nscalendarunitminute fromdate:[nsdate Date]]#define Currenthour[[Nscalendar Currentcalendar] component:nscalendarunithour fromdate:[nsdate Date]]#define Currentday[[Nscalendar Currentcalendar] component:nscalendarunitday fromdate:[nsdate Date]]#define Currentmonth[[Nscalendar Currentcalendar] component:nscalendarunitmonth fromdate:[nsdate Date]]#define CurrentYear[[Nscalendar Currentcalendar] component:nscalendarunityear fromdate:[nsdate Date]]Angle converted to radians # ANGEL (x) x/180.0* M_pi#define Kperseconda ANGEL (6) #define Kperminutea ANGEL (6) #define Kperhoura ANGEL ( -) #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:1Target:self selector: @selector (timechange) UserInfo:NilRepeats: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 ( -, -, $, $); }return_imageclock;} -(Calayer *) layersec{if(_layersec = =Nil{_layersec = [calayer layer]; _layersec.bounds = CGRectMake (0,0,2, the); _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, -); _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, +); _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;}
3.Demo Program

https://github.com/esdeath/0716–

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS animations, nice clocks

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.