05-Clock Effect

Source: Internet
Author: User

05-Clock effect

1. Build the interface.    Analysis interface.    Interface hour, minute, and second hand do not need to interact with the user. So you can use layer mode to do it.    Before doing, observe what the hour hand is doing.    is rotated around the center point of the dial, based on the current time (note that both are integers).    To understand a very important point of knowledge. Whether it's spinning or zooming, it's all about the anchor point.    To make the hour, minute, and the middle of the needle display, rotate around the center point.    It is necessary to set its position and Anchorpoint two properties.     Create second hand calayer *layer = [Calayer layer];    _seclayer = layer;    Layer.bounds = CGRectMake (0, 0, 1, 80);    Layer.anchorpoint = Cgpointmake (0.5, 1);    Layer.position = Cgpointmake (_clockview.bounds.size.width * 0.5, _clockview.bounds.size.height * 0.5); Layer.backgroundcolor = [Uicolor Redcolor].    Cgcolor; [_clockview.layer Addsublayer:layer];    2. Let the second hand begin to rotate.    Let the second hand rotate. So what is the current degree of rotation?    The current rotation angle is: the current time * How many degrees of rotation per second.    Calculates the number of rotations per second.    60 seconds turn 360 degrees 360 divided by 60 is how many degrees per second. Turn 6 degrees per second.    Gets the current time to create a calendar class Nscalendar *calendar = [Nscalendar Currentcalendar]; Convert a calendar class to a Date Component date component (year, month, day, hour, minute, second) component: Date component has what composition, he is an enumeration, inside has month and day seconds FromDate: current date nsdatecomponents *cmp = [Calendar Components:nscalendarunitsecond Fromdate:[nSdate Date]];    Our seconds are stored in the date component, which provides a lot of get methods.    Nsinteger second = Cmp.second;    Then the angle of the current second hand rotation is the current number of seconds multiplied by how many degrees per second.    Second * Perseca also have to convert the angle into radians.    Because the minute hand is also used, it pulls out a macro with a speed parameter.    #define ANGLE2RAD (angle) ((angle)/180.0 * m_pi) make it rotate once every second. So add a timer.     Call each second, rotate the second hand-(void) timechange{get the current number of seconds Create Calendar class Nscalendar *calendar = [Nscalendar Currentcalendar]; Converts a calendar class to a Date Component date component (year, month, day, hour, minute, second) component: Date component has what composition, he is an enumeration, inside has month and day seconds FromDate: current date nsdatecomponents *cm     p = [Calendar Components:nscalendarunitsecond fromdate:[nsdate Date]];     Our seconds are stored in the date component, which provides a lot of get methods.     Nsinteger second = Cmp.second;     How many degrees the second hand rotates.     CGFloat Angel = Angle2rad (second * Perseca);     Rotating seconds Self.secondL.transform = catransform3dmakerotation (Angel, 0, 0, 1);    Run to find out he'll just turn to a certain time to start the rotation at the beginning of the time to come to this method, get the current number of seconds to position it well.    To call the Timechange method once the timer has been added.    3. Add a quick copy of the minute hand and then add a minute hand member property. Modify the width, change the color to let it rotate, to calculate the rotation per minuteHow many degrees turn 60 minutes is just a lap so every minute is also 6 degrees.    Get the current number of points?     Also in the date component get inside there are left-shift symbols, right-shift symbols. He can use a parallel operation and now let him support the number of seconds and points immediately after adding a |                                         nsdatecomponents *cmp = [Calendar Components:nscalendarunitsecond |    Nscalendarunitminute fromdate:[nsdate Date]];    CGFloat Minueteangel = Angle2rad (minute * perminutea);    Self.minueL.transform = catransform3dmakerotation (minueteangel, 0, 0, 1);     4. Add the hour before the same copy, add one hours of attribute hours to how many degrees are currently how many hours, and then calculate the number of times per hour.     12-hour round. 360 divided by 12, turn 30 degrees per hour how many degrees cgfloat Hourangel = Angle2rad (Hour * perhoura);    Rotation hour Self.hourL.transform = catransform3dmakerotation (hourangel, 0, 0, 1);    It's a problem to write directly, and the hour will move a little bit. The next thing to figure out, every minute, hours to turn how many degrees 60 minutes an hour. Turn 30 degrees an hour. 30 divided by 60, that is, every minute, the number of hours to turn. 0.5
60 seconds a minute, 6 degrees a minute.
6 divided by 60, that is, every second, the minute hand how many degrees. 0.1
How many degrees of clockwise rotation cgfloat Hourangel = Angle2rad (Hour * Perhoura + minute * Perminutehoura); Rotation hour Self.hourL.transform = catransform3dmakerotation (hourangel, 0, 0, 1);
  
How many degrees the minute hand rotates
   CGFloat minueteAngel = angle2Rad(minute * perMinuteA + second * perSecondMinuteA));
  旋转分钟
  self.minueL.transform = CATransform3DMakeRotation(minueteAngel, 0, 0, 1);

05-Clock Effect

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.