前言:ios事件之觸摸事件詳解請?zhí)D(zhuǎn)http://www.reibang.com/p/1c355a7264b5
創(chuàng)建畫布畫線條
1骤铃、創(chuàng)建模型對象并聲明CGPoint屬性
.h文件
@interface TouchLine : NSObject
/*起點(diǎn)*/
@property(nonatomic,assign)CGPoint begin;
/*終點(diǎn)*/
@property(nonatomic,assign)CGPoint end;
@end
.m文件
@implementation TouchLine
@synthesize begin,end;
@end
2、自定義視圖情组,創(chuàng)建TouchDrawView類暖夭,繼承于UIView
.h文件
@interface TouchDrawView : UIView{
//存儲key值
NSMutableDictionary *lineInProcess_Dic;
//存儲key對應(yīng)的值
NSMutableArray *completeline_Arr;
}
/*清理*/
-(void)clearAll;
.m文件
@implementation TouchDrawView
-(id)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
//初始化
lineInProcess_Dic=[[NSMutableDictionary alloc]init];
completeline_Arr=[[NSMutableArray alloc]init];
[self setBackgroundColor:[UIColor whiteColor]];
//開啟多點(diǎn)觸摸事件,默認(rèn)為NO
[self setMultipleTouchEnabled:YES];
}
return self;
}
/*C函數(shù)繪制線條*/
-(void)drawRect:(CGRect)rect{
//設(shè)置上下文
CGContextRef context=UIGraphicsGetCurrentContext();
//設(shè)置線寬
CGContextSetLineWidth(context, 10.0);
//設(shè)置線條終點(diǎn)形狀
CGContextSetLineCap(context, kCGLineCapRound);//kCGLineCapRound屬性值指定繪制圓形端點(diǎn)两芳, 線條結(jié)尾處繪制一個直徑為線條寬度的半圓
//設(shè)置當(dāng)前繪圖上下文中的填充和筆劃顏色(已經(jīng)完成的線條)
[[UIColor blackColor]set];
for (TouchLine *line in completeline_Arr) {
//開始畫線
CGContextMoveToPoint(context, [line begin].x, [line begin].y);
//畫直線
CGContextAddLineToPoint(context, [line end].x, [line end].y);
//沿著路徑畫線
CGContextStrokePath(context);
}
//設(shè)置當(dāng)前繪圖上下文中的填充和筆劃顏色(正在畫的線條)
[[UIColor redColor]set];
for (NSValue *value in lineInProcess_Dic) {
TouchLine *line = [lineInProcess_Dic objectForKey:value];
//開始畫線
CGContextMoveToPoint(context, [line begin].x, [line begin].y);
//畫直線
CGContextAddLineToPoint(context, [line end].x, [line end].y);
//沿著路徑畫線
CGContextStrokePath(context);
}
}
-(void)clearAll{
[lineInProcess_Dic removeAllObjects];
[completeline_Arr removeAllObjects];
//重畫
[self setNeedsDisplay];
}
//一根手指或多根手指觸摸屏幕
- (void)touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event{
for (UITouch *touch in touches) {
//連按
if ([touch tapCount]>1) {
[self clearAll];
return;
}
//封裝UITouch對象地址至value_key中
NSValue *value_key = [NSValue valueWithNonretainedObject:touch];
//根據(jù)觸摸位置創(chuàng)建對象
CGPoint locat = [touch locationInView:self];
TouchLine *line = [[TouchLine alloc]init];
[line setBegin:locat];
[line setEnd:locat];
//將鍵-值對保存至字典中
[lineInProcess_Dic setObject:line forKey:value_key];
}
}
//一根手指或多根手指在屏幕上移動(隨著手指的移動摔寨,相關(guān)的對象會持續(xù)發(fā)送該消息)
- (void)touchesMoved:(NSSet *)touches withEvent:(nullable UIEvent *)event{
//根據(jù)傳入的UITouch對象,更新lineInProcess_Dic
for (UITouch *touch in touches) {
//取出當(dāng)前對應(yīng)UITouch對象的TouchLine對象
NSValue *value_key = [NSValue valueWithNonretainedObject:touch];
TouchLine *line = [lineInProcess_Dic objectForKey:value_key];
//更新TouchLine對象
CGPoint locat = [touch locationInView:self];
[line setEnd:locat];
}
//重畫
[self setNeedsDisplay];
}
//提取的公共方法
-(void)endTouches:(NSSet *)touches{
//將已經(jīng)完成的TouchLine對象移除
for (UITouch *touch in touches) {
//取出當(dāng)前對應(yīng)UITouch對象的TouchLine對象
NSValue *value_key = [NSValue valueWithNonretainedObject:touch];
TouchLine *line = [lineInProcess_Dic objectForKey:value_key];
//如果是連按怖辆,則line值為nil是复,所以先判斷,避免nil加入數(shù)組
if (line) {
[completeline_Arr addObject:line];
[lineInProcess_Dic removeObjectForKey:value_key];
}
}
//重畫
[self setNeedsDisplay];
}
//一根手指或多根手指離開屏幕
- (void)touchesEnded:(NSSet *)touches withEvent:(nullable UIEvent *)event{
[self endTouches:touches];
}
//在觸摸操作正常結(jié)束前疗隶,某個系統(tǒng)事件(如有電話打進(jìn)來)打斷了觸摸過程
- (void)touchesCancelled:(NSSet *)touches withEvent:(nullable UIEvent *)event{
[self endTouches:touches];
}
//3D 觸摸事件
- (void)touchesEstimatedPropertiesUpdated:(NSSet *)touches NS_AVAILABLE_IOS(9_1){
NSLog(@"暫時沒找到使用的方法案例");
}
3佑笋、在UIViewController的.m文件中調(diào)用
@implementation TouchVC
- (void)viewDidLoad {
[super viewDidLoad];
self.title=@"觸摸事件";
TouchDrawView *touchView = [[TouchDrawView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT)];
[self.view addSubview:touchView];
}