觸摸事件及自定義畫板

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];
    // 關(guān)閉touchView用戶交互
    // [touchView setUserInteractionEnabled:NO];
    [self.window addSubview:touchView];
    // 再定義一個touchView1,添加到touchView上
    TouchView *touchView1 = [[TouchView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    touchView1.backgroundColor = [UIColor redColor];
    // 關(guān)閉用戶交互,這里就是斬斷了響應(yīng)者鏈,當(dāng)前視圖(響應(yīng)者)及其上面的子視圖都不會響應(yīng)事件
    //?? [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

// 只要我們觸摸屏幕八孝,系統(tǒng)就會查找觸摸位置干跛,當(dāng)找到觸摸位置時楼入,系統(tǒng)就會查找當(dāng)前觸摸位置是否有時間需要處理(就是查找有沒有實現(xiàn)touch的一系列方法)嘉熊,如果有阐肤,就會處理事件孕惜,如果沒有,就不做任何操作

// 開始觸摸 touches:當(dāng)前屏幕上所有的觸摸對象
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 得到其中任意一個觸摸對象 (得到一根手指)
UITouch *aTouch = touches.anyObject;
// 得到當(dāng)前觸摸點在當(dāng)前view父視圖上的位置
CGPoint currentPoint = [aTouch locationInView:self.superview];
}

// 移動觸摸 (觸控完成之后的移動,只要觸控點在移動瞄勾,這個方法會多次調(diào)用)
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent )event{
// 得到觸控對象
UITouch aTouch = touches.anyObject;
// 得到當(dāng)前位置的上一個位置
CGPoint prePoint = [aTouch previousLocationInView:self.superview];
// 得到當(dāng)前位置
CGPoint currentPoint = [aTouch locationInView:self.superview];
// 計算偏移量
float delX = currentPoint.x - prePoint.x;
float delY = currentPoint.y - prePoint.y;
// 由于有可能有負(fù)數(shù)进陡,所以需要絕對值(開方)
float sqrtX = sqrt(delX
delX);
float sqrtY = sqrt(delY
delY);
// 確定滑動方向
// 情況一 x為負(fù)數(shù)四濒,并且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);
}

// 取消觸摸(來電操作打斷當(dāng)前的觸摸操作)
-(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];
// 存儲當(dāng)前的貝塞爾線
[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;
// 得到當(dāng)前的點
CGPoint currentPoiont = [aTouch locationInView:self.superview];
// 得到貝塞爾對象,用來添加此處得到的點
UIBezierPath bezierPath = self.allLineMutableArray.lastObject;
// 將此處得到的點添加到貝塞爾中
[bezierPath addLineToPoint:currentPoiont];
// 重新繪制當(dāng)前視圖
[self setNeedsDisplay]; // 調(diào)用此方法伴鳖,就會觸發(fā)drawRect來重新繪制當(dāng)前視圖
}
// 橡皮擦功能榜聂,刪除掉最后一條線(數(shù)組保存每次畫出的線條嗓蘑,刪除最后一條)
-(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{
}

// 此方法是用來重新繪制當(dāng)前視圖

  • (void)drawRect:(CGRect)rect {
    // 設(shè)置畫筆(顏色)
    for (UIBezierPath* bezierPath in self.allLineMutableArray) {
    UIColor myColor = [self.allColorMutableArray objectAtIndex:[self.allLineMutableArray indexOfObject:bezierPath]];
    [myColor setStroke];
    // 設(shè)置的線條的寬度
    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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末幢炸,一起剝皮案震驚了整個濱河市宛徊,隨后出現(xiàn)的幾起案子逻澳,更是在濱河造成了極大的恐慌赡盘,老刑警劉巖陨享,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件抛姑,死亡現(xiàn)場離奇詭異定硝,居然都是意外死亡蔬啡,警方通過查閱死者的電腦和手機(jī)诲侮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門沟绪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绽慈,“玉大人坝疼,你說我怎么就攤上這事钝凶⊙溆埃” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵铆隘,是天一觀的道長膀钠。 經(jīng)常有香客問我肿嘲,道長筑公,這世上最難降的妖魔是什么匣屡? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任誉结,我火速辦了婚禮券躁,結(jié)果婚禮上也拜,老公的妹妹穿的比我還像新娘慢哈。我一直安慰自己岸军,他們只是感情好瓦侮,可當(dāng)我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布方妖。 她就那樣靜靜地躺著党觅,像睡著了一般。 火紅的嫁衣襯著肌膚如雪镐牺。 梳的紋絲不亂的頭發(fā)上睬涧,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天畦浓,我揣著相機(jī)與錄音讶请,去河邊找鬼夺溢。 笑死企垦,一個胖子當(dāng)著我的面吹牛晒来,可吹牛的內(nèi)容都是我干的湃崩。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼剪返!你這毒婦竟也來了脱盲?” 一聲冷哼從身側(cè)響起日缨,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤面哥,失蹤者是張志新(化名)和其女友劉穎尚卫,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蹲坷,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年县匠,在試婚紗的時候發(fā)現(xiàn)自己被綠了乞旦。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片兰粉。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡玖姑,死狀恐怖焰络,靈堂內(nèi)的尸體忽然破棺而出符喝,到底是詐尸還是另有隱情协饲,我是刑警寧澤茉稠,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站战惊,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜吞获,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一况凉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧各拷,春花似錦刁绒、人聲如沸烤黍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽速蕊。三九已至嫂丙,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間规哲,已是汗流浹背跟啤。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留唉锌,地道東北人隅肥。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像袄简,于是被迫代替她去往敵國和親腥放。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容