iphone中的UITouch



手指在屏幕上能达到的精度和鼠标指针有很大的不同。当用户触击屏幕时,接触区域实际上是椭圆形的,而且比用户想像的位置更靠下一点。根据触摸屏幕的手指、手指的尺寸、手指接触屏幕的力量、手指的方向、以及其它因素的不同,其“接触部位”的尺寸和形状也有所不同。底层的多点触摸系统会分析所有的这些信息,为您计算出单一的触点。

 

UIResponder 是所有响应者对象的基类,

它不仅为事件处理,而且也为常见的响应者行为定义编程接口。UIApplication、UIView、和所有从UIView 派生出来的UIKit 类(包括UIWindow)都直接或间接地继承自UIResponder类。

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch* touch = [touches anyObject];

NSUInteger numTaps = [touch tapCount];

if (numTaps < 2) {

[self.nextResponder touchesBegan:touches withEvent:event];

} else {

[self handleDoubleTap:touch];

}

}

 

缺省情况下,视图会接收触摸事件。但是,您可以将其userInteractionEnabled

属性声明设置为NO,关闭事件传递的功能。

 

在一定的时间内关闭事件的传递。应用程序可以调用UIApplication 的

beginIgnoringInteractionEvents 方法,并在随后调用endIgnoringInteractionEvents 方法来实现这个目的。

 

缺省情况下,视图只接收多点触摸序列的第一个触摸事件,而忽略

所有其它事件。如果您希望视图处理多点触摸,就必须使它启用这个功能。在代码或Interface Builder 的查看器窗口中将视图的multipleTouchEnabled 属性设置为YES,就可以实现这个目标。

将事件传递限制在某个单独的视图上。缺省情况下,视图的exclusiveTouch 属性被设置为NO。将这个属性设置为YES 会使相应的视图具有这样的特性:即当该视图正在跟踪触摸动作时,窗口中的其它视图无法同时进行跟踪,它们不能接收到那些触摸事件。

多点触摸:

- (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

 

当一个或多个手指触碰屏幕时,发送touchesBegan:withEvent:消息。

当一个或多个手指在屏幕上移动时,发送touchesMoved:withEvent:消息。

当一个或多个手指离开屏幕时,发送touchesEnded:withEvent:消息。

当触摸序列被诸如电话呼入这样的系统事件所取消时,发送touchesCancelled:withEvent:消息。

上面这些方法都和特定的触摸阶段(比如UITouchPhaseBegan)相关联,该信息存在于UITouch 对象的phase 属性声明中。

为了处理给定阶段的事件,响应者对象常常从传入的集合参数中取得一或多个UITouch 对象,然后考察这些对象的属性或取得它们的位置(如果需要处理所有触摸对象,可以向该NSSet 对象发送anyObject 消息)。UITouch 类中有一个名为locationInView:的重要方法,如果传入self 参数值,它会给出触摸动作在响应者坐标系统中的位置(假定该响应者是一个UIView 对象,且传入的视图参数不为nil)。另外,还有一个与之平行的方法,可以给出触摸动作之前位置(previousLocationInView:)。UITouch 实例的属性还可以给出发生多少次触

碰(tapCount)、触摸对象的创建或最后一次变化发生在什么时间(timestamp)、以及触摸处于什么阶段(phase)。

 

- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event

{

UITouch *touch = [touches anyObject];

if ([touch tapCount] == 2) {

CGPoint tapPoint = [theTouch locationInView:self];

// Process a double-tap gesture

}

}

在touchesEnded:withEvent:方法中,当触击次数为一时,响应者对象就向自身发送一个performSelector:withObject:afterDelay:消息,其中的选择器标识由响应者对象实现的、用于处理单击手势的方法;第二个参数是一个NSValue 或NSDictionary 对象,用于保存相关的UITouch 对象;时延参数则表示单击和双击手势之间的合理时间间隔。

在touchesBegan:withEvent:方法中,如果触击次数为二,响应者对象会向自身发送一个cancelPreviousPerformRequestsWithTarget:消息,取消当前被挂起和延期执行的调用。如果触碰次数不为二,则在指定的延时之后,先前步骤中由选择器标识的方法就会被调用,以处理单击手势。

 

在视图中跟踪碰擦手势

#define HORIZ_SWIPE_DRAG_MIN 12

#define VERT_SWIPE_DRAG_MAX 4

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

UITouch *touch = [touches anyObject];

startTouchPosition = [touch locationInView:self];

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

UITouch *touch = [touches anyObject];

CGPoint currentTouchPosition = [touch locationInView:self];

// If the swipe tracks correctly.

if (fabsf(startTouchPosition.x - currentTouchPosition.x) >= HORIZ_SWIPE_DRAG_MIN

&&

fabsf(startTouchPosition.y - currentTouchPosition.y) <=

VERT_SWIPE_DRAG_MAX)

{

// It appears to be a swipe.

if (startTouchPosition.x < currentTouchPosition.x)

[self myProcessRightSwipe:touches withEvent:event];

else

[self myProcessLeftSwipe:touches withEvent:event];

}

else

{

// Process a non-swipe event.

}

}

 

处理复杂的多点触摸序列

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

UITouch *touch = [[event allTouches] anyObject];

// Only move the placard view if the touch was in the placard view

if ([touch view] != placardView) {

// On double tap outside placard view, update placard's display string

if ([touch tapCount] == 2) {

[placardView setupNextDisplayString];

}

return;

}

// "Pulse" the placard view by scaling up then down

// Use UIView's built-in animation

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:0.5];

CGAffineTransform transform = CGAffineTransformMakeScale(1.2, 1.2);

placardView.transform = transform;

[UIView commitAnimations];

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:0.5];

transform = CGAffineTransformMakeScale(1.1, 1.1);

placardView.transform = transform;

[UIView commitAnimations];

// Move the placardView to under the touch

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:0.25];

placardView.center = [self convertPoint:[touch locationInView:self]

fromView:placardView];

[UIView commitAnimations];

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

UITouch *touch = [[event allTouches] anyObject];

// If the touch was in the placardView, move the placardView to its location

if ([touch view] == placardView) {

CGPoint location = [touch locationInView:self];

location = [self convertPoint:location fromView:placardView];

placardView.center = location;

return;

}

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

UITouch *touch = [[event allTouches] anyObject];

// If the touch was in the placardView, bounce it back to the center

if ([touch view] == placardView) {

// Disable user interaction so subsequent touches don't interfere with animation

self.userInteractionEnabled = NO;

[self animatePlacardViewToCenter];

return;

}

}

 

在事件处理代码中,您可以将触摸状态的相关位置保存下来,以便在必要时和变化之后的UITouch 实例进行比较。定制视图可以用UIView 的hitTest:withEvent:方法或CALayer 的hitTest:方法来寻找接收触摸事件的子视图或层,进而正确地处理事件。下面的例子用于检测定制视图的层中的“Info” 图像是否被触碰。

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {

CGPoint location = [[touches anyObject] locationInView:self];

CALayer *hitLayer = [[self layer] hitTest:[self convertPoint:location

fromView:nil]];

if (hitLayer == infoImage) {

[self displayInfo];

}

}

 

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {

if ([touches count] == [[event touchesForView:self] count]) {

// last finger has lifted....

}

}

 

触摸事件中包含一个触摸对象的集合及其相关的状态,而运动事件中除了事件类型、子类型、和时间戳之外,没有其它状态。系统以这种方式来解析运动手势,避免和方向变化事件造成冲突。

 

为了处理运动事件,UIResponder 的子类必须实现motionBegan:withEvent: 或

motionEnded:withEvent:方法之一,或者同时实现这两个方法

UIKit 框架在UITextView、UITextField、和UIWebView 类中实现了拷贝-剪切-粘贴支持。如果您希望在自己的应用程序中得到这个行为,可以使用这些类的对象,或者自行实现。

 

UIPasteboard 类提供了粘贴板的接口。粘贴板是用于在一个应用程序内或不同应用程序间进行数据共享的受保护区域。该类提供了读写剪贴板上数据项目的方法。

UIMenuController 类可以在选定的拷贝、剪切、和粘贴对象的上下方显示一个编辑菜单。编辑菜单上的命令可以有拷贝、剪切、粘贴、选定、和全部选定。

UIResponder 类声明了canPerformAction:withSender:方法。响应者类可以实现这个方法,以根据当前的上下文显示或移除编辑菜单上的命令。

UIResponderStandardEditActions 非正式协议声明了处理拷贝、剪切、粘贴、选定、和全部选定命令的接口。当用户触碰编辑菜单上的某个命令时, 相应的

UIResponderStandardEditActions 方法就会被调用。

 

UIPasteboardNameGeneral 用于剪切、拷贝、和粘贴操作,涉及到广泛的数据类型。您可以通过该类的generalPasteboard 类方法来取得代表通用(General)粘贴板的单件对象。

UIPasteboardNameFind 用于检索操作。当前用户在检索条(UISearchBar)键入的字符串会被写入到这个粘贴板中, 因此可以在不同的应用程序中共享。您可以通过调用pasteboardWithName:create:类方法,并在名字参数中传入UIPasteboardNameFind 值来取得代表检索粘贴板的对象。典型情况下, 您只需使用系统定义的粘贴板就够了。但在必要时, 您也可以通过pasteboardWithName:create: 方法来创建自己的应用程序粘贴板。如果您调用pasteboardWithUniqueName 方法,UIPasteboard 会为您提供一个具有唯一名称的应用程序粘贴板。您可以通过其name 属性声明来取得这个名称。

 

调用UIMenuController 的sharedMenuController 类方法来取得全局的,即菜单控制器实例。

计算选定内容的边界,并用得到的边界矩形调用setTargetRect:inView:方法。系统会根据选定内容与屏幕顶部和底部的距离,将编辑菜单显示在该矩形的上方或下方。

调用setMenuVisible:animated:方法(两个参数都传入YES),在选定内容的上方或下方动画显示编辑菜单。

程序清单3-4演示了如何在touchesEnded:withEvent:方法的实现中显示编辑菜单(注意,例子中省略了处理选择的代码)。在这个代码片段中, 定制视图还向自己发送一个becomeFirstResponder 消息,确保自己在随后的拷贝、剪切、和粘贴操作中是第一响应者。

 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *theTouch = [touches anyObject];

if ([theTouch tapCount] == 2 && [self becomeFirstResponder]) {

// selection management code goes here...

// bring up editing menu.

UIMenuController *theMenu = [UIMenuController sharedMenuController];

CGRect selectionRect = CGRectMake(currentSelection.x, currentSelection.y, SIDE,SIDE);

[theMenu setTargetRect:selectionRect inView:self];

[theMenu setMenuVisible:YES animated:YES];

}

}

 

有条件地激活菜单命令

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {

BOOL retValue = NO;

ColorTile *theTile = [self colorTileForOrigin:currentSelection];

if (action == @selector(paste:) )

retValue = (theTile == nil) &&

[[UIPasteboard generalPasteboard]

containsPasteboardTypes:

[NSArray arrayWithObject:ColorTileUTI]];

else if ( action == @selector(cut:) || action == @selector(copy:) )

retValue = (theTile != nil);

else

retValue = [super canPerformAction:action

withSender:sender];

return retValue;

}

请注意,这个方法的最后一个else 子句调用了超类的实现,使超类有机会处理子类忽略的命令。

 

拷贝和剪切操作

- (void)copy:(id)sender {

UIPasteboard *gpBoard = [UIPasteboard generalPasteboard];

ColorTile *theTile = [self colorTileForOrigin:currentSelection];

if (theTile) {

NSData *tileData = [NSKeyedArchiver

archivedDataWithRootObject:theTile];

if (tileData)

[gpBoard setData:tileData forPasteboardType:ColorTileUTI];

}

}

- (void)cut:(id)sender {

[self copy:sender];

ColorTile *theTile = [self colorTileForOrigin:currentSelection];

if (theTile) {

CGPoint tilePoint = theTile.tileOrigin;

[tiles removeObject:theTile];

CGRect tileRect = [self rectFromOrigin:tilePoint inset:TILE_INSET];

[self setNeedsDisplayInRect:tileRect];

}

}

 

将粘贴板的数据粘贴到选定位置上

- (void)paste:(id)sender {

UIPasteboard *gpBoard = [UIPasteboard generalPasteboard];

NSArray *pbType = [NSArray arrayWithObject:ColorTileUTI];

ColorTile *theTile = [self colorTileForOrigin:currentSelection];

if (theTile == nil && [gpBoard containsPasteboardTypes:pbType]) {

NSData *tileData = [gpBoard dataForPasteboardType:ColorTileUTI];

ColorTile *theTile = (ColorTile *)[NSKeyedUnarchiver

unarchiveObjectWithData:tileData];

if (theTile) {

theTile.tileOrigin = self.currentSelection;

[tiles addObject:theTile];

CGRect tileRect = [self rectFromOrigin:currentSelection inset:TILE_INSET];

[self setNeedsDisplayInRect:tileRect];

}

}

}

 

 Quartz 是主要的描画接口,支持基于路径的描画、

抗锯齿渲染、渐变填充模式、图像、颜色、坐标空间变换、以及PDF 文档的创建、显示、和分析。UIKit 为Quartz 的图像和颜色操作提供了Objective-C 的封装。Core Animation 为很多UIKit 的视图属性声明的动画效果提供底层支持,也可以用于实现定制的动画。

在调用您提供的drawRect:方法之前,视图对象会自动配置其描画环境,使您的代码可以立即进行描画。作为这些配置的一部分,UIView 对象会为当前描画环境创建一个图形上下文(对应于CGContextRef 封装类型)

......

转自:http://www.2cto.com/kf/201108/99935.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值