第一步:创建UIView子类PaintView:
1、声明属性和方法:
#import <UIKit/UIKit.h>
@interface PaintView : UIView
@property(nonatomic,retain)NSMutableArray *allLines;
@property(nonatomic,retain)UIColor *lineColor;
@property(nonatomic,assign)CGFloat lineWidth;
-(void)undoLastDrawing;
-(void)allClean;
@end
第二步、实现方法并引入LineModel.h文件:
#import "PaintView.h"
#import "LineModel.h"
@implementation PaintView
-(void)dealloc{
[_allLines release];
[_lineColor release];
[super dealloc];
}
-(NSMutableArray *)allLines{
if (!_allLines) {
self.allLines=[NSMutableArray array];
}
return _allLines;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *aTouth=touches.anyObject;
CGPoint startPoint=[aTouth locationInView:self.superview];
UIBezierPath *bezierPath=[UIBezierPath bezierPath];
[bezierPath moveToPoint:startPoint];
LineModel *line=[[[LineModel alloc]init]autorelease];
line.path=bezierPath;
line.color=self.lineColor;
line.width=self.lineWidth;
[self.allLines addObject:line];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *aTouth=touches.anyObject;
CGPoint movePoint=[aTouth locationInView:self.superview];
LineModel *aLine=self.allLines.lastObject;
[aLine.path addLineToPoint:movePoint];
[self setNeedsDisplay];
}
-(void)undoLastDrawing{
[self.allLines removeLastObject];
[self setNeedsDisplay];
}
-(void)allClean;{
[self.allLines removeAllObjects];
[self setNeedsDisplay];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
for (LineModel *aLine in self.allLines) {
[aLine.color setStroke];
[aLine.path setLineWidth:aLine.width];
[aLine.path stroke];
}}
@end
第三步、创建NSObject子类LineModel同时引入<UIKit/UIKit.h>框架:
1、声明属性:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface LineModel : NSObject
@property(nonatomic,retain)UIBezierPath *path;
@property(nonatomic,retain)UIColor *color;
@property(nonatomic,assign)CGFloat width;
@end
2、dealloc:
#import "LineModel.h"
@implementation LineModel
-(void)dealloc{
[_path release];
[_color release];
[super dealloc];
}
@end
第四步:创建UIViewController子类MainViewController同时:
#import "MainViewController.h"
#import "PaintView.h"
@interface MainViewController ()
@end
@implementation MainViewController
-(void)loadView{
PaintView *paintView=[[[PaintView alloc]initWithFrame:[[UIScreen mainScreen]bounds]]autorelease];
paintView.backgroundColor=[UIColor whiteColor];
paintView.lineWidth=5;
paintView.lineColor=[UIColor blackColor];
self.view=paintView;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *aButton=[UIButton buttonWithType:UIButtonTypeSystem];
aButton.frame=CGRectMake(CGRectGetWidth(self.view.bounds)-80, 40, 60, 40);
[aButton setTitle:@"菜单" forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(handleAButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
UITextField *aTextField=[[[UITextField alloc]initWithFrame:CGRectZero]autorelease];
aTextField.tag=123;
[self.view addSubview:aTextField];
UIInputView *myInputView=[[[UIInputView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 60)]autorelease];
myInputView.backgroundColor=[UIColor whiteColor];
aTextField.inputView=myInputView;
NSArray *titles=@[@"减小",@"增大",@"撤销",@"清空",@"颜色"];
CGFloat width=(CGRectGetWidth(self.view.bounds)-20*6)/5;
CGFloat height=width;
CGFloat originx=20;
CGFloat originy=(60-height)/2;
for (int i=0; i<titles.count; i++) {
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.tag=100+i;
button.frame=CGRectMake(originx+(width+20)*i, originy, width, height);
button.layer.cornerRadius=width/2;
button.layer.masksToBounds=YES;
[button setTitle:titles[i] forState:UIControlStateNormal];
button.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:0.6];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[myInputView addSubview:button];
}
}
-(void)handleAButtonAction:(UIButton *)sender{
UITextField *aTextField=(UITextField *)[self.view viewWithTag:123];
if (aTextField.isFirstResponder) {
[aTextField resignFirstResponder];
}else{
[aTextField becomeFirstResponder];
}
}
-(void)handleButtonAction:(UIButton *)sender{
PaintView *paintView=(PaintView *)self.view;
switch (sender.tag) {
case 100:{
if (paintView.lineWidth>2) {
paintView.lineWidth-=1;
}
break;
}case 101:{
paintView.lineWidth+=1;
break;
}case 102:{
[paintView undoLastDrawing];
break;
}case 103:{
[paintView allClean];
break;
}case 104:{
sender.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
paintView.lineColor=sender.backgroundColor;
break;
}
default:
break;
}
}
第五步:在AppDelegate.m加载控制器:
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-(void)dealloc{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
MainViewController *mainVC=[[[MainViewController alloc]init]autorelease];
self.window.rootViewController=mainVC;
return YES;
}