Animating the clock with quartz2d (i)

Source: Internet
Author: User

Animating the clock with quartz2d (i)

To achieve the clock effect, first drag the material dial into the project

1. Define hour, minute and second seconds three layer class member properties

@property (Nonatomic,strong) calayer ***hourlayer;

2. Create a dial layer, set properties, and add to the parent view

//     Create a clock layer  setting property    calayer *clocklayer=[calayer layer];    Clocklayer.contentsID _nullable) ([UIImage imagenamed:@ "05-clock"  ]. Cgimage);    Clocklayer.position=self.view.center;    Clocklayer.bounds=cgrectmake (00);    [Self.view.layer Addsublayer:clocklayer];

3. Then we create the pointer layer

3.1 Create an hour layer to add to the clock layer

 //Create an hour layer, set the properties of the layersCalayer *hourlayer=[Calayer layer]; Hourlayer.backgroundcolor=[Uicolor Blackcolor].    Cgcolor; Hourlayer.bounds=cgrectmake (0,0,4, $); Hourlayer.position=cgpointmake (clocklayer.bounds.size.width*0.5, clocklayer.bounds.size.height*0.5); Hourlayer.anchorpoint=cgpointmake (0.5,0.75); //Modify the anchor point of the hour hand[Clocklayer Addsublayer:hourlayer]; _hourlayer=hourlayer;

3.2 Create a minute hand layer to add to the clock layer

//Create a minute hand layer, set the properties of the layersCalayer *minutelayer=[Calayer layer]; Minutelayer.backgroundcolor=[Uicolor Redcolor].    Cgcolor; Minutelayer.bounds=cgrectmake (0,0,4, -); Minutelayer.position=cgpointmake (clocklayer.bounds.size.width*0.5, clocklayer.bounds.size.height*0.5); Minutelayer.anchorpoint=cgpointmake (0.5,0.75); //Modify the anchor point of the minute hand[Clocklayer Addsublayer:minutelayer]; _minutelayer=minutelayer;

3.3 Create a second hand layer to add to the clock layer

//Create a minute hand layer, set the properties of the layersCalayer *minutelayer=[Calayer layer]; Minutelayer.backgroundcolor=[Uicolor Redcolor].    Cgcolor; Minutelayer.bounds=cgrectmake (0,0,4, -); Minutelayer.position=cgpointmake (clocklayer.bounds.size.width*0.5, clocklayer.bounds.size.height*0.5); Minutelayer.anchorpoint=cgpointmake (0.5,0.75); //Modify the anchor point of the minute hand[Clocklayer Addsublayer:minutelayer]; _minutelayer=minutelayer;

4. Use Cadisplaylink to turn on the timer function to refresh the data in real time (you can also use the Nstimer method).

//   Turn on timer, monitor time    Cadisplaylink *link=[cadisplaylink displaylinkwithtarget:self selector: @selector (startrotate)];    [link Addtorunloop:[nsrunloop Mainrunloop] formode:nsrunloopcommonmodes];

At this point our engineering interface has been loaded, and now we have to build the relationship between them, to implement the Startrotate method, so that the pointer rotates over time.

5. Introducing Nscalendar to get the current time

Nscalendar *calendar=[Nscalendar Currentcalendar];    *components= [Calendar Components:nscalendarunitsecond | Nscalendarunitminute | Nscalendarunithour fromdate:[nsdate Date]];

Get Time Properties

   //     Gets the number of seconds    CGFloat sec=components.second;     //     Gets the number of minutes    CGFloat minute=components.minute;     //     Get hours    CGFloat Hour=components.hour;

6. Next, we need to clarify the relationship between the hour, minute and second hand.

#define Perseconda 6  / second 6 °#define Perminutea 6  //  Minute hand rotation 6 °#define perhoura  // hourly 6 °#define Perminutehoura 0.5   // hourly rotation 0.5 °#define  Angle2randian (x)  ((x)/ 180.0*M_PI)    // angle to radians

7. Calculate the hour, minute, and second hand that need to be rotated.

   //     figure out how many degrees    the second hand rotates. CGFloat seconda=sec *Perseconda;     //     figure out how many degrees    the minute hand rotates. CGFloat minutea=minute*Perminutea;     //    figure out how many degrees    the hour hand rotates. CGFloat houra=hour*Perhoura;    //     the hour hand and the minute hand establish the relation    Houra +=minute*perminutehoura;

8. Call the Catransform3dmakerotation method to implement a pointer jump

0 0 1 );  Self.minuteLayer.transform001);    Self.hourLayer.transform001);

Finally run the project.

Animating the clock with quartz2d (i)

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.