The implementation is as follows:
The implementation code is as follows:
#import "ViewController.h"
Converts the rotation angle to the radian system
#define Angletoradion (angle) ((angle)/180.0 * M_PI)
The second hand turns the angle every second.
#define Persecondangle 6
The minute minute, the angle of rotation.
#define Perminuteangle 6
The angle at which the hour hand turns hourly
#define Perhourangle 30
The angle at which the hour hand turns every minute
#define PERMUNITEHOURANGLE 0.5
#define KCLOCKWIDTH 300
#define KCLOCKHEIGHT 250
@interface Viewcontroller ()
Chassis
@property (Weak, nonatomic) Iboutlet Uiimageview *imgview;
@property (nonatomic, weak) Calayer *secondlayer;//sec
@property (nonatomic, weak) calayer *minutelayer;//min
@property (nonatomic, weak) Calayer *hourlayer;//
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Clock
[Self setuphourlayer];
Minutes
[Self setupminutelayer];
Seconds
[Self setupsecondlayer];
Creating timers
[Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (timechange) Userinfo:nil Repeats:yes];
}
-(void) Setuphourlayer
{
Create a layer
Calayer *layer = [Calayer layer];
Layer.backgroundcolor = [Uicolor blackcolor]. Cgcolor;
Layer.cornerradius = 8;
Set the anchor point of a layer
Layer.anchorpoint = Cgpointmake (0.5, 1);
Set the position and dimensions of a layer
Layer.position = Cgpointmake (kclockwidth * 0.5, Kclockheight * 0.5);
Layer.bounds = CGRectMake (0, 0, 5, kclockwidth * 0.5-44);
Add a layer to a parent layer
[Self.imgView.layer Addsublayer:layer];
Self.hourlayer = layer;
}
-(void) Setupminutelayer
{
Calayer *layer = [Calayer layer];
Layer.backgroundcolor = [Uicolor blackcolor]. Cgcolor;
Layer.cornerradius = 4;
Set anchor points
Layer.anchorpoint = Cgpointmake (0.5, 1);
Setting Location and Dimensions
Layer.position = Cgpointmake (kclockwidth * 0.5, Kclockheight * 0.5);
Layer.bounds = CGRectMake (0, 0, 3, kclockwidth * 0.5-34);
Add a layer to a parent layer
[Self.imgView.layer Addsublayer:layer];
Self.minutelayer = layer;
}
-(void) Setupsecondlayer
{
Calayer *layer = [Calayer layer];
Layer.backgroundcolor = [Uicolor Redcolor]. Cgcolor;
Set anchor points
Layer.anchorpoint = Cgpointmake (0.5, 1);
Setting Location and Dimensions
Layer.position = Cgpointmake (kclockwidth * 0.5, Kclockheight * 0.5);
Layer.bounds = CGRectMake (0, 0, 1, kclockwidth * 0.5-24);
Add a layer to a parent layer
[Self.imgView.layer Addsublayer:layer];
Self.secondlayer = layer;
}
-(void) Timechange
{
Get Calendar Object
Nscalendar *calendar = [Nscalendar Currentcalendar];
NSLog (@ "calendar =%", calendar);
Get Date Component
nsdatecomponents *components = [Calendar Components:nscalendarunitsecond | Nscalendarunitminute | Nscalendarunithour fromdate:[nsdate Date]];
Gets the current time division seconds value
Nsinteger second = Components.second;
Nsinteger munite = Components.minute;
Nsinteger hour = Components.hour;
Calculates the angle at which the stopwatch needle turns at the current time (Radian system)
CGFloat secondangle = angletoradion (second * persecondangle);
CGFloat muniteangle = angletoradion (Munite * perminuteangle);
CGFloat hourangle = Angletoradion (hour *perhourangle + munite * permunitehourangle);
Modify the Transform property of the layer where the stopwatch pin is located, perform an implicit animation
Self.secondLayer.transform = catransform3dmakerotation (secondangle, 0, 0, 1);
Self.minuteLayer.transform = catransform3dmakerotation (muniteangle, 0, 0, 1);
Self.hourLayer.transform = catransform3dmakerotation (hourangle, 0, 0, 1);
}
@end
In the process of implementation, some minor flaws ... I hope you can study with us!!!
iOS for simple clock effects