美文网首页
iOS时钟搭建

iOS时钟搭建

作者: 恋空K | 来源:发表于2022-12-04 15:58 被阅读0次
    CALayer所有的旋转和缩放都是围绕锚点进行的。UIView的center是相对于父控件而言的
    
    #import "ViewController.h"
    
    #define angleToRadio(angle) ((angle) * M_PI / 180.0)
    
    //每一秒旋转6度
    #define perSecA 6
    
    //每一分旋转6度
    #define perMinA 6
    
    //每一小时旋转 30 度
    #define perHourA 30
    
    //每一分时针旋转多少度 0.5
    #define  perMinHourA 0.5
    
    @interface ViewController ()
    
    @property (weak, nonatomic) IBOutlet UIImageView *colockView;
    @property (weak, nonatomic) CALayer *secL;
    @property (weak, nonatomic) CALayer *minL;
    @property (weak, nonatomic) CALayer *hourL;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //所有的旋转,缩放都是绕着锚点进行的.
        //添加时针
        [self addHourL];
        
        //添加分针
        [self addMinL];
        
        //添加秒针
        [self addSecL];
       
        //添加定时器
        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
        
        [self timeChange];
    }
    
    - (void)timeChange {
        //让秒针开始旋转
        //获取当前的秒数
        //获取当前的日历
        NSCalendar *cal = [NSCalendar currentCalendar];
        //从什么时间获取日历当中的哪些组件(年,月,日,时,分,秒)
        NSDateComponents *cpt = [cal components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
        //当前时间的秒数
        NSInteger curSec =  cpt.second;
        NSInteger curMin = cpt.minute;
        NSInteger curHour = cpt.hour;
        NSLog(@"%ld-%ld-%ld",curHour,curMin,curSec);
        
        //求角度 = 当前的秒数 * 每一秒旋转多少度
        CGFloat secA = curSec * perSecA;
        self.secL.transform = CATransform3DMakeRotation(angleToRadio(secA), 0, 0, 1);
        
        //旋转分针
        //求角度 = 当前多少分 * 每一分旋转多少度
        CGFloat minA = curMin * perMinA;
        self.minL.transform =  CATransform3DMakeRotation(angleToRadio(minA), 0, 0, 1);
        
        //旋转时针
        //求角度 = 当前多少小时 * 每一小时旋转多少度
        CGFloat hourA = curHour * perHourA + curMin * perMinHourA;
        self.hourL.transform =  CATransform3DMakeRotation(angleToRadio(hourA), 0, 0, 1);
    }
    
    //所有的旋转,缩放都是绕着锚点进行的.
    - (void)addSecL {
        CALayer *secL = [CALayer layer];
        secL.backgroundColor = [UIColor redColor].CGColor;
        secL.bounds = CGRectMake(0, 0, 1, 80);
        
        secL.anchorPoint = CGPointMake(0.5, 1);
        secL.position = CGPointMake(self.colockView.bounds.size.width * 0.5, self.colockView.bounds.size.height * 0.5);
        
        [self.colockView.layer addSublayer:secL];
        self.secL = secL;
    }
    
    - (void)addMinL {
        CALayer *minL = [CALayer layer];
        minL.backgroundColor = [UIColor blackColor].CGColor;
        minL.bounds = CGRectMake(0, 0, 2, 70);
        minL.anchorPoint = CGPointMake(0.5, 1);
        minL.position = CGPointMake(self.colockView.bounds.size.width * 0.5, self.colockView.bounds.size.height * 0.5);
        
        [self.colockView.layer addSublayer:minL];
        self.minL = minL;
    }
    
    - (void)addHourL {
        CALayer *hourL = [CALayer layer];
        hourL.backgroundColor = [UIColor blackColor].CGColor;
        hourL.bounds = CGRectMake(0, 0, 3, 60);
        hourL.anchorPoint = CGPointMake(0.5, 1);
        hourL.position = CGPointMake(self.colockView.bounds.size.width * 0.5, self.colockView.bounds.size.height * 0.5);
        
        [self.colockView.layer addSublayer:hourL];
        self.hourL = hourL;
    }
    
    @end
    
    时钟效果代码如上

    相关文章

      网友评论

          本文标题:iOS时钟搭建

          本文链接:https://www.haomeiwen.com/subject/zlevfdtx.html