欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS實(shí)現(xiàn)簡易鐘表

 更新時間:2020年02月21日 07:33:40   作者:LayneCheung  
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)簡易鐘表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了iOS實(shí)現(xiàn)簡易鐘表的具體代碼,供大家參考,具體內(nèi)容如下

效果圖:

注意:表盤是一個UIImageView控件,設(shè)置image為表盤圖片

核心代碼:

//
// ViewController.m
// 時鐘
//
// Created by llkj on 2017/8/29.
// Copyright © 2017年 LayneCheung. All rights reserved.
//

#import "ViewController.h"

//每一秒旋轉(zhuǎn)多少度
#define perSecA 6
//每一分旋轉(zhuǎn)多少度
#define perMinA 6
//每一小時旋轉(zhuǎn)多少度
#define perHourA 30

//每一分時針旋轉(zhuǎn)的度數(shù)
#define perMinHour 0.5
//角度轉(zhuǎn)弧度
#define angle2Rad(angle) ((angle) / 180.0 * M_PI)
@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *clockView;
@property (nonatomic, weak) CALayer *secL;
@property (nonatomic, weak) CALayer *minL;
@property (nonatomic, weak) CALayer *hourL;
@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];

 [self setHour];
 [self setMin];
 [self setSec];

 [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
 [self timeChange];
}

- (void)timeChange{

 //獲取當(dāng)前秒
 NSCalendar *cal = [NSCalendar currentCalendar];
 NSDateComponents *cmp = [cal components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
 NSInteger curSec = cmp.second + 1;
 NSInteger curMin = cmp.minute;
 NSInteger curHour = cmp.hour;

 //秒針開始旋轉(zhuǎn)
 //計(jì)算秒針當(dāng)前旋轉(zhuǎn)的角度
 // angle = 當(dāng)前多少秒 * 每一秒旋轉(zhuǎn)多少度
 CGFloat secA = curSec * perSecA;
 //旋轉(zhuǎn)方向是Z軸
 self.secL.transform = CATransform3DMakeRotation(angle2Rad(secA), 0, 0, 1);


 //分針開始旋轉(zhuǎn)
 //計(jì)算分針當(dāng)前旋轉(zhuǎn)的角度
 // angle = 當(dāng)前多少分 * 每一分旋轉(zhuǎn)多少度
 CGFloat minA = curMin * perMinA;
 self.minL.transform = CATransform3DMakeRotation(angle2Rad(minA), 0, 0, 1);


 //時針開始旋轉(zhuǎn)
 //計(jì)算時針當(dāng)前旋轉(zhuǎn)的角度
 // angle = 當(dāng)前多少時 * 每一小時旋轉(zhuǎn)多少度
 CGFloat hourA = curHour * perHourA + curMin * perMinHour;

 self.hourL.transform = CATransform3DMakeRotation(angle2Rad(hourA), 0, 0, 1);
}
//添加秒針
- (void)setSec{

 CALayer *secL = [CALayer layer];
 secL.bounds = CGRectMake(0, 0, 1, 80);
 secL.backgroundColor = [UIColor redColor].CGColor;
 //繞著錨點(diǎn)旋轉(zhuǎn)
 secL.anchorPoint = CGPointMake(0.5, 1);
 secL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5);
 [self.clockView.layer addSublayer:secL];
 self.secL = secL;

}

//添加分針
- (void)setMin{

 CALayer *minL = [CALayer layer];
 minL.bounds = CGRectMake(0, 0, 3, 70);
 minL.cornerRadius = 1.5;
 minL.backgroundColor = [UIColor blackColor].CGColor;
 minL.anchorPoint = CGPointMake(0.5, 1);
 minL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5);
 [self.clockView.layer addSublayer:minL];
 self.minL = minL;

}

//添加時針
- (void)setHour{

 CALayer *hourL = [CALayer layer];
 hourL.bounds = CGRectMake(0, 0, 3, 60);
 hourL.backgroundColor = [UIColor blackColor].CGColor;
 hourL.anchorPoint = CGPointMake(0.5, 1);
 hourL.position = CGPointMake(self.clockView.bounds.size.width * 0.5, self.clockView.bounds.size.height * 0.5);
 [self.clockView.layer addSublayer:hourL];
 self.hourL = hourL;

}
- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.
}


@end

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS開發(fā)中控制屏幕旋轉(zhuǎn)的編寫方法小結(jié)

    iOS開發(fā)中控制屏幕旋轉(zhuǎn)的編寫方法小結(jié)

    這篇文章主要介紹了iOS開發(fā)中控制屏幕旋轉(zhuǎn)的編寫方法小結(jié),包括橫豎屏切換時視圖所出現(xiàn)的問題等經(jīng)常需要注意的地方,需要的朋友可以參考下
    2015-10-10
  • 學(xué)習(xí)iOS開關(guān)按鈕UISwitch控件

    學(xué)習(xí)iOS開關(guān)按鈕UISwitch控件

    這篇文章主要為大家詳細(xì)介紹了iOS開關(guān)按鈕UISwitch控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 最新評論