分享一個(gè)iOS下實(shí)現(xiàn)基本繪畫板功能的簡(jiǎn)單方法
代碼部分
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();
//設(shè)置畫筆粗細(xì)
CGContextSetLineWidth(context, 5.0f);
//設(shè)置畫筆顏色
//[[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);
CGContextAddLineToPoint(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);
CGContextAddLineToPoint(context, point2.x, point2.y);
CGContextStrokePath(context);
}
}
//不支持多點(diǎn)觸摸
- (BOOL) isMultipleTouchEnabled
{
return NO;
}
//創(chuàng)建一個(gè)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]];
}
//移動(dòng)過(guò)程中記錄這些points
//調(diào)用setNeedsDisplay,會(huì)觸發(fā)drawRect方法的調(diào)用
- (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
效果圖

相關(guān)文章
iOS開發(fā)之UIMenuController使用示例詳解
這篇文章主要為大家介紹了iOS開發(fā)之UIMenuController使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
解決ios端點(diǎn)擊按鈕閃爍問(wèn)題(小tips)
這篇文章主要介紹了ios端點(diǎn)擊按鈕閃爍的解決方法(小tips),需要的朋友參考下吧2017-10-10
iOS開發(fā)中常見(jiàn)的項(xiàng)目文件與MVC結(jié)構(gòu)優(yōu)化思路解析
這篇文章主要介紹了iOS開發(fā)中常見(jiàn)的項(xiàng)目文件與MVC結(jié)構(gòu)優(yōu)化思路解析,示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12
iOS開發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
iOS中UITableView使用的常見(jiàn)問(wèn)題總結(jié)
這篇文章主要總結(jié)了iOS中UITableView使用的常見(jiàn)問(wèn)題,其中包括如何設(shè)置headerView以及其高度、去掉多余cell的分割線 以及如何設(shè)置section數(shù)、行數(shù)等一系列的問(wèn)題,文中介紹的更詳細(xì),需要的朋友們下面來(lái)一起看看詳細(xì)介紹吧。2017-03-03
iOS11 WKWebView 無(wú)法加載內(nèi)容的解決方法
這篇文章主要介紹了iOS11 WKWebView 無(wú)法加載內(nèi)容,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
iOS中大尺寸圖片的旋轉(zhuǎn)與縮放實(shí)例詳解
圖片縮小旋轉(zhuǎn)是我們?cè)陂_發(fā)中經(jīng)常會(huì)遇到的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于iOS中大尺寸圖片的旋轉(zhuǎn)與縮放的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧2018-09-09
IOS 聊天界面(自適應(yīng)文字)的實(shí)現(xiàn)
本文主要介紹一個(gè)實(shí)現(xiàn)聊天界面的思路過(guò)程,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
IOS 創(chuàng)建并發(fā)線程的實(shí)例詳解
這篇文章主要介紹了IOS 創(chuàng)建并發(fā)線程的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07

