0829-0610画图
0829-0610modal&画图
--------------
path绘制四边形
// // NJView.m // 03-知识点补充 #import "NJView.h" @implementation NJView - (void)drawRect:(CGRect)rect { // 1.获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.绘制图形 /* // 设置起点 CGContextMoveToPoint(ctx, 10, 10); // 设置终点 CGContextAddLineToPoint(ctx, 100, 100); // 3.画圆 CGContextAddEllipseInRect(ctx, CGRectMake(50, 50, 50, 50)); */ // 2.创建路径(一个path就代表一条路径) // 但凡通过quarzt2d中的带有create/ copy / retain 方法创建出来的值都必须手动的释放 CGMutablePathRef path = CGPathCreateMutable(); // 设置起点 CGPathMoveToPoint(path, NULL, 10, 10); // 设置终点 CGPathAddLineToPoint(path, NULL, 100, 100); // 将路径添加到上下文中 CGContextAddPath(ctx, path); // 3.再创建一条路径用于保存圆 CGMutablePathRef path2 = CGPathCreateMutable(); // 在path中添加画的路径 CGPathAddEllipseInRect(path2, NULL, CGRectMake(50, 50, 50, 50)); CGContextAddPath(ctx, path2); // 3.渲染' CGContextStrokePath(ctx); // 释放前面创建的两条路径 CGPathRelease(path); CGPathRelease(path2); // 下面这种方式也可以释放路径 CFRelease(path); CFRelease(path2); } - (void)test { // 画四边形 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 1.第一种方式, 通过连接固定的点绘制四边形 // CGContextMoveToPoint(ctx, 0, 0); // CGContextAddLineToPoint(ctx, <#CGFloat x#>, <#CGFloat y#>) // CGContextAddLineToPoint(ctx, <#CGFloat x#>, <#CGFloat y#>) // CGContextAddLineToPoint(ctx, <#CGFloat x#>, <#CGFloat y#>) // CGContextAddLineToPoint(ctx, <#CGFloat x#>, <#CGFloat y#>) // 2.指定起点和宽高绘制四边形 // CGContextAddRect(ctx, CGRectMake(10, 10, 100, 100)); // CGContextStrokePath(ctx); // 3.两步合为一部 // CGContextStrokeRect(ctx, CGRectMake(10, 10, 100, 100)); // CGContextFillRect(ctx, CGRectMake(10, 10, 100, 100)); // 4.通过OC的方法绘制实心的四边形, 注意没有空心的方法 // UIRectFill(CGRectMake(10, 10, 100, 100)); // 5.通过绘制线条设置宽度 CGContextMoveToPoint(ctx, 10, 10); CGContextAddLineToPoint(ctx, 100, 100); CGContextSetLineWidth(ctx, 50); CGContextStrokePath(ctx); } @end
-------------
动画
1、设置定时器
2、方法里面 self setNeedsDisplay 立即执行drawRect
3、drawRect再次绘图
// NJView.m // 02-刷帧动画 #import "NJView.h" @interface NJView() @property (nonatomic, assign) int imageY; @end @implementation NJView -(void)awakeFromNib { NSLog(@"awakeFromNib"); // 创建CADisplayLink, 默认每秒60次 CADisplayLink *display = [CADisplayLink displayLinkWithTarget:self selector:@selector(updataImage)]; // 将CADisplayLink加入到消息循环中 [display addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; } /* - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { NSLog(@"initWithCoder"); // NSTimer一般用于定时的更新一些非界面上的数据 [NSTimer scheduledTimerWithTimeInterval: 0.1 target:self selector:@selector(updataImage) userInfo:nil repeats:YES]; // 创建CADisplayLink, 默认每秒60次 CADisplayLink *display = [CADisplayLink displayLinkWithTarget:self selector:@selector(updataImage)]; // 将CADisplayLink加入到消息循环中 [display addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; } return self; } */ - (void)updataImage { [self setNeedsDisplay]; } - (void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextClearRect(ctx, rect); self.imageY += 5; if (self.imageY > rect.size.height) { self.imageY = 0; } // Drawing code UIImage *image = [UIImage imageNamed:@"snow"]; [image drawAtPoint:CGPointMake(10, self.imageY)]; // [self setNeedsDisplay]; } @end
刷贞CADisplayLink 美妙60次数 //在绘图上比NSTimer好
清屏
-initWithCoder
//setNeesDisplay 重新drawRect
-awakeFromNib前还有一个方法 -initWithCoder也会被执行 【这2个是一起的 纯代码创建不会执行这2个方法】
ARC和非ARC都要release的地方
在quartz2d中 如果方法中有 Create 单词出现就必须release
//OC方法
//这个也可以释放C语言的方法
------------------
自定义UIImageView
1、新建FFImageView 继承 UIView
2、模仿UIImageView对象的示例步骤 分析下面一下就知道FFImageView还需要一个image属性才能显示 所以FFImageView里加一个 UIImage *image属性
3、模仿点击按钮图片切换功能:模仿UIImageView给image重新赋值的操作 分
析一下就知道系统给UIImageView的image赋值的时候 有set方法 set方法里面有 self setNeedsDisplay 推理出还有一个drawRect方法
所以我们这里需要充重写set方法 里面有[setNeedsPlay] 和 drawRect方法 //UIImageView没有重写drawRect方法 我们自己来写 根据文档提示说这里来修改
drawRect方法上description文档说明
Draws the receiver’s image within the passed-in rectangle.
The default implementation of this method does nothing
// NJView.h #import <UIKit/UIKit.h> @interface NJImageView : UIView @property (nonatomic, strong) UIImage *image; @end
// NJView.m // 04-自定义View模仿系统UIImageView #import "NJImageView.h" @implementation NJImageView - (void)drawRect:(CGRect)rect { // Drawing code [self.image drawInRect:rect]; // } -(void)setImage:(UIImage *)image { _image = image; [self setNeedsDisplay]; } @end
// // NJViewController.m // 04-自定义View模仿系统UIImageView #import "NJViewController.h" #import "NJImageView.h" @interface NJViewController () @property (nonatomic, weak) UIImageView *imageView; @property (nonatomic, weak) NJImageView *njIv; @end @implementation NJViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. /* UIImageView *iv = [[UIImageView alloc] init]; iv.image = [UIImage imageNamed:@"me"]; iv.frame = CGRectMake(100, 100, 100, 100); [self.view addSubview:iv]; self.imageView = iv; */ NJImageView *njIv = [[NJImageView alloc] init]; njIv.image = [UIImage imageNamed:@"me"]; njIv.frame = CGRectMake(100, 100, 100, 100); [self.view addSubview:njIv]; self.njIv = njIv; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 50, 50)]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [btn setTitle:@"点点大家的疯狂的教科书" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [self.view addSubview:btn]; } - (void)btnClick { NSLog(@"btnClick"); // 创建一张新的图片替换原有图片 UIImage *image = [UIImage imageNamed:@"psb.jpeg"]; // self.imageView.image = image; self.njIv.image = image; } @end
// // NJView.h // 04-自定义View模仿系统UIImageView #import <UIKit/UIKit.h> @interface NJImageView : UIView @property (nonatomic, strong) UIImage *image; @end
// // NJView.m // 04-自定义View模仿系统UIImageView #import "NJImageView.h" @implementation NJImageView - (void)drawRect:(CGRect)rect { // Drawing code [self.image drawInRect:rect]; } -(void)setImage:(UIImage *)image { _image = image; [self setNeedsDisplay]; } @end
----------------
(Bitmap)图片绘制并生成 用CGContext 和 UIGraphic开头即可
0、storyboard里拖一个UIImageView iv1 viewController - viewDidLoad方法里 准备绘制图片到 iv1里
1、开启图片上下文
2、获取当前上下文 ct1
3、画椭圆到ct1里
4、渲染展示(ct1)
5、获取上下文中的图片
6、获取图片的二进制数据
7、结束上下文 UIGraphicsEndImageContext();
7、二进制writeToFile写到桌面 桌面/abc.png
// // NJViewController.m #import "NJViewController.h" @interface NJViewController () @property (weak, nonatomic) IBOutlet UIImageView *iv; @end @implementation NJViewController - (void)viewDidLoad { [super viewDidLoad]; // -1.加载图片 // UIImage *image = [UIImage imageNamed:@"me"]; // 0.创建一个bitmap的上文 // CGBitmapContextCreate /* size :指定将来创建出来的bitmap的大小 opaque : YES:不透明 NO:透明 scale: 缩放比例 创建出来的bitmap就对应一个UIImage */ UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200) , NO , 0); // 1.获取bitmap上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.绘图 CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 100, 100)); // 3.渲染 CGContextStrokePath(ctx); // 4.获取生成的图片 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();//结束图片上下文 // 5.显示生成的图片到imageview self.iv.image = image; // 6.保存绘制好的图片到文件中 // 先将图片转换为二进制数据, 然后再将图片写到文件中 // UIImageJPEGRepresentation(image, 1) NSData *data = UIImagePNGRepresentation(image); [data writeToFile:@"/Users/apple/Desktop/abc.png" atomically:YES]; } @end
------
图片水印
// // NJViewController.m // 02-图片水印 #import "NJViewController.h" @interface NJViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation NJViewController - (void)viewDidLoad { [super viewDidLoad]; UIImage *oldImage = [UIImage imageNamed:@"scene"]; UIGraphicsBeginImageContextWithOptions(oldImage.size, NO, 0.0); [oldImage drawInRect:CGRectMake(0, 0, oldImage.size.width, oldImage.size.height)]; UIImage *waterImage = [UIImage imageNamed:@"logo"]; CGFloat waterW = 50; CGFloat waterH = 25; CGFloat waterX = oldImage.size.width - waterW - 5; CGFloat waterY = oldImage.size.height - waterH - 5; CGRect waterRect = CGRectMake(waterX, waterY, waterW, waterH); [waterImage drawInRect:waterRect]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); self.imageView.image = newImage; NSData *data = UIImagePNGRepresentation(newImage); [data writeToFile:@"/Users/imac/Desktop/CCC.png" atomically:YES]; } @end
原图片裁剪和保存
1、加载图片 开启图片上下文
2、上下文绘制一个椭圆 裁剪上下文 画图到上下文
3、从上下文获取图片 图片转成二进制数据 二进制数据输出到指定路径文件
- (void)test { // 0 .加载图片 UIImage *image = [UIImage imageNamed:@"me"]; // 1.创建图片上下文 UIGraphicsBeginImageContextWithOptions(image.size, NO, 0); // 2.指点可用范围 // 获取刚才创建的图片上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 画圆 CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, image.size.width, image.size.height)); // 指点可用范围 CGContextClip(ctx); // 3.绘图图片 [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; // 4.取出图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); self.iv.image = newImage; // 5.存储图片 // 2.将图片写到文件中 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"aaa.png"]; NSLog(@"%@", path); NSData *data = UIImagePNGRepresentation(newImage); [data writeToFile:path atomically:YES]; }
--------
颜色=背景色
图片平铺效果
1、绘图 拿到上下文的图片
2、获取图片颜色 [UIColor colorWithPatternImage:image]
3、设置视图view的contentView的背景色
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIColor *myColor = [UIColor colorWithPatternImage:image]; self.contentView.backgroundColor = myColor; //设置背景图片
- (void)viewDidLoad { [super viewDidLoad]; // // 1.生成一张以后用于平铺的小图片 CGSize size = CGSizeMake(self.view.frame.size.width, 44); UIGraphicsBeginImageContextWithOptions(size , NO, 0); // 2.画矩形 CGContextRef ctx = UIGraphicsGetCurrentContext(); CGFloat height = 44; CGContextAddRect(ctx, CGRectMake(0, 0, self.view.frame.size.width, height)); [[UIColor redColor] set]; CGContextFillPath(ctx); // 3.画线条 CGFloat lineWidth = 2; CGFloat lineY = height - lineWidth; CGFloat lineX = 0; CGContextMoveToPoint(ctx, lineX, lineY); CGContextAddLineToPoint(ctx, 320, lineY); [[UIColor blackColor] set]; CGContextStrokePath(ctx); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIColor *myColor = [UIColor colorWithPatternImage:image]; self.contentView.backgroundColor = myColor; }
-------------
view - 触摸事件
1、single application 新建FFOneView
2、 拖一个uiview放到控制器的view上 并指定类为FFOneView
3、重写父类UIController 的四个方法
4、勾选FFOneView的 multiple touch 可以支持多点触摸
5、touchesmoved:(NSSet *)touches WithEvent(UIEvent *)event
(
1、获取一个手指 UITouch touch = [touchs anyObject]
2、获取手指上一次触摸的位置CGRect prePositon = [touch previousLocationInView:self]; 和当前触摸位置
3、比较2次位置x 和 y移动距离距离
4、重新设置view的center值
)
// // NJView.m // 09-view拖拽 #import "NJView.h" @implementation NJView // 触摸事件完整的调用过程touchesBegan --> touchesMoved --> touchesEnded // 当手指触摸view的时候调用 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // [event allTouches] == touches // NSArray:用来保存同一种类型的有序的值 // NSSet: 用来保存无序的值 // NSArray *arr = @[@"1", @"2", @"3"]; // arr[2]; // NSSet set = @[@"2", @"3",@"1"]; // UITouch *touch = [touches anyObject]; // 获取手指触摸的位置 // 如果locationInView传递self, 将来获取出来的位置,是以自己的左上角为原点(00) // 如果传递的是父视图,将来获取出来的位置,是以父视图的左上角为原点(00) // CGPoint point = [touch locationInView:self.superview]; // NSLog(@"touchesBegan %@", NSStringFromCGPoint(point)); // 获取手指点击的次数 // NSLog(@"tapCount = %d", touch.tapCount); // NSLog(@"touchesBegan %p", touch); NSLog(@"%d", touches.count); } // 当手指在view上移动的时候调用 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { // UITouch *touch = [touches anyObject]; // NSLog(@"touchesMoved %p", touch); UITouch *touch = [touches anyObject]; // 0. 获取上一次的位置 CGPoint prePoint = [touch previousLocationInView:self]; // NSLog(@"prePoint = %@", NSStringFromCGPoint(prePoint)); // 1.获取当前的位置 CGPoint currentPoint = [touch locationInView:self]; // NSLog(@"currentPoint = %@", NSStringFromCGPoint(currentPoint)); CGFloat moveX = currentPoint.x - prePoint.x; CGFloat moveY = currentPoint.y - prePoint.y; // NSLog(@"moveX = %.1f", moveX); // 2.改变当前视图的位置,为手指指定的位置 CGPoint temp = self.center; // NSLog(@"%f,%f",self.bounds.origin.x,self.bounds.origin.y); temp.x += moveX; temp.y += moveY; self.center = temp; } // 当手指离开view的时候调用 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { // UITouch *touch = [touches anyObject]; // NSLog(@"touchesEnded %p", touch); } // 当触摸事件被打断的时候调用 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { } @end
[touch previousLocationInView:self]; [touch currentLocationInView:self];
UITouch t = [touches anyObject]; //无序 一根手指 touchesbegain move ended事件里 t都是同一个对象
[touch locationInView:self];//获取手指在某个view中的坐标位置
touchesBegin touchesMoved touchesEnded(手指移开)
NSSet 存入无序 NSArray存入有序
1根手指对应一个UITouch对象t(不用写* 已经被typedef UITouch * UITouch了) 手指移动时 对象的的属性值更新 cancel离开屏幕会销毁这个对象
t.tapCount这个属性不建议用
touches.count = 手指对象数量
事件:触摸 加速 远程控制
继承UIResponder 对象"响应者对象"才能接收并处理事件
响应者对象:UIApplication UIViewController UIView
触摸事件:touchesBegin touchesMoved touchesEnded(手指移开) : withEvent:事件来相应接收的事件
加速事件:motionBegin motionended motioncancelled(被电话打断)
远程控制事件:remoteControlReceiveReceivedWithEvent
2根手指同时触摸 只有一次touch事件 一次touchbegain touches参数中有2个UITouch对象(手指)
UIEvent 事件对象 记录事件 类型和产生时间
属性类型有 type (3个枚举值) subType(每个类型的细分类型 枚举) 时间属性 timeStamp
bitmap 上下文和UIView没有关系
-------------
其他不重要的:
swift
新建commandLine 选择语言 swift 新建playgound可以实时的看到变量的值