手写板制作----UITouch
2016-01-17 02:51 000aaa 阅读(205) 评论(0) 编辑 收藏 举报先写一下UITouch的功能和用法:
UITouch是view本身存在的协议。只需实现它的具体方法就行了。还有就是UIScrollView中没有UITouch这个事件,要想在UIScrollView中实现这个触摸事件,方法就是重写UIScrollView:
//手指接触到屏幕时触发
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
//手指在屏幕上移动的时候触发
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
//一次接触完成 手指离开屏幕的时候触发
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
//突然强制接触终止时触发(比如:不小心按到锁屏键,或者突然来电话了)
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
有了这些监听手指在屏幕上的位置的方法,就可以得到手指在屏幕上某个view中的位置
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self];//self 指一个view 现在是自动一个view }
在手指接触屏幕的时候就创建好用于装接下来记录点的容器
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { if (!m_pointArr) { m_pointArr = [NSMutableArray arrayWithCapacity:50]; } NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:100]; [m_pointArr addObject:mArr]; }
(每接触一次就创建一个动态数组用于装接下来手指划过的点)
在手指在屏幕上移动的时候将手指经过的点全部记录下来
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self]; PointObj *pObj = [[PointObj alloc]initWithX:point.x Y:point.y]; NSMutableArray *currentArr = [m_pointArr lastObject]; [currentArr addObject:pObj]; [self setNeedsDisplay];//这个方法的调用和让view自动调用他的 - (void)drawRect:(CGRect)rect
}
(这里的PointObj类 就是一个点类 生成一个点对象便于放在数组中/* 蛋疼得很这个 数组中只让方对象!!*/)
#import <Foundation/Foundation.h> @interface PointObj : NSObject @property(nonatomic,assign)float x,y; -(instancetype)initWithX:(float)P_X Y:(float)P_Y; @end
#import "PointObj.h" @implementation PointObj -(instancetype)initWithX:(float)P_X Y:(float)P_Y { if (self = [super init]) { _x = P_X; _y = P_Y; } return self; } @end
记录的点有了,下面就是将记录下来的点绘成图形啦 这里要用到quarz2d绘图那里的知识!!
- (void)drawRect:(CGRect)rect { if (m_pointArr && m_pointArr.count > 0) { for (NSMutableArray *item in m_pointArr) { if (item.count > 2) { PointObj *firstObj = [item firstObject]; CGContextRef ctf = UIGraphicsGetCurrentContext(); CGContextMoveToPoint(ctf, firstObj.x, firstObj.y); for (int i = 1; i < item.count; i++) { PointObj *oneObj = [item objectAtIndex:i]; CGContextAddLineToPoint(ctf, oneObj.x, oneObj.y); } CGContextSetFillColorWithColor(ctf, [UIColor blackColor].CGColor); CGContextSetLineWidth(ctf, 5.0); //线条的宽度 //将会出的线渲染出来 CGContextStrokePath(ctf); } } } }
到此为止 手写的功能已经实现啦!!
绘制的图形有了之后只需要通过截屏 或者区域截取又或者对一个View截取 得到一个Image的对象
下面介绍一些截屏的方法
1.直接截取一个view中的一部分区域 (如果直接传入view.bounds那就是直接截取这个view)
- (UIImage *)screenshotWithRect:(CGRect)rect; { UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale); CGContextRef context = UIGraphicsGetCurrentContext(); if (context == NULL) { return nil; } CGContextSaveGState(context); CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y); if( [self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) { [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO]; } else { [self.layer renderInContext:context]; } CGContextRestoreGState(context); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
2.对手机屏幕截屏
- (UIImage *)getImageWithFullScreenshot { // Source (Under MIT License): https://github.com/shinydevelopment/SDScreenshotCapture/blob/master/SDScreenshotCapture/SDScreenshotCapture.m#L35 BOOL ignoreOrientation = [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0?YES:NO; UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; CGSize imageSize = CGSizeZero; if (UIInterfaceOrientationIsPortrait(orientation) || ignoreOrientation) imageSize = [UIScreen mainScreen].bounds.size; else imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale); CGContextRef context = UIGraphicsGetCurrentContext(); for (UIWindow *window in [[UIApplication sharedApplication] windows]) { CGContextSaveGState(context); CGContextTranslateCTM(context, window.center.x, window.center.y); CGContextConcatCTM(context, window.transform); CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y); // Correct for the screen orientation if(!ignoreOrientation) { if(orientation == UIInterfaceOrientationLandscapeLeft) { CGContextRotateCTM(context, (CGFloat)M_PI_2); CGContextTranslateCTM(context, 0, -imageSize.width); } else if(orientation == UIInterfaceOrientationLandscapeRight) { CGContextRotateCTM(context, (CGFloat)-M_PI_2); CGContextTranslateCTM(context, -imageSize.height, 0); } else if(orientation == UIInterfaceOrientationPortraitUpsideDown) { CGContextRotateCTM(context, (CGFloat)M_PI); CGContextTranslateCTM(context, -imageSize.width, -imageSize.height); } } if([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:NO]; else [window.layer renderInContext:UIGraphicsGetCurrentContext()]; CGContextRestoreGState(context); } UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
文章到此就结束了,希望对路过的人有所帮助!!!!!