ios觸摸事件案例

前言:ios事件之觸摸事件詳解請(qǐng)?zhí)D(zhuǎn)http://m.itdecent.cn/p/1c355a7264b5
創(chuàng)建畫(huà)布畫(huà)線條
1、創(chuàng)建模型對(duì)象并聲明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類(lèi),繼承于UIView

.h文件
@interface TouchDrawView : UIView{
    //存儲(chǔ)key值
    NSMutableDictionary *lineInProcess_Dic;
    //存儲(chǔ)key對(duì)應(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]];
        //開(kāi)啟多點(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é)尾處繪制一個(gè)直徑為線條寬度的半圓
    //設(shè)置當(dāng)前繪圖上下文中的填充和筆劃顏色(已經(jīng)完成的線條)
    [[UIColor blackColor]set];
    for (TouchLine *line in completeline_Arr) {
        //開(kāi)始畫(huà)線
        CGContextMoveToPoint(context, [line begin].x, [line begin].y);
        //畫(huà)直線
        CGContextAddLineToPoint(context, [line end].x, [line end].y);
        //沿著路徑畫(huà)線
        CGContextStrokePath(context);
    }
    
    //設(shè)置當(dāng)前繪圖上下文中的填充和筆劃顏色(正在畫(huà)的線條)
    [[UIColor redColor]set];
    for (NSValue *value in lineInProcess_Dic) {
        TouchLine *line = [lineInProcess_Dic objectForKey:value];
        //開(kāi)始畫(huà)線
        CGContextMoveToPoint(context, [line begin].x, [line begin].y);
        //畫(huà)直線
        CGContextAddLineToPoint(context, [line end].x, [line end].y);
        //沿著路徑畫(huà)線
        CGContextStrokePath(context);
    }
    
}

-(void)clearAll{
    [lineInProcess_Dic removeAllObjects];
    [completeline_Arr removeAllObjects];
    //重畫(huà)
    [self setNeedsDisplay];
}


//一根手指或多根手指觸摸屏幕
- (void)touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event{
    for (UITouch *touch in touches) {
        //連按
        if ([touch tapCount]>1) {
            [self clearAll];
            return;
        }
        //封裝UITouch對(duì)象地址至value_key中
        NSValue *value_key = [NSValue valueWithNonretainedObject:touch];
        //根據(jù)觸摸位置創(chuàng)建對(duì)象
        CGPoint locat = [touch locationInView:self];
        TouchLine *line = [[TouchLine alloc]init];
        [line setBegin:locat];
        [line setEnd:locat];
        //將鍵-值對(duì)保存至字典中
        [lineInProcess_Dic setObject:line forKey:value_key];
    }
}

//一根手指或多根手指在屏幕上移動(dòng)(隨著手指的移動(dòng),相關(guān)的對(duì)象會(huì)持續(xù)發(fā)送該消息)
- (void)touchesMoved:(NSSet *)touches withEvent:(nullable UIEvent *)event{
    //根據(jù)傳入的UITouch對(duì)象,更新lineInProcess_Dic
    for (UITouch *touch in touches) {
        //取出當(dāng)前對(duì)應(yīng)UITouch對(duì)象的TouchLine對(duì)象
        NSValue *value_key = [NSValue valueWithNonretainedObject:touch];
        TouchLine *line = [lineInProcess_Dic objectForKey:value_key];
        //更新TouchLine對(duì)象
        CGPoint locat = [touch locationInView:self];
        [line setEnd:locat];
    }
    //重畫(huà)
    [self setNeedsDisplay];
}

//提取的公共方法
-(void)endTouches:(NSSet *)touches{
    //將已經(jīng)完成的TouchLine對(duì)象移除
    for (UITouch *touch in touches) {
        //取出當(dāng)前對(duì)應(yīng)UITouch對(duì)象的TouchLine對(duì)象
        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];
        }
    }
    //重畫(huà)
    [self setNeedsDisplay];
}

//一根手指或多根手指離開(kāi)屏幕
- (void)touchesEnded:(NSSet *)touches withEvent:(nullable UIEvent *)event{
    [self endTouches:touches];
}

//在觸摸操作正常結(jié)束前,某個(gè)系統(tǒng)事件(如有電話打進(jìn)來(lái))打斷了觸摸過(guò)程
- (void)touchesCancelled:(NSSet *)touches withEvent:(nullable UIEvent *)event{
    [self endTouches:touches];
}

//3D 觸摸事件
- (void)touchesEstimatedPropertiesUpdated:(NSSet *)touches NS_AVAILABLE_IOS(9_1){
    NSLog(@"暫時(shí)沒(méi)找到使用的方法案例");
}

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];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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