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(delXdelX);
float sqrtY = sqrt(delYdelY);
// 确定滑动方向
// 情况一 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
网友评论