一個圓形時鐘
先上效果看看:
突然想到時鐘挺有意思的就弄了個
先畫個錶盤吧
///
CGPoint center = CGPointMake(ViewWidth*0.5, ViewHeight*0.5);
CAShapeLayer *smillLayer = [CAShapeLayer layer];
UIBezierPath *smillpath = [UIBezierPath bezierPath];
for (int i = 0 ; i<60; i++) {
CGPoint pos = [self getRoundPointR:ViewR angle:-90+6*I];
CGPoint pos1 = [self getRoundPointR:ViewR-5 angle:-90+6*I];
[smillpath moveToPoint:CGPointMake(pos.x, pos.y)];
[smillpath addLineToPoint:CGPointMake(pos1.x, pos1.y)];
}
smillLayer.path = smillpath.CGPath;
smillLayer.lineWidth = 1;
smillLayer.strokeColor = self.theDialColor.CGColor;
smillLayer.fillColor = [UIColor clearColor].CGColor;
[self.layer addSublayer:smillLayer];
CAShapeLayer *momentLayer = [CAShapeLayer layer];
UIBezierPath *momentpath = [UIBezierPath bezierPath];
NSArray *arr = @[@"12",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11"];
for (int i = 0 ; i<arr.count; i++) {
CGPoint pos = [self getRoundPointR:ViewR angle:-90+30*I];
CGPoint pos1 = [self getRoundPointR:ViewR-8 angle:-90+30*I];
[momentpath moveToPoint:CGPointMake(pos.x, pos.y)];
[momentpath addLineToPoint:CGPointMake(pos1.x, pos1.y)];
CGPoint pos2 = [self getRoundPointR:ViewR-16 angle:-90+30*I];
NSString *str = arr[i];
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
CGSize size = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, 0.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor blackColor]};
[str drawAtPoint:CGPointMake(pos2.x-size.width/2, pos2.y-7) withAttributes:dict];
}
momentLayer.path = momentpath.CGPath;
momentLayer.lineWidth = 2;
momentLayer.strokeColor = self.theDialColor.CGColor;
momentLayer.fillColor = [UIColor clearColor].CGColor;
[self.layer addSublayer:momentLayer];
CAShapeLayer *backGroundLayer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:ViewR startAngle:-M_PI_2 endAngle:-M_PI_2+M_PI_2*4 clockwise:YES];
backGroundLayer.path = path.CGPath;
backGroundLayer.lineWidth = 1;
backGroundLayer.strokeColor = self.theDialColor.CGColor;
backGroundLayer.fillColor = [UIColor clearColor].CGColor;
[self.layer addSublayer:backGroundLayer];
畫時針
///
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(5, 0)];
[path addLineToPoint:CGPointMake(0, ViewR-(ViewR/2))];
[path addLineToPoint:CGPointMake(10, ViewR-(ViewR/2))];
[path closePath];
CAShapeLayer *hoursLayer = [CAShapeLayer layer];
hoursLayer.lineJoin = kCALineJoinRound;
hoursLayer.anchorPoint = CGPointMake(0.5, 1);
hoursLayer.position = CGPointMake(ViewWidth*0.5, ViewHeight*0.5);
hoursLayer.bounds = CGRectMake(0, 0, 10, ViewR-(ViewR/2));
hoursLayer.path = path.CGPath;
hoursLayer.strokeColor = self.hourColor.CGColor;
hoursLayer.fillColor = self.hourColor.CGColor;
[self.layer addSublayer:hoursLayer];
self.hoursLayer = hoursLayer;
畫分針
///
CGFloat weight = 8;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(weight/2, 0)];
[path addLineToPoint:CGPointMake(0, ViewR-(ViewR/10))];
[path addLineToPoint:CGPointMake(weight, ViewR-(ViewR/10))];
[path closePath];
CAShapeLayer *minuteLayer = [CAShapeLayer layer];
minuteLayer.anchorPoint = CGPointMake(0.5, 1);
minuteLayer.position = CGPointMake(ViewWidth*0.5, ViewHeight*0.5);
minuteLayer.bounds = CGRectMake(0, 0, weight, ViewR-(ViewR/10));
minuteLayer.path = path.CGPath;
minuteLayer.strokeColor = self.minuteColor.CGColor;
minuteLayer.fillColor = self.minuteColor.CGColor;
[self.layer addSublayer:minuteLayer];
self.minuteLayer = minuteLayer;
畫秒針
///
CATextLayer *textLayer = [CATextLayer layer];
textLayer.alignmentMode = @"center";
textLayer.anchorPoint = CGPointMake(0.5, 0.5);
textLayer.position = CGPointMake(ViewWidth*0.5, ViewHeight*0.5+40);
textLayer.bounds = CGRectMake(0, 0, 100, 20);
textLayer.string = @"00:00:00";
textLayer.fontSize = 12;
textLayer.foregroundColor = [UIColor blackColor].CGColor;
[self.layer addSublayer:textLayer];
self.textLayer = textLayer;
CGFloat weight = 6;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(weight/2, 0)];
[path addLineToPoint:CGPointMake(0, ViewR-(ViewR/40))];
[path addLineToPoint:CGPointMake(weight, ViewR-(ViewR/40))];
[path closePath];
CAShapeLayer *secondLayer = [CAShapeLayer layer];
secondLayer.anchorPoint = CGPointMake(0.5, 1);
secondLayer.position = CGPointMake(ViewWidth*0.5, ViewHeight*0.5);
secondLayer.bounds = CGRectMake(0, 0, weight, ViewR-(ViewR/40));
secondLayer.path = path.CGPath;
secondLayer.strokeColor =self.secondColor.CGColor;
secondLayer.fillColor = self.secondColor.CGColor;
[self.layer addSublayer:secondLayer];
self.secondLayer = secondLayer;
設定每秒重新整理時間
///
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *compoents = [calendar components:NSCalendarUnitSecond|NSCalendarUnitMinute|NSCalendarUnitHour fromDate:[NSDate date]];
CGFloat sec = compoents.second;
CGFloat minute = compoents.minute;
CGFloat hour = compoents.hour;
NSLog(@"%f:%f:%f",hour,minute,sec);
CGFloat secondA = sec * 6;
CGFloat minuteA = minute * 6;
CGFloat hourA = hour *30;
hourA += minute * 0.5;
self.secondLayer.transform = CATransform3DMakeRotation(angle2RADIAN(secondA), 0, 0, 1);
self.minuteLayer.transform = CATransform3DMakeRotation(angle2RADIAN(minuteA), 0, 0, 1);
self.hoursLayer.transform = CATransform3DMakeRotation(angle2RADIAN(hourA), 0, 0, 1);
self.textLayer.string = [NSString stringWithFormat:@"%02.f:%02.f:%02.f",hour,minute,sec];
如對您有幫助請具體可參考本文dome
備註:
如果有不足或者錯誤的地方還望各位讀者批評指正,可以評論留言,筆者收到後第一時間回覆。
QQ/微信:976971956/ljh976971956。
簡書號:江湖呼呼:[簡書]
感謝各位觀眾老爺的閱讀,如果覺得筆者寫的還湊合,可以關注或收藏一下,不定期分享一些好玩的實用的demo給大家。
文/江湖呼呼(簡書作者)
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。
相關文章
- canvas圓形時鐘效果Canvas
- CSS3圓形時鐘效果程式碼CSSS3
- SVG圓形鐘錶效果SVG
- JavaScript 動態圓形鐘錶JavaScript
- JavaScript動態圓形鐘錶效果詳解JavaScript
- canvas繪製圓形鐘錶程式碼例項Canvas
- 如何用CSS製作一個圓形放大鏡CSS
- CSS 繪製一個時鐘CSS
- 用GDI+旋轉多邊形來繪製一個時鐘摸擬小程式
- Flutter 圓形/圓角頭像Flutter
- Android 圓角、圓形 ImageView 實現AndroidView
- 動圖之一個圓繞另一個圓轉動
- 圓形視訊和圓角視訊的一種實現方式
- canvas 繪製圓形Canvas
- 如何使用原生技術寫一個倒數計時時鐘
- 圓形放大的hover效果
- SVG <circle> 繪製圓形SVG
- CSS圓形圖片效果CSS
- 一分鐘教程-超橢圓快速繪製
- Android 開發:glide圓角,圓形,效率問題AndroidIDE
- 大腦可以代替時鐘嗎, 一個有意思的話題. 準時的人肉鐘錶
- 實現一個CRDT工具庫——VClock 時鐘向量類
- 使用clay.js繪製一棵圓形樹JS
- Flutter 波浪圓形進度條Flutter
- CGContextRef處理圓形圖片GCContext
- Glide實現圓角圖片,以及圓形圖片IDE
- Python:一個鬧鐘Python
- 一個有趣的人形時鐘(幾年前的外掛了)
- 直播網站原始碼,Canvas實現圓形時間倒數計時進度條網站原始碼Canvas
- JavaScript時間輪盤:js元素圓形佈局製作時間輪盤動畫JavaScriptJS動畫
- CSS畫出半圓,四分之一圓,三角等圖形CSS
- Android自定義圓形進度條Android
- Android 圓形ProgressBar 改變顏色Android
- win10 時間顯示改成圓時鐘方法 win10怎麼改時間表盤Win10
- 20行程式碼寫一個簡單 Blazor 時鐘元件行程Blazor元件
- 請使用canvas畫一個橢圓Canvas
- [-Flutter 自定義元件-] 圓形進度條Flutter元件
- 如何用html實現圓形雲臺HTML