- Quatz2D 可以绘制控件
- 图形上下文CGContextRef 保存绘图信息 绘图状态 决定绘制到哪里去
- - (void)drawRect:(CGRect)rect{ } view有多大 rect就有多大
- 获取图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
- 画线
2.1 确定起点
CGContextMoveToPoint(ctx, 10, 30);
2.2 添加一条线
CGContextAddLineToPoint(ctx, 50, 100);
2.3 渲染出图像
CGContextStrokePath(ctx); 空心的图像
CGContextFillPath; 填充的图形
- 画矩形
CGContextAddRect(ctx, CGRectMake(100, 100, 50 , 50));
- 设置线条宽度
CGContextSetLineWidth(ctx, 10);
- 设置线条颜色
5.1 通过c语言来设置 CGContextSetRGBStrokeColor (ctx, r, g, b, a); 233.0/255.0f
Stroke 只能配合stroke 如果用fill 是没有效果的
CGContextSetRGBFillColor
5.2 通过oc设置
[[UIColor redColor] setFill];
[[UIColor redColor] setStroke];
[[UIColor redColor] set] set是代表都有效果
- 设置线条的头圆
6.1 CGContextSetLineCap(ctx, kCGLineCapRound);
6.2 设置线之间链接线条的节点的样式
CGContextSetLineJoin(kCGLineJoinRound);
- 画椭圆
CGContextAddEllipseInRect(ctx, CGRectMake());
- 画圆弧
CGContextAddArc(ctx, 圆心x, 圆心y, 半径, 开始的角度, 结束的角度, 顺时针还是逆时针);
0 顺时针 1 逆时针
Fill 只会填充起始和终点, 并不会看点
- 闭合路径
CGContextClosePath(ctx);
- 画文字不需要上下文
10.1 设置文字
NSString *word = @“我们饿了, 要吃饭”;
10.2 画文字
withAttribute 用来表示文字的属性 字典!
[word drawAtPoint:(CGPoint)withAttributes:();
nsMutableDictionary *dict = [NSMutableDictionary dictionary]
Dict[NSForegroundColorAttributeName] = [UIColor redColor];
Dict[NSFontAttributeName] = [UIFont systemFontOfSize:14];
- [word drawInRect:(cgrect) withAttributes:]
超出范围不显示 自动换行
[word drawWithRect: options: attributes]
- 画图片不需要上下文
UIImage *image = [UIImage imageNamed:]
不会拉伸图片
[image drawAtPoint:CGPointMake(100, 100)];
会根据设定的范围拉伸图片
[image drawInRect:CGRectMake(10, 10, 100, 100)];
进行平铺展示
[image drawAsPatternInRect:rect]
- 贝塞尔曲线
13.1 设置控制点
13.2 设置起始点
13.3 设置终点
CGContextAddQuadCurveToPoint(ctx,控制点X, 控制点Y ,结束X,结束Y);
- 如果只是绘制线条必须使用stroke
- 让下一次使用在此是默认样式
CGContextSaveGState(ctx);
开启一个上下文栈 copy的上下文放在栈中 保存的样式存在栈中
渲染图形的效果始终是根据上下文栈顶的上下文来确定
再画第二条线的时候 恢复原来的上下文,放在上下文栈中
CGContextRestoreGState
- 矩阵旋转操作
矩阵操作要放在图形的上面
CGContextRotateCTM(ctx, M_PI_4); 旋转操作
上下文的操作缩放
CGContextScaleCTM(ctx, 0.5, 1);
上下文的平移
CGContextTranslateCTM(ctx, 100, 200);
- 裁剪当前图片
绘制图形 之后裁剪图片
CGContextClip();
- drawRect: (CGRect)rect: 这个方法不能直接调用
[self.arcView setNeedsDisplaty];
[self setNeedsDisplaty];
[self setNeedsDisplayInRect:];
- 为什么控件用weak 是因为 weak 可以不让计数器增加, 利于提高性能
- 为什么用nonatomic 是因为手机端重性能轻安全性
- 圆的增大方向是顺时针
- Storyboard 和 xib默认是归档在程序中, 当使用的时候需要先解档。
他就会调用 -(instancetype)initWithCoder:(NSCoder *)aDecoder{}
然后就会转换成代码
awakeFromNib 这个事加载xib的时候调用 先解档再调用这个
- storyboard的本质是xib
- Nstimer 通常是 每隔一秒以上调用某一个方法的时候使用
- CADisplayLink 他可以调用一秒钟调用60次
- CADisplayLink *display = [CADsiplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];
[Display addToRunLoop:[NSRunLoop mainRunLoop] forMode:]
- UI层次只有一条线程 图片轮播器只有一条线程
- 上下文保存 线条的属性 和 具体
- C语言 在oc nil 在c 传NULL
CGMutablePath path = CGPathCreateMutable();
CGPathMoveToPoint (path, NULL, 100, 100);
CGPathAddLineToPoint(path, NULL, 200, 300);
CGContextAddPath(ctx, path);
- arc是编译器特性, 是在对应的位置添加retain release,autoRelease。
不过这是针对oc对象, 智能管理oc对象的内存
如果是c语言需要手动管理内存
只要在c中出现create, copy, retain, 都需要释放内存
CGPathRealease(path); corefundation也可以使用CFRelease;
- Analyze 是上线的时候, 我们需要进行内存检测。
- 两步合一步CGContextStrokeRect(ctx, CGRectMake(100, 100, 100, 50));
- UIButton *btn = [uibutton buttonwithtype];
- UIGraphicsBeginImageContextWithOptions(CGSize size, 不透明 == YES , 是否被压缩 0 标示不压缩);
- NSArray/NSDictionary/NSString/NSData
- 图片要写入文件要转成2进制
CGFloat compressionQuality: 标示图片质量,压缩值
NSData *imageData = [UIImageJPEGRepresentation(image, 压缩至)
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:@""可以拖文件夹 atomically:YES];
- 类方法一般比对象方法快 方法中没有访问成员变量的时候就可以使用类方法
- 抽取工具类方法 要找对应的类 可以写Extension 或者Help
- 截屏
UIGraphicBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]
UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
UIgraphicsEndImageContext();
NSData *clipsImageData = UIImagePnGRepresentation(clipImage);
[clipImageData writeToFile:@"/User --- "automically: YES];
- 保存到相册中就不需要转成2进制
- UIImageWriteToSavedPhotosAlbum(clipImage, self, @selector(这个点进去有的))
在这个方法中判断 if(error) 如果是真就保存失败
- 三大事件 触摸事件 加速计事件 远程控制事件
- UIApplication UIVIEWcONTROLLER uivgIEW 都继承自UIResponder 都能处理事件
- touchesBegan touchesMoves touchesEnded touchesCancles
- motionBegan motionEnded motionCancelled
- 点击次数 tabCount 触摸了多少秒 timestamp
- Nsset 存储无序的不同的对象
NSArry 存储有序的可以重复的对象
NSDictionary:用键值对来存储数据
- 一次触摸事件获取的手指是一样的
- 获取当前触摸点的位置 CGPoint currentPoint = [touch locationInView:self];
获取上一次的触摸点的位置[touch previousLocationInView:self];