1.画线步骤
- (void)drawRect:(CGRect)rect {
// Drawing code
//1.获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//2.拼接路径
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10, 10)];
[path addQuadCurveToPoint:CGPointMake(100, 10) controlPoint:CGPointMake(50, 100)];
//3.把路径添加到上下文
// 直接把UIKit的路径转换成CoreGraphics,CG开头就能转
CGContextAddPath(context, path.CGPath);
//设置线框
CGContextSetLineWidth(context, 10);
//设置线帽
CGContextSetLineCap(context, kCGLineCapRound);
//设置颜色
[[UIColor redColor] set];
//4.渲染上下文到视图
CGContextStrokePath(context);
}
2.自定义控件weak,必须设置frame,否则显示不出来。
@property(nonatomic,weak) UILabel *label;
-(UILabel *)label{
if (_label == nil) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor redColor];
[self addSubview:label];
_label = label;//需要先设置label,最后在赋给_label,因为是weak
}
return _label;
}
3.drawRect: 当视图显示的时候,就会调用,默认是调用一次
[self setNeedsDisplay];//重新绘制,在view上做一个重绘的标记,当下次屏幕刷新的时候,就会调用drawRect。
4.instancetype & id的比较
(1) instancetype在类型表示上,跟id一样,可以表示任何对象类型
(2) instancetype只能用在返回值类型上,不能像id一样用在参数类型上
(3) instancetype比id多一个好处:编译器会检测instancetype的真实类型
5.xib和storyboard的比较,一个轻量级一个重量级。
共同点:都用来描述软件界面; 都用Interface Builder工具来编辑
不同点: Xib是轻量级的,用来描述局部的UI界面; Storyboard是重量级的,用来描述整个软件的多个界面,并且能展示多个界面之间的跳转关系
6.imageView默认的是不允许用户交互的
7.Images.xcassets中的素材
(1)只支持png格式的图片
(2) 图片只支持[UIImage imageNamed]的方式实例化,但是不能从Bundle中加载
(3) 在编译时,Images.xcassets中的所有文件会被打包为Assets.car的文件
8.两个定时器
第一个:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateImage) userInfo:nil repeats:YES];
说明: NSTimer一般用于定时的更新一些非界面上的数据,告诉多久调用一次
第二个:
CADisplayLink *display= [CADisplayLink displayLinkWithTarget:self selector:@selector(updateImage)];
[display addToRunLoop:[NSRunLoopmainRunLoop] forMode:NSDefaultRunLoopMode];
说明: CADisplayLink刷帧,默认每秒刷新60次。该定时器创建之后,默认是不会执行的,需要把它加载到消息循环中
-(void)awakeFromNib{
//NSTimer一般用于定时的更新一些非界面上的数据,告诉多久调用一次这里使用会出现卡顿的现象
// [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];
// CADisplayLink刷帧,默认每秒刷新60次。该定时器创建之后,默认是不会执行的,需要把它加载到消息循环中
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];
[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}