本节任务:创建一个视图,让用户在视图上拖动手指来画线。  


  UIView类能够重载4个方法来处理不同的触摸事件。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event   // 一个手指或多个手指触摸屏幕。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event   //一个或多个手指在视图上移动

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event   //一个或多个手指抬离屏幕时。

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event   //系统事件,在触摸结束前被其打断。

@property(nonatomic, getter=isMultipleTouchEnabled) BOOL multipleTouchEnabled   // 默认为NO,如果想要处理多手指触摸事件,则需设置为YES。

  一个UITouch对应在屏幕上的一个手指。如果一个视图开始拥有触摸对象,该视图将拥有触摸对象的生命周期。你不应该引用触摸对象。  


  创建一个新的Single View Application工程,命名为TouchTracker。

  首先,需要一个模型对象来描述line。创建一个NSObject的子类,取名为BNRLine。

  再创建一个UIView的子类,取名为BNRDrawView。BNRDrawView用来追踪全部画好了的线条以及目前正在画的线条。

  再创建一个UIViewController的子类来管理BNRDrawView对象,取名为BNRDrawViewController。

  

  在BNRLine.h头文件中,声明两个CGPoint属性,如下:

 @property (nonatomic) CGPoint begin;
@property (nonatomic) CGPoint end;

同时,将 #import <Foundation/Foundation.h> 改为 #import <UIKit/UIKit.h> 。

  打开BNRDrawViewController.m文件,重载loadView方法,将BNRDrawView对象设置为BNRDrawViewController的view。

当请求显示控制器的view时,但是为nil,则视图控制器会自动调用loadView方法。该方法应加载或创建一个视图,并将其赋给控制器的view属性。如果视图控制器有一个相对于的nib文件,loadView方法将从该nib文件加载view。如果你使用了Interface Builder创建了视图并初始化了视图控制器,你一定不能重载该方法。

  如果使用Interface Builder设计用户界面时,当视图对象从nib文件中加载视图时,initWithFrame:方法将不会被调用。

 - (void)loadView {
self.view = [[BNRDrawView alloc] initWithFrame:CGRectZero];
}

同时,在实现文件顶部导入BNRDrawView头文件,如下:

#import "BNRDrawView.h"

  打开AppDelegate.m文件,创建一个BNRDrawViewController对象,将其作为window的rootViewController。同时导入BNRDrawViewController的头文件, #import "BNRDrawViewController.h" 。

方法application:didFinishLaunchingWithOptions:方法修改如下:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
BNRDrawViewController *dvc = [[BNRDrawViewController alloc] init];
self.window.rootViewController = dvc; self.window.backgroundColor = [UIColor yellowColor];
[self.window makeKeyAndVisible];
return YES;
}

  打开BNRDrawView.m,导入BNRLine头文件,并在类扩展中声明两个属性,如下:

 #import "BNRLine.h"

 @interface BNRDrawView ()

 @property (nonatomic, strong) BNRLine *currentLine;
@property (nonatomic, strong) NSMutableArray *finishedLines; @end

同时,添加initWithFrame:代码如下:

 - (instancetype)initWithFrame:(CGRect)r {
self = [super initWithFrame:r];
if (self) {
self.finishedLines = [[NSMutableArray alloc] init];
self.backgroundColor = [UIColor grayColor];
}
return self;
}

同时,需要实现对finishedLine和currentLine的绘制。

当一个视图第一次显示或者一个事件的发生使得视图可见的部分失效了,则drawRect:方法会被调用。绝对不能直接调用该方法。为了使失效的部分重新绘制,需调用setNeedsDisplay或setNeedsDisplayInRect:方法。如下:

 - (void)strokeLike:(BNRLine *)line {
UIBezierPath *bp = [UIBezierPath bezierPath];
bp.lineWidth = ;
bp.lineCapStyle = kCGLineCapRound; [bp moveToPoint:line.begin];
[bp addLineToPoint:line.end];
[bp stroke];
} - (void)drawRect:(CGRect)rect {
[[UIColor blackColor] set];
for (BNRLine *line in self.finishedLines) {
[self strokeLike:line];
}
if (self.currentLine) {
[[UIColor redColor] set];
[self strokeLike:self.currentLine];
}
}

该工程的对象关系如下所示:


  当触摸事件开始时,需要创建一个line,将begin和end设置为触摸开始的点位置处。当手指移动时,将更新end。因此,打开BNRDrawView.m文件,touchesBegin:withEvent:方法实现如下:

 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *t = [touches anyObject];
CGPoint location = [t locationInView:self];
self.currentLine = [[BNRLine alloc] init];
self.currentLine.begin = location;
self.currentLine.end = location;
[self setNeedsDisplay];
}

  touchesMoved:withEvent:方法实现如下:

 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *t = [touches anyObject];
CGPoint location = [t locationInView:self];
self.currentLine.end = location;
[self setNeedsDisplay];
}

  当触摸事件结束时,将currentLine添加到finishedLines,touchesEnded:withEvent:实现如下:

 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.finishedLines addObject:self.currentLine];
self.currentLine = nil;
[self setNeedsDisplay];
}

  启动程序,当正在画线时,线条显示红色。当画线结束,将变为黑色。


  默认情况下,一个视图将一次只接受一个触摸。

  此时,需要一个属性来包含尽可能多的将在屏幕上画出的线条。打开BNRDrawView.m,用下列的属性代替currentLine。

@property (nonatomic, strong) NSMutableDictionary *linesInProgress;

修改BNRDrawView中initWithFrame:方法使其能接收多点触摸,并初始化linesInProgress属性,如下:

 - (instancetype)initWithFrame:(CGRect)r {
self = [super initWithFrame:r];
if (self) {
self.linesInProgress = [[NSMutableDictionary alloc] init];
self.finishedLines = [[NSMutableArray alloc] init];
self.backgroundColor = [UIColor grayColor];
self.multipleTouchEnabled = YES;
}
return self;
}

修改触摸响应事件如下:

 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"%@", NSStringFromSelector(_cmd)); for (UITouch *t in touches) {
CGPoint location = [t locationInView:self];
BNRLine *line = [[BNRLine alloc] init];
line.begin = location;
line.end = location;
NSValue *key = [NSValue valueWithNonretainedObject:t];
self.linesInProgress[key] = line;
}
[self setNeedsDisplay];
} - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"%@", NSStringFromSelector(_cmd)); for (UITouch *t in touches) {
NSValue *key = [NSValue valueWithNonretainedObject:t];
BNRLine *line = self.linesInProgress[key];
line.end = [t locationInView:self];
} [self setNeedsDisplay];
} - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"%@", NSStringFromSelector(_cmd)); for (UITouch *t in touches) {
NSValue *key = [NSValue valueWithNonretainedObject:t];
BNRLine *line = self.linesInProgress[key];
[self.finishedLines addObject:line];
[self.linesInProgress removeObjectForKey:key];
}
[self setNeedsDisplay];
}

  其中,我们使用valueWithNonretainedObject:方法来产生key用于保存BNRLine。该方法产生的NSValue对象将拥有与该lian有关的UITouch对象的地址。在每次触摸事件中。UITouch对象的地址是保持不变的。

  在一个NSDictionary中,object使用的key必须遵守NSCopying协议,该协议允许他们接收copy方法。

最后,修改drawRect:方法:

 - (void)drawRect:(CGRect)rect {
[[UIColor blackColor] set];
for (BNRLine *line in self.finishedLines) {
[self strokeLike:line];
}
[[UIColor redColor] set];
for (NSValue *key in self.linesInProgress) {
[self strokeLike:self.linesInProgress[key]];
}
}

  最后,添加touchesCancelled:withEvent:方法。当一个触摸事件取消时,应将在linesInProgress中的线条全部移除。如下:

 - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"%@", NSStringFromSelector(_cmd));
for (UITouch *t in touches) {
NSValue *key = [NSValue valueWithNonretainedObject:t];
[self.linesInProgress removeObjectForKey:key];
}
[self setNeedsDisplay];
}

程序代码:http://pan.baidu.com/s/1jGKYYt4

UITouch - BNR的更多相关文章

  1. UIGestureRecognizer - BNR

    继续上篇UITouch - BNR.该篇将实现线条选择.移动和删除操作. UIGestureRecognizer有一系列子类,每一个子类都用于识别特定的手势.当识别出一个手势时,手势识别器会拦截视图的 ...

  2. 你真的了解UIEvent、UITouch吗?

    一:首先查看一下关于UIEvent的定义 //事件类型 typedef NS_ENUM(NSInteger, UIEventType) { UIEventTypeTouches, UIEventTyp ...

  3. iOS开发——UI进阶篇(十二)事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别

    触摸事件 在用户使用app过程中,会产生各种各样的事件 一.iOS中的事件可以分为3大类型 触摸事件加速计事件远程控制事件 响应者对象在iOS中不是任何对象都能处理事件,只有继承了UIResponde ...

  4. iOS - UITouch

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITouch : NSObject @available(iOS 2.0, *) public class UIT ...

  5. UITouch的用法

    UITouch一般无法直接获取,是通过UIView的touchesBegan等函数获得. //这四个方法是UIResponder中得方法 // Generally, all responders wh ...

  6. 触摸事件UITouch的用法

    触摸屏幕是iOS设备接受用户输入的主要方式,包括单击.双击.拨动以及多点触摸等,这些操作都会产生触摸事件. 在Cocoa中,代表触摸对象的类是UITouch.当用户触摸屏幕后,就会产生相应的事件,所有 ...

  7. UITouch 触摸事件处理(实例)

    来源:http://www.open-open.com/lib/view/open1341882439838.html 1. UITouch 的主要方法: - (void)touchesBegan:( ...

  8. UITouch触摸事件

    UITouch触摸事件 主要为三个方法 1.-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{2.3. UITouch * ...

  9. UITouch附加

    框架 /System/Library/Frameworks/SpriteKit.framework 可用性 可用于iOS 7.0或者更晚的版本 声明于 SKNode.h 概览 重要提示:这是一个初步的 ...

随机推荐

  1. 线程组ThreadGroup分析详解 多线程中篇(三)

    线程组,顾名思义,就是线程的组,逻辑类似项目组,用于管理项目成员,线程组就是用来管理线程. 每个线程都会有一个线程组,如果没有设置将会有些默认的初始化设置 而在java中线程组则是使用类ThreadG ...

  2. nginx部署dotnet core站点

    步骤 aspnetcore程序端口号5001,实际外部端口号8001,相当于把8001收到的请求转发给5001. 把发布出来的文件全部丢掉 /var/www/JuXiangTou 里面去.可以用scp ...

  3. 跨站点请求伪造(CSRF)学习

    一.CSRF介绍 伪造一个站点,在站点中伪造一个向其他站点的请求,在用户访问该站点时让用户执行 假设有如下URL能删除一篇文章: 攻击者在自己的域中构造一个页面: 内容为: 使用一个img标签,其地址 ...

  4. JDK源码分析(四)—— ConcurrentHashMap

    一.概述 ConcurrentHashMap是Java5中新增加的一个线程安全的Map集合,可以用来替代HashTable. 锁分段技术 原理:将数据分成一段一段的存储,然后给每一段数据配一把锁,当一 ...

  5. 微信小程序支付接入注意点

    一.微信支付后台服务器部署 服务器采用ubuntu16.04 + php7.0 + apache2.0. 微信支付后台服务使用了curl 和 samplexml ,因此php.ini配置中必须开启这两 ...

  6. 无依赖简单易用的Dynamics 365实体记录数计数器并能计算出FetchXml返回的记录数

    本人微信公众号:微软动态CRM专家罗勇 ,回复278或者20180812可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 我们 ...

  7. 广州.NET微软技术俱乐部与其他技术群的区别

    .NET和微软技术相关的微信群有不少, 那么广州.NET微软技术俱乐部与其他技术群的区别是什么呢? 有十分大的区别! 本群公告写得很清楚! 本群坚决禁止讨论“JAVA和.NET哪个好”.“NET有没有 ...

  8. asp.net/wingtip/UI和导航

    ASP.NET Web窗体可以让web应用创建动态内容.静态网页的知识和HTML.CSS一模一样,区别在于ASP.NET网页包括了ASP.NET可以识别并处理的服务器端的内容.对于静态页面(.html ...

  9. 互联网安全中心(CIS)发布新版20大安全控制

    这些最佳实践最初由SANS研究所提出,名为“SANS关键控制”,是各类公司企业不可或缺的安全控制措施.通过采纳这些控制方法,公司企业可防止绝大部分的网络攻击. 有效网络防御的20条关键安全控制 对上一 ...

  10. 作为JavaScript开发人员,这些必备的VS Code插件你都用过吗?

    本文翻译自:https://www.sitepoint.com/vs-code-extensions-javascript-developers/ 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的 ...