触摸事件及自定义画板

import "AppDelegate.h"

import "TouchView.h"

import "ScrawlView.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [self.window setRootViewController:[[UIViewController alloc]init]];

    for (int i = 0; i < 10 ; i++) {
    TouchView *touchView = [[TouchView alloc]initWithFrame:self.window.frame];
    touchView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
    [self.window addSubview:touchView];
    }
    // 定义一个touchView
    TouchView *touchView = [[TouchView alloc]initWithFrame:CGRectMake(50, 50, 300, 300)];
    touchView.backgroundColor = [UIColor yellowColor];
    // 关闭touchView用户交互
    // [touchView setUserInteractionEnabled:NO];
    [self.window addSubview:touchView];
    // 再定义一个touchView1,添加到touchView上
    TouchView *touchView1 = [[TouchView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    touchView1.backgroundColor = [UIColor redColor];
    // 关闭用户交互,这里就是斩断了响应者链,当前视图(响应者)及其上面的子视图都不会响应事件
    //�� [touchView1 setUserInteractionEnabled:NO];
    [touchView addSubview:touchView1];

    TouchView *touchView2 = [[TouchView alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
    touchView.backgroundColor = [UIColor redColor];
    [self.window addSubview:touchView2];

    ScrawlView *scrawlView = [[ScrawlView alloc]initWithFrame:self.window.frame];
    scrawlView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:scrawlView];

    return YES;
    }


import "TouchView.h"

define COLOUR [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0]

@implementation TouchView

// 只要我们触摸屏幕,系统就会查找触摸位置,当找到触摸位置时,系统就会查找当前触摸位置是否有时间需要处理(就是查找有没有实现touch的一系列方法),如果有,就会处理事件,如果没有,就不做任何操作

// 开始触摸 touches:当前屏幕上所有的触摸对象
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 得到其中任意一个触摸对象 (得到一根手指)
UITouch *aTouch = touches.anyObject;
// 得到当前触摸点在当前view父视图上的位置
CGPoint currentPoint = [aTouch locationInView:self.superview];
}

// 移动触摸 (触控完成之后的移动,只要触控点在移动,这个方法会多次调用)
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent )event{
// 得到触控对象
UITouch aTouch = touches.anyObject;
// 得到当前位置的上一个位置
CGPoint prePoint = [aTouch previousLocationInView:self.superview];
// 得到当前位置
CGPoint currentPoint = [aTouch locationInView:self.superview];
// 计算偏移量
float delX = currentPoint.x - prePoint.x;
float delY = currentPoint.y - prePoint.y;
// 由于有可能有负数,所以需要绝对值(开方)
float sqrtX = sqrt(delX
delX);
float sqrtY = sqrt(delY
delY);
// 确定滑动方向
// 情况一 x为负数,并且sqrtX > sqrtY 从左边出屏幕
if ((delX < 0) && (sqrtX > sqrtY)) {
CGRect frame = self.frame;
// 整个屏幕的宽度
// float screenWidth = [UIScreen mainScreen].bounds.size.width;
frame.origin.x = - frame.size.width;
self.frame = frame;
}
// 情况二
if ((delY < 0) && (sqrtX < sqrtY)) {
CGRect frame = self.frame;
frame.origin.y = -frame.size.height;
self.frame = frame;
}
// 情况三
if ((delX > 0) && (sqrtX > sqrtY)) {
CGRect frame = self.frame;
frame.origin.x = frame.size.width;
self.frame = frame;
}
// 情况四
if ((delY > 0) && (sqrtX < sqrtY)) {
CGRect frame = self.frame;
frame.origin.y = frame.size.height;
self.frame = frame;
}
}

// 停止触摸(手指离开屏幕)
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self setBackgroundColor:COLOUR];
NSLog(@"————————%s",func);
}

// 取消触摸(来电操作打断当前的触摸操作)
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent )event{
NSLog(@"**********%s",func);
}
/

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

  • (void)drawRect:(CGRect)rect {
    // Drawing code
    }
    */

@end

import "ScrawlView.h"

@interface ScrawlView ()

@property (nonatomic ,retain)NSMutableArray *allLineMutableArray; // 用来存储所有的线
@property (nonatomic ,retain)NSMutableArray *allColorMutableArray;
@property (nonatomic ,retain)NSMutableArray *allLineWidthMutableArray;

@end

@implementation ScrawlView

-(NSMutableArray*)allLineMutableArray {
if (!_allLineMutableArray) {
_allLineMutableArray = [[NSMutableArray alloc]init];

    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    deleteBtn.frame = CGRectMake(0, 0, 50, 50);
    [deleteBtn setTitle:@"橡皮擦" forState:UIControlStateNormal];
    [deleteBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:deleteBtn];
    
}return _allLineMutableArray;

}

-(NSMutableArray*)allColorMutableArray {
if (!_allColorMutableArray) {
_allColorMutableArray = [[NSMutableArray alloc]init];
}return _allColorMutableArray;
}

-(NSMutableArray*)allLineWidthMutableArray{
if (!_allLineWidthMutableArray) {
_allLineWidthMutableArray = [[NSMutableArray alloc]init];
}return _allLineWidthMutableArray;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 得到触摸对象
UITouch *aTouch = touches.anyObject;
// 得到初始点
CGPoint startPoint = [aTouch locationInView:self.superview];
// 初始化一个贝塞尔曲线,用来存储所有轨迹上的点
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
// 贝塞尔存储起始点
[bezierPath moveToPoint:startPoint];
// 存储当前的贝塞尔线
[self.allLineMutableArray addObject:bezierPath];

UIColor *myColour = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
[self.allColorMutableArray addObject:myColour];

}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 得到触摸对象
UITouch *aTouch = touches.anyObject;
// 得到当前的点
CGPoint currentPoiont = [aTouch locationInView:self.superview];
// 得到贝塞尔对象,用来添加此处得到的点
UIBezierPath bezierPath = self.allLineMutableArray.lastObject;
// 将此处得到的点添加到贝塞尔中
[bezierPath addLineToPoint:currentPoiont];
// 重新绘制当前视图
[self setNeedsDisplay]; // 调用此方法,就会触发drawRect来重新绘制当前视图
}
// 橡皮擦功能,删除掉最后一条线(数组保存每次画出的线条,删除最后一条)
-(void)deleteBtnAction:(UIButton
)sender{
if (_allLineMutableArray && _allLineMutableArray.count) {
[self.allLineMutableArray removeLastObject];
[self setNeedsDisplay];
}
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}

// 此方法是用来重新绘制当前视图

  • (void)drawRect:(CGRect)rect {
    // 设置画笔(颜色)
    for (UIBezierPath* bezierPath in self.allLineMutableArray) {
    UIColor myColor = [self.allColorMutableArray objectAtIndex:[self.allLineMutableArray indexOfObject:bezierPath]];
    [myColor setStroke];
    // 设置的线条的宽度
    bezierPath.lineWidth = arc4random();
    // 画线
    [bezierPath stroke];
    }
    }
    /

    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
  • (void)drawRect:(CGRect)rect {
    // Drawing code
    }
    */

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容