iOS實(shí)現(xiàn)簡易鐘表
本文實(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)
//計算秒針當(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)
//計算分針當(dāng)前旋轉(zhuǎn)的角度
// angle = 當(dāng)前多少分 * 每一分旋轉(zhuǎn)多少度
CGFloat minA = curMin * perMinA;
self.minL.transform = CATransform3DMakeRotation(angle2Rad(minA), 0, 0, 1);
//時針開始旋轉(zhuǎn)
//計算時針當(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中Button按鈕的狀態(tài)和點(diǎn)擊事件
這篇文章先是給大家介紹iOS中Button按鈕的狀態(tài),而后又詳細(xì)介紹了iOS中按鈕點(diǎn)擊事件處理方式,本文介紹的很詳細(xì),有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-09-09
iOS ScrollView實(shí)現(xiàn)自動布局的方法(適用Swift 3.0 )
傳說中有一個美工ios開發(fā)者在遇到這個問題的時候特意跑到蘋果總部去咨詢?nèi)绾螌crollview進(jìn)行自動布局。當(dāng)然大家不用去了,下面這篇文章就來給大家介紹關(guān)于iOS ScrollView實(shí)現(xiàn)自動布局的方法,文中的語法同樣也適用Swift 3.0 ,需要的朋友可以參考下。2017-12-12
利用iOS開發(fā)實(shí)現(xiàn)翻轉(zhuǎn)撲克牌動畫的方法
這篇文章主要給大家介紹了關(guān)于利用iOS開發(fā)實(shí)現(xiàn)翻撲克牌動畫的方法,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07
iOS開發(fā)中實(shí)現(xiàn)郵件和短信發(fā)送的簡單示例
這篇文章主要介紹了iOS開發(fā)中實(shí)現(xiàn)郵件和短信發(fā)送的簡單示例,編程語言依然是傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-09-09
詳解iOS App設(shè)計模式開發(fā)中對于享元模式的運(yùn)用
這篇文章主要介紹了iOS App設(shè)計模式開發(fā)中對于享元模式的運(yùn)用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04
iOS漸變圓環(huán)旋轉(zhuǎn)動畫CAShapeLayer CAGradientLayer
這篇文章主要介紹了iOS漸變圓環(huán)旋轉(zhuǎn)動畫CAShapeLayer CAGradientLayer的相關(guān)資料,需要的朋友可以參考下2016-09-09

