ios-简易时钟

下面教大家用CALayer做时钟,不啰嗦了,上图上代码。

#import"ViewController.h"

#define perSec6

#define perMin6

#define perHour30

#define angleToRadious(angle) ((angle)/180.0* M_PI)

@interfaceViewController()

@property(nonatomic,strong)CALayer*secLay;

@property(nonatomic,strong)CALayer*minLay;

@property(nonatomic,strong)CALayer*HourLay;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

UIImageView*img = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"timg.jpeg"]];

img.frame=CGRectMake(0,0,200,200);

img.center=self.view.center;

[self.viewaddSubview:img];

//分针

self.minLay= [[CALayeralloc]init];

self.minLay.frame=CGRectMake(0,0,2,80);

//position这个坐标是相对父视图的

self.minLay.position=CGPointMake(img.bounds.size.width/2, img.bounds.size.height/2);

//anchorPoint:这个坐标是本View的中心坐标,anchorPoint跟position是永远重合的,所以想要anchorPoint和position相同,则需要把anchorPoint设置为(0,0),但该案例需求不同,因为我们希望把秒针设置到表盘的中心点,而此时的anchorPoint还是(0。5,0.5),这是默认的,如果需要设置成从表盘中心原点向外延伸的一条直线,则需要把anchorPoint设置为(0.5,1),0.5是为了让该线处于原点中心,而1呢,是为了让它与position重合。

self.minLay.anchorPoint=CGPointMake(0.5,1);

self.minLay.backgroundColor= [UIColorblackColor].CGColor;

[img.layeraddSublayer:self.minLay];

//时针

self.HourLay= [[CALayeralloc]init];

self.HourLay.frame=CGRectMake(0,0,2,60);

self.HourLay.position=CGPointMake(img.bounds.size.width/2, img.bounds.size.height/2);

self.HourLay.anchorPoint=CGPointMake(0.5,1);

self.HourLay.backgroundColor= [UIColorblackColor].CGColor;

[img.layeraddSublayer:self.HourLay];

//秒针

self.secLay= [[CALayeralloc]init];

self.secLay.frame=CGRectMake(0,0,1,80);

self.secLay.position=CGPointMake(img.bounds.size.width/2, img.bounds.size.height/2);

self.secLay.anchorPoint=CGPointMake(0.5,1);

self.secLay.backgroundColor= [UIColorredColor].CGColor;

[img.layeraddSublayer:self.secLay];

//数字Label

UILabel*timelb = [[UILabelalloc]init];

timelb.frame=CGRectMake(0,120,self.view.bounds.size.width,100);

timelb.font= [UIFontsystemFontOfSize:50];

timelb.textAlignment=NSTextAlignmentCenter;

[self.viewaddSubview:timelb];

__weakViewController*weakSelf =self;

[NSTimerscheduledTimerWithTimeInterval:1repeats:YESblock:^(NSTimer*_Nonnulltimer) {

//获取当前日历

NSCalendar*cal = [NSCalendarcurrentCalendar];

//根据需要获取对应的日历参数,比如时分秒:NSCalendarUnitHour、NSCalendarUnitMinute、NSCalendarUnitSecond

NSDateComponents*cmp = [calcomponents:NSCalendarUnitSecond|NSCalendarUnitMinute|NSCalendarUnitHourfromDate:[NSDatedate]];

NSIntegersec = cmp.second;

NSIntegermin = cmp.minute;

NSIntegerhour = cmp.hour;

//秒针一分钟走360度,每一秒是6度,根据当前秒数乘以度数,就是秒针在表盘上的旋转度数了,同理分针

CGFloatcurrSec = sec *perSec;

CGFloatcruuMin = min *perMin;

//时针比较特殊,它的幅度还有分针的幅度有关,那么需要知道一小时占六十分钟的百分比是多少?就是30/60 = 0.5度。举个例子:当时间在1小时45分的时候,如果用hour * perHour,那么得出的时针只是指到1点而已,而实际中,这个幅度是已经快两点了,那么是不符合要求的,所以需要知道分针当前的时间乘上0.5度再加上hour * perHour,这才是正确的刻度。

CGFloatcurrhour = hour *perHour+ min *0.5;

/*

*CATransform3DMakeRotation(<#CGFloat angle#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat z#>)

*在这个API中,第一个参数表示旋转的度数,而在上面中,我们已经知道了时分秒该旋转多少度,但还不是真正的度数,需要进行转换

* #define angleToRadious(angle) ((angle)/180.0 * M_PI)通过这个宏,转换成度数

*后面的X,Y,Z的数值取值都是在[0,1]区间的,在这个案例中,因为我们的时分秒针都围绕Z轴旋转的,所以X,Y取零便可。

*/

weakSelf.secLay.transform=CATransform3DMakeRotation(angleToRadious(currSec),0,0,1);

weakSelf.minLay.transform=CATransform3DMakeRotation(angleToRadious(cruuMin),0,0,1);

weakSelf.HourLay.transform=CATransform3DMakeRotation(angleToRadious(currhour),0,0,1);

timelb.text= [NSStringstringWithFormat:@"%zd : %zd : %zd",hour,min,sec];

}];

}

@end


图一

效果如图一所示,直接新建View或者ViewController,代码原封不动搬过去就可以用了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • #import "ViewController.h" //每一秒旋转多少度 #define perSecA6 //...
    会写bug的程序媛阅读 1,691评论 4 1
  • Transform field value key paths! position.x position.y 1....
    youngZhou阅读 374评论 0 1
  • CALayer 概念 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮、一个文本标签、一个文...
    iOS_Cqlee阅读 1,046评论 8 2
  • 动画只是Core Animation特性的冰山一角 Core Animation功能 Core Animation...
    封楼阅读 383评论 0 0
  • 晚上下班回来经常盯着手机屏幕发呆,就想写点什么,打发下漫长的夜晚,并记录下工作中的点点滴滴。 1.tablevie...
    so_what阅读 1,867评论 3 5