代碼部門
TouchView.h
#import <UIKit/UIKit.h>
@interface TouchView : UIView
{
NSMutableArray *points;
NSArray *points_all;
CGContextRef context;
UIColor *paint_clr;
}
@property (strong,nonatomic) NSMutableArray *points;
@property (strong,nonatomic) NSArray *points_all;
@property (strong,nonatomic) UIColor *paint_clr;
@end
TouchView.m
#import "TouchView.h"
@implementation TouchView
@synthesize points, points_all, paint_clr;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
paint_clr = [UIColor greenColor];
}
return self;
}
// Only override drawRect: if you perform custom draWing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// DraWing code
if ((!self.points) || (self.points.count < 2)) {
return;
}
context = UIGraphicsGetCurrentContext();
//設置畫筆粗細
CGContextSetLineWidth(context, 5.0f);
//設置畫筆色彩
//[[UIColor blueColor]set ];
// [paint_clr set];
//CGContextSetStrokeColorWithColor(context, [[UIColor blueColor]CGColor]);
CGContextSetStrokeColorWithColor(context, [paint_clr CGColor]);
//畫之前的軌跡
for (int j = 0 ; j < [self.points_all count]; j++) {
NSMutableArray *points_tmp = [points_all objectAtIndex:j];
for (int i = 0;i < [points_tmp count]-1;i++)
{
CGPoint point1 = [[points_tmp objectAtIndex:i] CGPointValue];
CGPoint point2 = [[points_tmp objectAtIndex:(i+1)] CGPointValue];
CGContextMoveToPoint(context, point1.x, point1.y);
CGContextAddL.netoPoint(context, point2.x, point2.y);
CGContextStrokePath(context);
}
}
//畫此次
for (int i=0; i < [self.points count]-1; i++) {
CGPoint point1 = [[self.points objectAtIndex:i] CGPointValue];
CGPoint point2 = [[self.points objectAtIndex:(i+1)] CGPointValue];
CGContextMoveToPoint(context, point1.x, point1.y);
CGContextAddL.netoPoint(context, point2.x, point2.y);
CGContextStrokePath(context);
}
}
//不支撐多點觸摸
- (BOOL) isMultipleTouchEnabled
{
return NO;
}
//創立一個array,而且記載初始ponit
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
{
self.points = [NSMutableArray array];
CGPoint pt = [[touches anyObject] locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:pt]];
}
//挪動進程中記載這些points
//挪用setNeedsDisplay,會觸發drawRect辦法的挪用
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint pt = [[touches anyObject] locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:pt]];
[self setNeedsDisplay];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSMutableArray *points_tmp = [[NSMutableArray alloc] initWithArray:self.points];
if (self.points_all == nil) {
self.points_all = [[NSArray alloc] initWithObjects:points_tmp, nil];
}else {
self.points_all = [self.points_all arrayByAddingObject:points_tmp];
}
}
@end
ViewController.h
#import <UIKit/UIKit.h>
@class TouchView;
@interface ViewController : UIViewController
{
TouchView *tv;
}
@end
ViewController.m
#import "ViewController.h"
#import "TouchView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.userInteractionEnabled = YES;
// TouchView *tv = [[TouchView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 260.0f, 260.0f)];
tv = [[TouchView alloc]initWithFrame:self.view.frame];
tv.backgroundColor = [UIColor blackColor];
[self.view addSubview:tv];
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[@"White Red Blue Green Yellow" componentsSeparatedByString:@" "]];
seg.segmentedControlStyle = UISegmentedControlSegmentCenter;
seg.tintColor = [UIColor blackColor];
seg.center = CGPointMake(self.view.center.x, (self.view.bounds.size.height - seg.bounds.size.height));
[self.view addSubview:seg];
[seg addTarget:self action:@selector(colorChange:) forControlEvents:UIControlEventValueChanged];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void) colorChange: (UISegmentedControl *) seg
{
switch ([seg selectedSegmentIndex])
{
case 0:
tv.paint_clr = [UIColor whiteColor];
break;
case 1:
tv.paint_clr = [UIColor redColor];
break;
case 2:
tv.paint_clr = [UIColor blueColor];
break;
case 3:
tv.paint_clr = [UIColor greenColor];
break;
case 4:
tv.paint_clr = [UIColor yellowColor];
break;
default:
break;
}
}
@end
後果圖
【分享一個iOS下完成根本繪畫板功效的簡略辦法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!