UI进阶之模拟时钟指针案例
来源:互联网 发布:cog数据库官网 编辑:程序博客网 时间:2024/06/14 02:30
前言:在看到这个案例后,自己先要对它产生基本的认知.就是做这个案例需要用到那些知识,先在脑海里产生一个框架,以及对于一些知识的运用,知识点的串联.还有就是看到案例之后,不要盲目的去敲代码,要先理清思路,只要思路顺了,代码很快就可以搞定.
CALayer介绍
1.在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮、一个文本框、一个Label,一 个图片等等,这些都是UIView.
2.其实 UIView之所以能够显示到屏幕上面,完全是因为他内部存在图层Layer.
3.在创建UIView时,UIView会自动创建一个图层(Layer),就是UIView的Layer属性.
4.当UIView需要显示到屏幕上面时,系统会自动调用drawRect方法进行绘图,并且会将所有内容绘制在自己的图层 上,绘图完毕后,系统会将图层拷贝到屏幕上,于是就完成了UIView的显示.
5.UIView本身不具备显示功能,是它内部的层才有显示功能.
6.CALayer的常见属性:
@property CGFloat borderWidth;//边框的宽度
@property CGColorRef borderColor;//边框的颜色
@property CGColorRef backgroundColor; //背景颜色
@property float opacity;//透明度
@property CGColorRef shadowColor; //阴影颜色
@property float shadowOpacity;//阴影透明度
@property CGSize shadowOffset;//阴影偏移量
@property CGFloat shadowRadius;//阴影半径
@property(strong) id contents;//CALayer包含的内容(图片之类的)
@property CGFloat cornerRadius;
@property CGRect bounds;
@property CGPoint position;
@property CGPoint anchorPoint;//定位点,锚点
@property CATransform3D transform;
@property(getter=isHidden) BOOL hidden;
@property(readonly) CALayer *superlayer;
@property(copy) NSArray *sublayers;
@property BOOL masksToBounds;//是否裁剪边框
7.使用 CALayer的”定位点(锚点)”实现一个时钟动画(通过 UIView 来实现,目的是为了说明控件的 center 其实就是 CALayer的 position, 通过修改了 position其实就修改了 center 的含义了)
* 注意: 在使用 Quartz2D 绘图的时候, 对绘图上下文的旋转是对坐标系的旋转, 通过 UI 控件的 transform 对控件做旋转是按照 Center 来旋转的。
* 注意: 控件的 center 属性, 其实就是对应的 CALayer的 postion。所以控件的 center并不是永远表示控件的中心点。
- 解决不准确的问题:(我试验后延迟了一秒,很不容易发现)
• 通过 NSTimer 实现的动画可能造成卡顿、不连贯的情况(NSTimer 不准确)
• CADisplayLink 表示”显示连接”, 与显示器的刷新频率相关。
• 显示器每秒钟刷新60次(60HZ)(电视新闻上拍的电脑显示器就不清楚,原因刷新频率不一样)
• 通过 CADisplayLink 来解决
不说那么多啦,直接上代码
#import "ViewController.h"@interface ViewController ()@property (nonatomic, weak) UIView * secondView;//秒针@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //1.创建表盘 CALayer * clockLayer = [CALayer layer]; //1.1设置大小和位置 clockLayer.bounds = CGRectMake(0, 0, 150, 150); clockLayer.position = self.view.center; //1.2设置内容图片(contents包含图片,先设置[UIImage imageNamed:@"clock"].CGImage,这时系统会提示 Implicit conversion of C pointer type 'CGImageRef _Nullable' (aka 'struct CGImage *') to Objective-C pointer type 'id _Nullable' requires a bridged cast,这时不要害怕,就点击__bridge id _Nullable,意思是需要进行桥连,就OK啦); clockLayer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"clock"].CGImage); //1.3把时钟图层添加到视图上面 [self.view.layer addSublayer:clockLayer]; //2.创建秒针 UIView * secondView = [[UIView alloc] init]; //2.1设置秒针的大小和位置 secondView.bounds = CGRectMake(0, 0, 1, 50); secondView.center = self.view.center; //2.2修改锚点(一般锚点默认为CGPointMake(0.5,0.5),这里需要改变y值即可) secondView.layer.anchorPoint = CGPointMake(0.5, 1); //2.3设置颜色 secondView.backgroundColor = [UIColor redColor]; [self.view addSubview:secondView]; self.secondView = secondView; //开启定时器// NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(fire) userInfo:nil repeats:YES];// [timer fire]; //3.连接CADispalyLink CADisplayLink * link = [CADisplayLink displayLinkWithTarget:self selector:@selector(fire)]; //3.1添加到主运行循环 [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];}//触发方法- (void) fire{ //1.获取当前日历(类方法 + (NSCalendar *)currentCalendar; // user's preferred calendar用户推荐的日历) NSCalendar * currentCalendar = [NSCalendar currentCalendar]; //2.获取当前时间 NSDate * currentDate = [NSDate date]; //3.获取时间的一部分 NSDateComponents * components = [currentCalendar components:NSCalendarUnitSecond fromDate:currentDate]; //4.获取当前秒 NSInteger second = components.second; //5.获取当前秒针要旋转的弧度 CGFloat angle = 2 * M_PI * second / 60 ; //6.让秒针转起来// self.secondView.transform = CGAffineTransformRotate(self.secondView.transform, angle); self.secondView.transform = CGAffineTransformMakeRotation(angle);}
这是我运行的截图
*搭建完时钟后的截图
*搭建完秒针后的截图
*全部搞定后的截图
- UI进阶之模拟时钟指针案例
- javascript 案例:模拟时钟
- PyQt5 - QWidgets部件进阶教程之模拟时钟
- JavaScript案例之时钟
- 练习之模拟时钟
- android 进阶之UI
- python编程之 模拟时钟
- 指针进阶之指针运算
- VB之旅-指针时钟
- GDI绘图之指针时钟
- STM32F0项目进阶之实时时钟DS1307
- Android初级进阶之自定义时钟(一)
- Android初级进阶之自定义时钟(二)
- Android之模拟时钟和数字时钟,计时器
- c语言之模拟时钟秒表
- 原创的--------《windows 程序设计》之模拟时钟
- html5之canvas实现模拟简易时钟
- 模拟时钟
- 你好,色彩 android:background="@color/white" [create file color.xml at res/values/]
- Android Studio的使用01-基本设置教程
- Servlet 3特性:异步Servlet
- java中代理模式总结
- 新手学Android开发经常出现的问题
- UI进阶之模拟时钟指针案例
- UITableView最上面tableHeaderView留空间的两种方法
- [letecode Java] Surrounded Regions
- android中LinearGradient线性渐变
- radio 性别(可取消选择)
- 可以选择item的listview
- maven打包去掉测试的部分pom.xml配置
- 10009---jQuery效果--滑动
- 行为识别(action recognition)相关资料