ios觸摸事件案例

前言:ios事件之觸摸事件詳解請?zhí)Dhttp://www.itdecent.cn/p/1c355a7264b5
創(chuàng)建畫布畫線條
1、創(chuàng)建模型對象并聲明CGPoint屬性

.h文件
@interface TouchLine : NSObject
/*起點*/
@property(nonatomic,assign)CGPoint  begin;
/*終點*/
@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對應的值
    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]];
        //開啟多點觸摸事件,默認為NO
        [self setMultipleTouchEnabled:YES];
    }
    return self;
}

/*C函數(shù)繪制線條*/
-(void)drawRect:(CGRect)rect{
    //設置上下文
    CGContextRef context=UIGraphicsGetCurrentContext();
    //設置線寬
    CGContextSetLineWidth(context, 10.0);
    //設置線條終點形狀
    CGContextSetLineCap(context, kCGLineCapRound);//kCGLineCapRound屬性值指定繪制圓形端點, 線條結尾處繪制一個直徑為線條寬度的半圓
    //設置當前繪圖上下文中的填充和筆劃顏色(已經(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);
    }
    
    //設置當前繪圖上下文中的填充和筆劃顏色(正在畫的線條)
    [[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];
    }
}

//一根手指或多根手指在屏幕上移動(隨著手指的移動,相關的對象會持續(xù)發(fā)送該消息)
- (void)touchesMoved:(NSSet *)touches withEvent:(nullable UIEvent *)event{
    //根據(jù)傳入的UITouch對象,更新lineInProcess_Dic
    for (UITouch *touch in touches) {
        //取出當前對應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) {
        //取出當前對應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];
}

//在觸摸操作正常結束前,某個系統(tǒng)事件(如有電話打進來)打斷了觸摸過程
- (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];
}
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容