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(delXdelX);
float sqrtY = sqrt(delYdelY);
// 確定滑動方向
// 情況一 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