IOS clock development case sharing _ios

Source: Internet
Author: User
Tags current time

The example of this article introduces the iOS clock development process, for your reference, the specific contents are as follows

The idea is to use calayer implicit animation to achieve. Because the UIView layer, which is a manually created layer, is animated by default when its properties are changed, these properties are also called animated properties. such as bounds, backgroundcolor, position.

The clock inside the dial is a uiview, and the three needles are three manually created layer.

First get a uiimageview on the storyboard, set the picture of the dial

Then initialize the three pins in the viewdidload, set the timer, get the current time, and point to the corresponding angle of the hand hand hand hand for the current time.

VIEWCONTROLLER.M//clock effect////Created by Daniel on 16/4/7. copyright©2016 years Daniel.
All rights reserved.
 
#define KCLOCKWH _clockview.bounds.size.width//seconds second hand turn how many degrees #define Preseconda 6//A minute the minute hand turns how many degrees #define PREMINUTEA 6 How much is the hour clockwise #define Prehoura 30//minute clockwise turn how many degrees #define Prehourminute 0.5//second minutes of the minute turn #define Preminutesecond 0.1 #defi Ne Angle2raditon (a) ((a)/180.0 * m_pi) #import "ViewController.h" @interface Viewcontroller () @property (weak, Nona
 
Tomic) Iboutlet Uiimageview *clockview;
 
/** Second Hand * * @property (nonatomic, strong) Calayer *secondl;
 
/** minute Hand * * @property (nonatomic, strong) Calayer *minutel;
 
/** Clockwise/@property (nonatomic, strong) Calayer *hourl;
   
  @end @implementation Viewcontroller-(void) viewdidload {[Super viewdidload];
   
  Add hour [self setuphourlayer];
  
  Add the minute hand [self setupminutelayer];
   
  Add second seconds [self setupsecondlayer]; Add timer [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (Timechange) Userinfo:nil Repeats:yes];
   
Get time at the start of the run so that there will be no pauses at startup [self timechange];
   
  }-(void) Timechange {//Get current system time Nscalendar *calendar = [Nscalendar Currentcalendar]; nsdatecomponents *cmp = [Calendar Components:nscalendarunitsecond | Nscalendarunitminute |
   
  Nscalendarunithour fromdate:[nsdate Date]];
   
  Get seconds Nsinteger second = Cmp.second;
   
  Get the Nsinteger minute = Cmp.minute;
   
  Get hours Nsinteger hour = cmp.hour;
   
  Calculate the number of seconds to turn cgfloat Seconda = second * Preseconda;
   
  Calculate the minute hand turn how many degrees cgfloat Minutea = minute * Preminutea + second * preminutesecond;
   
  Calculate how much the clockwise turn cgfloat Houra = hour * Prehoura + minute * prehourminute;
   
  Rotating seconds _secondl.transform = Catransform3dmakerotation (Angle2raditon (Seconda), 0, 0, 1);
   
  Rotating minute Hand _minutel.transform = Catransform3dmakerotation (Angle2raditon (Minutea), 0, 0, 1);
   
Rotate Clockwise _hourl.transform = catransform3dmakerotation (Angle2raditon (Houra), 0, 0, 1); #pragma mark-Initialization of the hour-(void) Setuphourlayer {Calayer *hourl = [Calayer layer]; Set the second hand background color hourl.backgroundcolor = [Uicolor blackcolor].
   
  Cgcolor;
   
  Set seconds Anchor Point hourl.anchorpoint = Cgpointmake (0.5, 1);
   
  Sets the hand anchor point at the parent control's position hourl.position = Cgpointmake (kclockwh * 0.5, kclockwh * 0.5);
   
  Hourl.cornerradius = 4;
   
  Set the seconds hand bounds hourl.bounds = CGRectMake (0, 0, 4, kclockwh * 0.5-40);
   
  Add the second hand to the Clockview layer [_clockview.layer addsublayer:hourl];
   
_hourl = Hourl;
   
  #pragma mark-initializes the minute hand-(void) Setupminutelayer {calayer *minutel = [Calayer layer]; Set the second hand background color minutel.backgroundcolor = [Uicolor blackcolor].
   
  Cgcolor;
   
  Set seconds Anchor Point minutel.anchorpoint = Cgpointmake (0.5, 1);
   
  Sets the hand anchor point at the parent control's position minutel.position = Cgpointmake (kclockwh * 0.5, kclockwh * 0.5);
   
  Minutel.cornerradius = 4;
   
  Set the seconds hand bounds minutel.bounds = CGRectMake (0, 0, 4, kclockwh * 0.5-20);
   
  Add the second hand to the Clockview layer [_clockview.layer Addsublayer:minutel];
_minutel = Minutel;   
#pragma mark-initializes the second hand-(void) Setupsecondlayer {calayer *secondl = [Calayer layer]; Set the second hand background color secondl.backgroundcolor = [Uicolor Redcolor].
   
  Cgcolor;
   
  Set seconds Anchor Point secondl.anchorpoint = Cgpointmake (0.5, 1);
   
   
  Sets the hand anchor point at the parent control's position secondl.position = Cgpointmake (kclockwh * 0.5, kclockwh * 0.5);
   
  Set the seconds hand bounds secondl.bounds = CGRectMake (0, 0, 1.5, kclockwh * 0.5-20);
   
  Add the second hand to the Clockview layer [_clockview.layer Addsublayer:secondl];
   
_secondl = Secondl; } @end

Effect Chart:

The above is the entire content of this article, I hope to learn about iOS program design help.

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.