iOS 18th day (4, core animation, clock effect, timer, picture rotation angle, Calayer anchor point, get current, hour, second, minute)

Source: Internet
Author: User

****

#import "HMViewController.h"//Seconds to 6 degrees per second .#definePersecenda 6//6 degrees per minute .#definePerminutea 6//Turn 6 degrees per hour clockwise .#definePerhoura 30//turn 6 degrees per minute .#definePerminutehoura 0.5#defineAngle2radian (x) ((x)/180.0 * M_PI)@interfaceHmviewcontroller () {Calayer*_second; Calayer*_minute; Calayer*_hour;} @property (Weak, nonatomic) Iboutlet UIView*Redview, @property (weak, nonatomic) Iboutlet Uiimageview*Clockview;@end@implementationHmviewcontroller- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib. //1. Add the second hand[self addsecond]; //2. Add the minute hand[self addmintue]; //3. Add the hour hand[self addhour]; //Creating Timers[Nstimer Scheduledtimerwithtimeinterval:1target:self selector: @selector (update) Userinfo:nil Repeats:yes]; [Self update];}- (void) addhour{cgfloat Imagew=_clockview.bounds.size.width; CGFloat Imageh=_clockview.bounds.size.height; //1. Add the hour handCalayer *hour =[Calayer layer]; //2. Setting anchor pointsHour.anchorpoint = Cgpointmake (0.5,1); //3. Setting the locationHour.position = Cgpointmake (Imagew *0.5, Imageh *0.5); //4. Setting the sizeHour.bounds = CGRectMake (0,0,4, Imageh *0.5- -); //5. RedHour.backgroundcolor =[Uicolor Blackcolor].        Cgcolor; Hour.cornerradius=4; //Add to Layer[_clockview.layer Addsublayer:hour]; _hour=Hour;}//Add minute hand- (void) addmintue{cgfloat Imagew=_clockview.bounds.size.width; CGFloat Imageh=_clockview.bounds.size.height; //1. Add the minute handCalayer *minute =[Calayer layer]; //2. Setting anchor pointsMinute.anchorpoint = Cgpointmake (0.5,1); //3. Setting the locationMinute.position = Cgpointmake (Imagew *0.5, Imageh *0.5); //4. Setting the sizeMinute.bounds = CGRectMake (0,0,2, Imageh *0.5- -); //5. RedMinute.backgroundcolor =[Uicolor Bluecolor].        Cgcolor; //Add to Layer[_clockview.layer Addsublayer:minute]; _minute=minute;}//Add seconds- (void) addsecond{cgfloat Imagew=_clockview.bounds.size.width; CGFloat Imageh=_clockview.bounds.size.height; //1. Add the second handCalayer *second =[Calayer layer]; //2. Setting anchor pointsSecond.anchorpoint = Cgpointmake (0.5,1); //3. Setting the locationSecond.position = Cgpointmake (Imagew *0.5, Imageh *0.5); //4. Setting the sizeSecond.bounds = CGRectMake (0,0,1, Imageh *0.5- -); //5. RedSecond.backgroundcolor =[Uicolor Redcolor].        Cgcolor; //Add to Layer[_clockview.layer Addsublayer:second]; _second=second;}- (void) update{//gets the number of seconds//Get Calendar ObjectNscalendar *calendar =[Nscalendar Currentcalendar]; //Get Date Componentnsdatecomponents *compoents = [Calendar Components:nscalendarunitsecond | Nscalendarunitminute |nscalendarunithour fromdate:[nsdate Date]]; //gets the number of secondsCGFloat sec =Compoents.second; //Get minutesCGFloat minute =Compoents.minute; //Get HoursCGFloat hour =Compoents.hour; //figure out how many degrees the second hand rotates .CGFloat Seconda = sec *Persecenda; //figure out how many degrees the minute hand rotatesCGFloat Minutea = minute *Perminutea; //figure out how many degrees the hour is rotated .CGFloat Houra = Hour *Perhoura; Houra+ = minute *Perminutehoura; //Rotating seconds_second.transform = Catransform3dmakerotation (Angle2radian (Seconda),0,0,1); //Rotating minute hand_minute.transform = Catransform3dmakerotation (Angle2radian (Minutea),0,0,1); //Rotate Hour_hour.transform = Catransform3dmakerotation (Angle2radian (Houra),0,0,1);}@end

iOS 18th day (4, core animation, clock effect, timer, picture rotation angle, Calayer anchor point, get current, hour, second, minute)

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.