[IOS]手写板
来源:互联网 发布:微软最牛程序员 编辑:程序博客网 时间:2024/05/09 12:48
手写板
Demo:http://download.csdn.net/detail/u012881779/8615333
绘图板视图控制器:
#import "NotePadDrawViewController.h"#import "NoteDrawPadView.h"@implementation NotePadDrawViewController- (void)viewDidLoad{ [super viewDidLoad]; CGFloat Height = [UIScreen mainScreen].bounds.size.height; CGFloat width = [UIScreen mainScreen].bounds.size.width; CGRect rect = self.view.frame; rect.size.width = width; rect.size.height = Height - 64; if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) { rect.origin.y = 44; } else { rect.origin.y = 64; } NSArray* array = [[NSBundle mainBundle] loadNibNamed:@"NoteDrawPadView" owner:nil options:nil]; NoteDrawPadView *drawPad = (NoteDrawPadView*)[array objectAtIndex:0]; drawPad.frame = rect; drawPad.drawPath = self.drawItemPath; drawPad.pushVC = self; [drawPad showInView:self.view]; [self.view bringSubviewToFront:drawPad];}- (void)dealloc{ self.drawItemPath = nil;}- (IBAction)clickBackBtn:(id)sender { [self.navigationController popViewControllerAnimated:YES];}@end绘图板视图:
#import "NoteDrawPadView.h"#import <QuartzCore/QuartzCore.h>#import "NoteDrawPadItemView.h"#import "AppDelegate.h"NSString* DrawPadSaveNotification = @"DrawPadSaveNotification";static CGFloat ToolbarHeight = (48.0f);@interface NoteDrawPadView ()@property (retain, nonatomic) IBOutlet UIImageView *mBackImageView;@property (retain, nonatomic) IBOutlet UIView *mToolPanel;@property (retain, nonatomic) IBOutlet UIView *mColorPanel;@property (retain, nonatomic) IBOutlet UIView *mSizePanel;@property (retain, nonatomic) IBOutlet UIButton *mToolSelectedBtn;@property (retain, nonatomic) IBOutlet UIButton *mColorSelectedBtn;@property (retain, nonatomic) IBOutlet UIButton *mSizeSelectedBtn;@property (retain, nonatomic) IBOutlet UIButton *mToolCancelBtn;@property (retain, nonatomic) IBOutlet UILabel *mFenGeXian;@property (retain, nonatomic) NoteDrawPadItemView *mItemPadView;@end@implementation NoteDrawPadView// iOS7+ (UIImage*)imageFromKeyWindow:(CGRect)rect{ AppDelegate *APP = (AppDelegate *)[[UIApplication sharedApplication] delegate]; UIWindow *keyWindow = [APP window]; UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale); CGContextRef context = UIGraphicsGetCurrentContext(); if ([keyWindow respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) { [keyWindow drawViewHierarchyInRect:keyWindow.bounds afterScreenUpdates:YES]; } else { [keyWindow.layer renderInContext:context]; } UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;}+ (UIImage*)imageFromView:(UIView*)item clipRect:(CGRect)rect{ UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale); CGContextRef context = UIGraphicsGetCurrentContext(); // iOS7 if ([item respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) { [item drawViewHierarchyInRect:CGRectMake(0, 0, rect.size.width, rect.size.height) afterScreenUpdates:YES]; } else { [item.layer renderInContext:context]; } CGContextSaveGState(context); UIRectClip(rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;}+ (UIImage*)imageFromView:(UIView*)item{ UIGraphicsBeginImageContextWithOptions(item.frame.size, NO, [UIScreen mainScreen].scale); CGContextRef context = UIGraphicsGetCurrentContext(); // iOS7 if ([item respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) { [item drawViewHierarchyInRect:item.bounds afterScreenUpdates:YES]; } else { [item.layer renderInContext:context]; } CGContextSaveGState(context); UIRectClip(item.frame); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;}- (void)showInView:(UIView *)view{ [view addSubview:self]; if (self.drawPath.length != 0) { NSData* data = [NSData dataWithContentsOfFile:self.drawPath]; UIImage* image = [UIImage imageWithData:data]; self.mBackImageView.image = image; } CGRect rect = self.bounds; rect.size.height -= ToolbarHeight; _mItemPadView = [[NoteDrawPadItemView alloc] initWithFrame:rect]; [self insertSubview:_mItemPadView atIndex:1]; rect.origin.x = 0; rect.origin.y = self.bounds.size.height-ToolbarHeight; rect.size.height = ToolbarHeight; //工具面板 self.mToolPanel.frame = rect; [self insertSubview:self.mToolPanel atIndex:3]; //颜色面板 self.mColorPanel.frame = rect; [self insertSubview:self.mColorPanel atIndex:2]; //粗细面板 self.mSizePanel.frame = rect; [self insertSubview:self.mSizePanel atIndex:2];}- (void)showInKeyWindow{ self.mToolCancelBtn.hidden = NO; if (self.drawPath.length != 0) { NSData* data = [NSData dataWithContentsOfFile:self.drawPath]; UIImage* image = [UIImage imageWithData:data]; self.mBackImageView.image = image; } CGRect rect = self.bounds; rect.size.height -= ToolbarHeight; _mItemPadView = [[NoteDrawPadItemView alloc] initWithFrame:rect]; [self insertSubview:_mItemPadView atIndex:1]; rect.origin.x = 0; rect.origin.y = self.bounds.size.height-ToolbarHeight; rect.size.height = ToolbarHeight; //工具面板 self.mToolPanel.frame = rect; [self insertSubview:self.mToolPanel atIndex:3]; //颜色面板 self.mColorPanel.frame = rect; [self insertSubview:self.mColorPanel atIndex:2]; //粗细面板 self.mSizePanel.frame = rect; [self insertSubview:self.mSizePanel atIndex:2]; UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; self.transform = CGAffineTransformIdentity; if (UIInterfaceOrientationIsLandscape(orientation)) { self.mFenGeXian.hidden = NO; if (orientation == UIInterfaceOrientationLandscapeRight) { self.transform = CGAffineTransformMakeRotation((90 / 180.0) * M_PI); } else { self.transform = CGAffineTransformMakeRotation((270 / 180.0) * M_PI); } self.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); } else { self.bounds = [UIScreen mainScreen].bounds; } AppDelegate *APP = (AppDelegate *)[[UIApplication sharedApplication] delegate]; [[APP window] addSubview:self];}- (void)showInKeyView:(UIView *)keyView{ self.mToolCancelBtn.hidden = NO; CGRect rect = self.bounds; rect.size.height -= ToolbarHeight; _mItemPadView = [[NoteDrawPadItemView alloc] initWithFrame:rect]; [self insertSubview:_mItemPadView atIndex:1]; rect.origin.x = 0; rect.origin.y = self.bounds.size.height-ToolbarHeight; rect.size.height = ToolbarHeight; //工具面板 self.mToolPanel.frame = rect; [self insertSubview:self.mToolPanel atIndex:3]; //颜色面板 self.mColorPanel.frame = rect; [self insertSubview:self.mColorPanel atIndex:2]; //粗细面板 self.mSizePanel.frame = rect; [self insertSubview:self.mSizePanel atIndex:2]; UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsLandscape(orientation)) { self.mFenGeXian.hidden = NO; CGRect rect = [UIScreen mainScreen].bounds; rect.size.height = rect.size.width - 20; rect.size.width = [UIScreen mainScreen].bounds.size.height; if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) { rect.origin.y = 0; } else { rect.origin.y = 20; } self.frame = rect; } else { self.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-20); } [keyView addSubview:self];}-(void)showInKeyViewWithMySelf:(UIView *)keyView rect:(CGRect)mrect{ self.mToolCancelBtn.hidden = NO; self.frame = mrect; [_mBackImageView setImage:[UIImage imageWithData:_imgedata]]; CGRect rect = self.bounds; rect.size.height -= ToolbarHeight; _mItemPadView = [[NoteDrawPadItemView alloc] initWithFrame:rect]; [self insertSubview:_mItemPadView atIndex:1]; rect.origin.x = 0; rect.origin.y = self.bounds.size.height-ToolbarHeight; rect.size.height = ToolbarHeight; //工具面板 _mToolPanel.frame = rect; [self insertSubview:_mToolPanel atIndex:3]; //颜色面板 _mColorPanel.frame = rect; [self insertSubview:_mColorPanel atIndex:2]; //粗细面板 _mSizePanel.frame = rect; [self insertSubview:_mSizePanel atIndex:2]; [keyView addSubview:self];}- (void)layoutSubviews{ [super layoutSubviews]; UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsLandscape(orientation)) { CGRect rect = [UIScreen mainScreen].bounds; rect.size.height = rect.size.width - 20; rect.size.width = [UIScreen mainScreen].bounds.size.height; if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) { rect.origin.y = 0; } else { rect.origin.y = 20; } self.frame = rect; }}- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { } return self;}- (void)dealloc{ [_mBackImageView setImage:nil]; _mBackImageView = nil; _mToolPanel = nil; _mColorPanel = nil; _mSizePanel = nil; _mToolSelectedBtn = nil; _mColorSelectedBtn = nil; _mSizeSelectedBtn = nil; _mToolCancelBtn = nil; _mFenGeXian = nil; _mItemPadView=nil; _drawPath =nil; _mTagstring = nil; _imgedata = nil; [super dealloc];}//点击工具栏面板- (IBAction)clickToolPanelBtn:(id)sender { __block typeof(self) weakself = self; NSInteger tag = [sender tag]; if (tag < 38) { self.mToolSelectedBtn.selected = NO; self.mToolSelectedBtn = (UIButton*)sender; self.mToolSelectedBtn.selected = YES; } switch (tag) { case 31: //颜色板 { if (_mSizePanel.alpha == 1.0) { CGRect sizeRect =_mSizePanel.frame; sizeRect.origin.y += ToolbarHeight; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ weakself.mSizePanel.alpha = 0.0; weakself.mSizePanel.frame = sizeRect; } completion:^(BOOL finished) { if (finished) { CGRect colorRect = weakself.mColorPanel.frame; colorRect.origin.y -= ToolbarHeight; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ weakself.mColorPanel.alpha = 1.0; weakself.mColorPanel.frame = colorRect; } completion:^(BOOL finished) { }]; } }]; } else { CGRect colorRect = _mColorPanel.frame; if (_mColorPanel.alpha == 0.0) { colorRect.origin.y -= ToolbarHeight; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ weakself.mColorPanel.alpha = 1.0; weakself.mColorPanel.frame = colorRect; } completion:^(BOOL finished) { }]; } else { colorRect.origin.y += ToolbarHeight; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ weakself.mColorPanel.alpha = 0.0; weakself.mColorPanel.frame = colorRect; } completion:^(BOOL finished) { }]; } } //设置最后一次颜色 [_mItemPadView setLineStrokeColor:_mItemPadView.latestColor]; } break; case 32: //粗细板 { if (_mColorPanel.alpha == 1.0) { CGRect colorRect = _mColorPanel.frame; colorRect.origin.y += ToolbarHeight; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ weakself.mColorPanel.alpha = 0.0; weakself.mColorPanel.frame = colorRect; } completion:^(BOOL finished) { if (finished) { CGRect sizeRect = weakself.mSizePanel.frame; sizeRect.origin.y -= ToolbarHeight; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ weakself.mSizePanel.alpha = 1.0; weakself.mSizePanel.frame = sizeRect; } completion:^(BOOL finished) { }]; } }]; } else { CGRect sizeRect = _mSizePanel.frame; if (self.mSizePanel.alpha == 0.0) { sizeRect.origin.y -= ToolbarHeight; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ weakself.mSizePanel.alpha = 1.0; weakself.mSizePanel.frame = sizeRect; } completion:^(BOOL finished) { }]; } else { sizeRect.origin.y += ToolbarHeight; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ weakself.mSizePanel.alpha = 0.0; weakself.mSizePanel.frame = sizeRect; } completion:^(BOOL finished) { }]; } } //设置最后一次颜色 [_mItemPadView setLineStrokeColor:_mItemPadView.latestColor]; } break; case 33: //橡皮擦 { if (self.mColorPanel.alpha == 1.0) { CGRect colorRect = _mColorPanel.frame; colorRect.origin.y += ToolbarHeight; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ weakself.mColorPanel.alpha = 0.0; weakself.mColorPanel.frame = colorRect; } completion:^(BOOL finished) { }]; } else if (self.mSizePanel.alpha == 1.0) { CGRect sizeRect = _mSizePanel.frame; sizeRect.origin.y += ToolbarHeight; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ weakself.mSizePanel.alpha = 0.0; weakself.mSizePanel.frame = sizeRect; } completion:^(BOOL finished) { }]; } [_mItemPadView setLineStrokeColor:-1]; } break; case 34: //清除屏幕 { if (self.mColorPanel.alpha == 1.0) { CGRect colorRect = self.mColorPanel.frame; colorRect.origin.y += ToolbarHeight; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ weakself.mColorPanel.alpha = 0.0; weakself.mColorPanel.frame = colorRect; } completion:^(BOOL finished) { }]; } else if (_mSizePanel.alpha == 1.0) { CGRect sizeRect = _mSizePanel.frame; sizeRect.origin.y += ToolbarHeight; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ weakself.mSizePanel.alpha = 0.0; weakself.mSizePanel.frame = sizeRect; } completion:^(BOOL finished) { }]; } UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:@"你确定要清除画布吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; alertView.tag = 1212; [alertView show]; } break; case 38: //取消保存 { if(_mTagstring){ [[NSNotificationCenter defaultCenter] postNotificationName:@"NSaddPersonListviewNotifications" object:nil userInfo:nil]; }else{ [self removeFromSuperview]; } } break; case 39: //保存画板 { if(_mTagstring==nil){ if (!_mToolCancelBtn.hidden) {//表示内文页的保存 [_mToolPanel removeFromSuperview]; [_mColorPanel removeFromSuperview]; [_mSizePanel removeFromSuperview]; CGRect rect = [UIScreen mainScreen].bounds; if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) { rect.origin.y = 0; } else { rect.origin.y = 20; } UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsLandscape(orientation)) { rect.size.height = rect.size.width-(0+20); rect.size.width = [UIScreen mainScreen].bounds.size.height; } else { rect.size.height -= (0+20); rect.size.width = [UIScreen mainScreen].bounds.size.width; } UIImage* image = [NoteDrawPadView imageFromView:_pushVC.view clipRect:rect]; NSData* data = UIImageJPEGRepresentation(image, 1.0); NSString* filePath = [NSString stringWithFormat:@"%ld.jpg", (long)[[NSDate date] timeIntervalSince1970]]; NSString* drawPath = [self getBiaoHuiFileSavePath]; drawPath = [drawPath stringByAppendingPathComponent:filePath]; if (![[NSFileManager defaultManager] fileExistsAtPath:drawPath]) { [[NSFileManager defaultManager] createFileAtPath:drawPath contents:data attributes:NULL]; } } else {//记事本的手写板保存 UIImage* image = [NoteDrawPadView imageFromView:_mItemPadView]; NSData* data = UIImageJPEGRepresentation(image, 1.0); BOOL bSuccess; NSString* bMessage = @""; NSString* savePath = @""; if (_drawPath.length != 0) {//表示修改手写板 savePath = _drawPath; if ([[NSFileManager defaultManager] isWritableFileAtPath:_drawPath]) { bSuccess = [data writeToFile:_drawPath atomically:YES]; if (bSuccess) { bMessage = @"保存成功"; } else { bMessage = @"保存失败"; } } } else { NSString* filePath = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]]; NSString* fileItem = [NSString stringWithFormat:@"%@_D.jpg",filePath]; NSString* drawPath = [self getDrawingFileSavePath]; drawPath = [drawPath stringByAppendingPathComponent:fileItem]; savePath = drawPath; if (![[NSFileManager defaultManager] fileExistsAtPath:drawPath]) { bSuccess = [[NSFileManager defaultManager] createFileAtPath:drawPath contents:data attributes:NULL]; if (bSuccess) { bMessage = @"保存成功"; } else { bMessage = @"保存失败"; } } } if (bSuccess) { NSNotification* notification = [[NSNotification alloc] initWithName:DrawPadSaveNotification object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:savePath, @"DrawPath", nil]]; [[NSNotificationCenter defaultCenter] postNotification:notification]; } UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:bMessage delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]; alertView.tag = 1313; [alertView show]; } }else{ CGRect rect = [UIScreen mainScreen].bounds; UIImage* imge =[NoteDrawPadView imageFromKeyWindow:rect]; NSData* data = UIImageJPEGRepresentation(imge, 0.35); NSDictionary* dics = [[NSDictionary alloc] initWithObjectsAndKeys:data,@"img",_mTagstring,@"tag", nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@"NSaddPersonListviewNotifications" object:nil userInfo:dics]; } } break; default: break; }}- (void)alertView:(UIAlertView*)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex{ if (alertView.tag == 1212) {//清除屏幕 //设置清屏按钮未选中 self.mToolSelectedBtn.selected = NO; if (buttonIndex == 0) { //设置最后一次颜色 [_mItemPadView setLineStrokeColor:_mItemPadView.latestColor]; return; } else { [_mItemPadView clear]; } } else if (alertView.tag == 1313) {//记事本保存成功 } else {//内文页保存成功 [self removeFromSuperview]; } }//点击颜色面板- (IBAction)clickColorPanelBtn:(id)sender { __block typeof(self) weakself = self; NSInteger tag = [sender tag]; tag = tag - 23; [self.mItemPadView setLineStrokeColor:tag]; _mColorSelectedBtn.selected = NO; _mColorSelectedBtn = (UIButton*)sender; _mColorSelectedBtn.selected = YES; CGRect colorRect = _mColorPanel.frame; if (_mColorPanel.alpha == 1.0) { colorRect.origin.y += ToolbarHeight; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ weakself.mColorPanel.alpha = 0.0; weakself.mColorPanel.frame = colorRect; } completion:^(BOOL finished) { }]; }}//点击线条粗细面板- (IBAction)clickSizePanelBtn:(id)sender { __block typeof(self) weakself = self; CGFloat lineWith = 0.0; NSInteger tag = [sender tag]; if (tag == 41) { lineWith = 10.0; } else if (tag == 45) { lineWith = 7.0; } else { lineWith = 3.0; } [_mItemPadView setLineStrokeWidth:lineWith]; _mSizeSelectedBtn.selected = NO; _mSizeSelectedBtn = (UIButton*)sender; _mSizeSelectedBtn.selected = YES; CGRect sizeRect =_mSizePanel.frame; if (_mSizePanel.alpha == 1.0) { sizeRect.origin.y += ToolbarHeight; [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ weakself.mSizePanel.alpha = 0.0; weakself.mSizePanel.frame = sizeRect; } completion:^(BOOL finished) { }]; }}- (NSString*)getDrawingFileSavePath{ NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Notepad"]; NSString *filePath = [path stringByAppendingPathComponent:@"Drawings"]; if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) { [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]; } return filePath;}- (NSString*)getBiaoHuiFileSavePath{ NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Notepad"]; NSString *filePath = [path stringByAppendingPathComponent:@"BiaoHui"]; if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) { [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]; } return filePath;}@end手写板工具:
#import <UIKit/UIKit.h>@interface NoteDrawPadItemView : UIView {int segmentColor; //线的颜色索引float segmentWidth; //线的宽度CGColorRef segmentColorRef; //线的颜色引用NSMutableArray* allPoints;NSMutableArray* allLines;NSMutableArray* allColors;NSMutableArray* allWidths;}@property (nonatomic) BOOL allline;@property (nonatomic) int latestColor; //线的最后一次颜色索引// 初始化数据- (void)initDrawPad;- (void)addPointToLines;- (void)setPathPoint:(CGPoint)point; //设置路径的点- (void)setLineStrokeColor:(int)sender; //sender表示选择颜色Button的Tag- (void)setLineStrokeWidth:(float)sender; //sender表示选择粗细Button的Tag//清屏按钮- (void)clear;//撤销当前的线条- (void)removeCurrentLine;//橡皮擦- (void)eraser;@end#import "NoteDrawPadItemView.h"@implementation NoteDrawPadItemView- (int)drawLineStrokeColor{ switch (segmentColor){case 0:segmentColorRef = [[UIColor colorWithRed:0 green:0 blue:0 alpha:1.0] CGColor];break;case 1:segmentColorRef = [[UIColor colorWithRed:219/255.0 green:62/255.0 blue:17/255.0 alpha:1.0] CGColor];break;case 2:segmentColorRef = [[UIColor colorWithRed:238/255.0 green:210/255.0 blue:41/255.0 alpha:1.0] CGColor];break;case 3:segmentColorRef = [[UIColor colorWithRed:107/255.0 green:176/255.0 blue:23/255.0 alpha:1.0] CGColor];break;case 4:segmentColorRef = [[UIColor colorWithRed:32/255.0 green:127/255.0 blue:221/255.0 alpha:1.0] CGColor];break; case -1: segmentColorRef = [[UIColor clearColor] CGColor]; break;default:break;} return segmentColor;}- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; //默认值 segmentColor = 0; _latestColor = 0; segmentWidth = 2.0; } return self;}- (void)dealloc{ segmentColorRef = NULL; allPoints = nil;allLines = nil;allColors = nil;allWidths = nil; [super dealloc];}- (void)drawRect:(CGRect)rect{ [super drawRect:rect]; //获取上下文CGContextRef context = UIGraphicsGetCurrentContext();//设置笔冒CGContextSetLineCap(context, kCGLineCapRound);//设置画线的连接处 拐点圆滑CGContextSetLineJoin(context, kCGLineJoinRound);//第一次初始化数据 //static BOOL allline = NO;if (_allline == NO) {allLines = [[NSMutableArray alloc] initWithCapacity:10];allColors = [[NSMutableArray alloc] initWithCapacity:10];allWidths = [[NSMutableArray alloc] initWithCapacity:10];self.allline = YES;} //画之前线if ([allLines count] > 0) {for (int i = 0; i < [allLines count]; i++) {NSArray* lines = [NSArray arrayWithArray:[allLines objectAtIndex:i]];if ([allColors count] > 0) {segmentColor = [[allColors objectAtIndex:i] intValue];segmentWidth = [[allWidths objectAtIndex:i] floatValue];} if ([lines count] > 1) {CGContextBeginPath(context);CGPoint startPoint = [[lines objectAtIndex:0] CGPointValue];CGContextMoveToPoint(context, startPoint.x, startPoint.y);for (int j = 0; j < [lines count]-1; j++) {CGPoint endPoint = [[lines objectAtIndex:j+1] CGPointValue];CGContextAddLineToPoint(context, endPoint.x, endPoint.y);} //设置线条的颜色int state = [self drawLineStrokeColor]; if (state == -1) { CGContextSetBlendMode(context, kCGBlendModeClear); } else { CGContextSetBlendMode(context, kCGBlendModeNormal); } CGContextSetStrokeColorWithColor(context, segmentColorRef); ////绘制画笔宽度 if (state == -1) { CGContextSetLineWidth(context, segmentWidth); } else { CGContextSetLineWidth(context, segmentWidth); }CGContextStrokePath(context);}}} //画当前的线 if ([allPoints count] > 1) { CGContextBeginPath(context); //起点 CGPoint startPoint = [[allPoints objectAtIndex:0] CGPointValue]; CGContextMoveToPoint(context, startPoint.x, startPoint.y); //把Move的点全部加入数组 for (int i = 0; i < [allPoints count]-1; i++) { CGPoint endPoint= [[allPoints objectAtIndex:i+1] CGPointValue]; CGContextAddLineToPoint(context, endPoint.x, endPoint.y); } //在颜色和画笔大小数组里面取不相应的值 segmentColor = [[allColors lastObject] intValue]; segmentWidth = [[allWidths lastObject] floatValue]; //绘制画笔颜色 int state = [self drawLineStrokeColor]; if (state == -1) { CGContextSetBlendMode(context, kCGBlendModeClear); } else { CGContextSetBlendMode(context, kCGBlendModeNormal); } CGContextSetStrokeColorWithColor(context, segmentColorRef); CGContextSetFillColorWithColor (context, segmentColorRef); //绘制画笔宽度 if (state == -1) { CGContextSetLineWidth(context, segmentWidth); } else { CGContextSetLineWidth(context, segmentWidth); } //把数组里面的点全部画出来 CGContextStrokePath(context); }}#pragma mark - 触摸事件//手指开始触屏开始- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch* touch = [touches anyObject];CGPoint startPoint = [touch locationInView:self]; //在外部方法里面调用 [self addLineStrokeColor]; [self addLineStrokeWidth]; [self initDrawPad]; [self setPathPoint:startPoint];}//手指移动时候发出- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {NSArray* points = [touches allObjects];CGPoint movePoint = [[points objectAtIndex:0] locationInView:self]; [self setPathPoint:movePoint];[self setNeedsDisplay];}//当手指离开屏幕时候- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self addPointToLines];[self setNeedsDisplay];}//电话呼入等事件取消时候发出- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {}- (void)initDrawPad{ allPoints = [[NSMutableArray alloc] initWithCapacity:10];}- (void)addPointToLines{ [allLines addObject:allPoints];}- (void)setPathPoint:(CGPoint)point{ NSValue* pointValue = [NSValue valueWithCGPoint:point];[allPoints addObject:[pointValue retain]];[pointValue release];}- (void)setLineStrokeColor:(int)sender{ if (sender != -1) {//记录用户最后一次选择的颜色 _latestColor = sender; } segmentColor = sender;}- (void)addLineStrokeColor{NSNumber* lineColor = [NSNumber numberWithInt:segmentColor];[allColors addObject:lineColor];}- (void)setLineStrokeWidth:(float)sender{ segmentWidth = sender;}- (void)addLineStrokeWidth{NSNumber* lineWidth = [NSNumber numberWithFloat:segmentWidth];[allWidths addObject:lineWidth];}//清屏按钮- (void)clear{ if ([allLines count] > 0){[allLines removeAllObjects];[allColors removeAllObjects];[allWidths removeAllObjects];[allPoints removeAllObjects];allLines = [[NSMutableArray alloc] initWithCapacity:10];allColors = [[NSMutableArray alloc] initWithCapacity:10];allWidths = [[NSMutableArray alloc] initWithCapacity:10];[self setNeedsDisplay];}}//撤销当前的线条- (void)removeCurrentLine{ if ([allLines count] > 0){[allLines removeLastObject];[allColors removeLastObject];[allWidths removeLastObject];[allPoints removeAllObjects];}[self setNeedsDisplay];}//橡皮擦- (void)eraser{ segmentColorRef = [[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0] CGColor];}@end示图:
0 0
- [IOS]手写板
- iOS Core Graphics手写板
- IOS Quartz2D 学习一 手写板
- Flex手写板
- 绘图板 手写板
- MSN手写板新功能!
- 网页手写板特效
- Android绘图学习 - 手写板
- 手写板市场分析之我见
- 手写板开发总结
- 手写板 (调用百度接口)
- 手写板 (调用百度接口)
- 单链表定义手写板
- Android遇上手写板
- 实现鼠标手写板(.Net)
- 安卓手写板app
- 签名涂鸦手写板
- 基于opencv3的手写板
- stm32跑程序很慢
- FaceBook开源库Fresco
- 俄罗斯方块的 C++实现
- maven-compiler-plugin 指定JDK版本和编码
- Android使用代码实现RelativeLayout,LinearLayout布局
- [IOS]手写板
- 去掉ScrollView拉到尽头顶部和底部渐变效果
- Http/Https、TCP/IP与SOCKET的连接
- 在游戏中屏蔽银行
- win32SDK中使用bmp和png位图
- Win8.1学习之----系统遇到DEP0700和出现“This app can't be activated by the Built-in Administrator”问题
- 腾讯一面二面终面真题
- 骨瓷碗
- INSTALL_PARSE_FAILED_NO_CERTIFICATES