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

注意:表盤是一個(gè)UIImageView控件,設(shè)置image為表盤圖片
核心代碼:
//
// ViewController.m
// 時(shí)鐘
//
// 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
//每一小時(shí)旋轉(zhuǎn)多少度
#define perHourA 30
//每一分時(shí)針旋轉(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;
//秒針開(kāi)始旋轉(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);
//分針開(kāi)始旋轉(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);
//時(shí)針開(kāi)始旋轉(zhuǎn)
//計(jì)算時(shí)針當(dāng)前旋轉(zhuǎn)的角度
// angle = 當(dāng)前多少時(shí) * 每一小時(shí)旋轉(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;
}
//添加時(shí)針
- (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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開(kāi)發(fā)中文件的上傳和下載功能的基本實(shí)現(xiàn)
這篇文章主要介紹了iOS開(kāi)發(fā)中文件的上傳和下載功能的基本實(shí)現(xiàn),并且下載方面講到了大文件的多線程斷點(diǎn)下載,需要的朋友可以參考下2015-11-11
以代碼實(shí)例總結(jié)iOS應(yīng)用開(kāi)發(fā)中數(shù)據(jù)的存儲(chǔ)方式
這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中數(shù)據(jù)的存儲(chǔ)方式的實(shí)例總結(jié),代碼為傳統(tǒng)的Objective-C語(yǔ)言,需要的朋友可以參考下2016-02-02
IOS 仿時(shí)光網(wǎng)選票UI實(shí)例代碼
這篇文章主要介紹了IOS 仿時(shí)光網(wǎng)選票UI實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09
iOS開(kāi)發(fā)中控制屏幕旋轉(zhuǎn)的編寫方法小結(jié)
學(xué)習(xí)iOS開(kāi)關(guān)按鈕UISwitch控件

