Xcode版本4.5.1
1. 開啓一個專案
2. 設定成iPad直立模式,並放上一個Button到UIView
4. 註冊Button到mainViewController.h,並增加兩個畫布instance
#import <UIKit/UIKit.h>
#import "PaintMaskViewController.h"
@interface mainViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *toggleCanvasBtn;
@property(nonatomic, retain) PaintMaskViewController *painCanvasView1;
@property(nonatomic, retain) PaintMaskViewController *painCanvasView2;
@end
5. 增加畫布的設定與控制碼
#import "mainViewController.h"
@interface mainViewController ()
@end
@implementation mainViewController
@synthesize toggleCanvasBtn;
@synthesize painCanvasView1;
@synthesize painCanvasView2;
BOOL canvasIsShowed;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
canvasIsShowed = FALSE;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)toggleCanvas:(id)sender
{
if (canvasIsShowed) {
[toggleCanvasBtn setTitle:@"圖二" forState:UIControlStateNormal];
if (painCanvasView2 == nil) {
painCanvasView2 = [[PaintMaskViewController alloc] init];
//[painCanvasView initialize];
}
[painCanvasView1.view removeFromSuperview];
[self.view addSubview:painCanvasView2.view];
canvasIsShowed = FALSE;
}else {
[toggleCanvasBtn setTitle:@"圖一" forState:UIControlStateNormal];
if (painCanvasView1 == nil) {
painCanvasView1 = [[PaintMaskViewController alloc] init];
}
[painCanvasView2.view removeFromSuperview];
[self.view addSubview:painCanvasView1.view];
canvasIsShowed = TRUE;
}
}
@end
6. 在PaintMaskViewController.h設定UIImageView
#import <UIKit/UIKit.h>
@interface PaintMaskViewController : UIViewController
@property(nonatomic, retain) UIImageView* drawImage;
@end
7. 在PaintMaskViewController.m加入觸控畫圖的程式碼
#import "PaintMaskViewController.h"
@interface PaintMaskViewController ()
@end
@implementation PaintMaskViewController
int mouseMoved;
BOOL mouseSwiped;
CGPoint lastPoint;
CGPoint currentPoint;
@synthesize drawImage;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(drawImage == nil) {
self.view.frame = CGRectMake(0, 0, 720, 800);
self.drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = self.view.frame;
//此處非常重要,注意[self.view addSubview:self.drawImage];
}
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
return;
}
lastPoint = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self.view];
[self drawLine:1];
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//Double click to clean the canvas
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
return;
}
if(!mouseSwiped) {
[self drawLine:0];
}
}
- (void) drawLine:(int) move
{
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
if (move == 1)
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
else
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
@end
8. 執行結果,兩張畫布切換




















































