iOS开发:手势识别与网络异步下载全解析
立即解锁
发布时间: 2025-08-17 01:56:35 阅读量: 31 订阅数: 29 AIGC 


iOS 6编程实战指南
### iOS开发:手势识别与网络异步下载全解析
#### 1. iOS手势识别概述
在iOS开发中,手势识别是提升用户交互体验的重要部分。通过不同的手势识别器,能让用户以直观的方式与应用进行交互,以下将详细介绍几种常见的手势识别器及其应用。
#### 2. 平移手势识别器(UIPanGestureRecognizer)
平移手势识别器 `UIPanGestureRecognizer` 可以检测用户的平移手势。在识别平移手势时,它会经历以下几个状态:
1. `UIGestureRecognizerStateBegan`
2. `UIGestureRecognizerStateChanged`
3. `UIGestureRecognizerStateEnded`
我们可以按照以下方式实现手势识别器的目标方法,让标签的中心随着用户手指的移动而持续移动:
```objc
- (void) handlePanGestures:(UIPanGestureRecognizer*)paramSender{
if (paramSender.state != UIGestureRecognizerStateEnded &&
paramSender.state != UIGestureRecognizerStateFailed){
CGPoint location = [paramSender locationInView:paramSender.view.superview];
paramSender.view.center = location;
}
}
```
为了能在视图控制器的视图上移动标签,我们需要获取手指在视图上的位置,而不是标签上的位置。因此,我们调用了平移手势识别器的 `locationInView:` 方法,并将标签的父视图作为目标视图传入。
可以使用平移手势识别器的 `locationInView:` 方法来查找当前平移手指的位置。若要检测多个手指的位置,则使用 `locationOfTouch:inView:` 方法。通过 `UIPanGestureRecognizer` 的 `minimumNumberOfTouches` 和 `maximumNumberOfTouches` 属性,你可以同时检测多个平移触摸。在示例中,为了简单起见,我们只检测一个手指。
在 `UIGestureRecognizerStateEnded` 状态下,报告的 x 和 y 值可能不是数字,即可能等于 `NAN`。所以,我们需要避免在这个特定状态下使用报告的值。
#### 3. 长按手势识别器(UILongPressGestureRecognizer)
##### 3.1 问题与解决方案
如果你想检测用户在视图上点击并按住手指一段时间的操作,可以创建一个 `UILongPressGestureRecognizer` 类的实例,并将其添加到需要检测长按手势的视图中。
视图控制器的 `.h` 文件定义如下:
```objc
#import <UIKit/UIKit.h>
@interface Detecting_Long_Press_GesturesViewController : UIViewController
@property (nonatomic, strong)
UILongPressGestureRecognizer *longPressGestureRecognizer;
@property (nonatomic, strong) UIButton *dummyButton;
@end
```
视图控制器的 `viewDidLoad` 实例方法如下:
```objc
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.dummyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.dummyButton.frame = CGRectMake(0.0f,
0.0f,
72.0f,
37.0f);
self.dummyButton.center = self.view.center;
[self.view addSubview:self.dummyButton];
/* First create the gesture recognizer */
self.longPressGestureRecognizer =
[[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPressGestures:)];
/* The number of fingers that must be present on the screen */
self.longPressGestureRecognizer.numberOfTouchesRequired = 2;
/* Maximum 100 points of movement allowed before the gesture
is recognized */
self.longPressGestureRecognizer.allowableMovement = 100.0f;
/* The user must press two fingers (numberOfTouchesRequired) for
at least one second for the gesture to be recognized */
self.longPressGestureRecognizer.minimumPressDuration = 1.0;
/* Add this gesture recognizer to the view */
[self.view addGestureRecognizer:self.longPressGestureRecognizer];
}
```
如果长按手势识别器在用户端手势持续进行时向接收对象发送事件,而此时有电话或其他中断进来,手势识别器的状态将变为 `UIGestureRecognizerStateCancelled`。在用户再次发起启动识别过程所需的操作(在此示例中,在视图控制器的视图上用两个手指按住至少一秒)之前,该手势识别器不会再向接收对象发送任何消息。
##### 3.2 长按手势识别器的重要属性
长按手势识别器有四个重要属性会影响其识别行为,具体如下:
| 属性 | 描述 | 默认值 |
| ---- | ---- | ---- |
| `numberOfTapsRequired` | 用户在触发手势之前必须在目标视图上执行的点击次数。点击是指将手指放在屏幕上并抬起的动作。 | 0 |
| `numberOfTouchesRequired` | 在识别手势之前必须触摸屏幕的手指数量。如果 `numberOfTapsRequired` 属性设置为大于 0 的值,则必须指定相同数量的手指来检测点击。 | 无 |
| `allowableMovement` | 在手势识别中止之前,屏幕上的手指可以移动的最大像素数。 | 无 |
| `minimumPressDuration` | 用户必须将手指按在屏幕上的时间(以秒为单位),才能检测到手势事件。 | 无 |
在示例中,这些属性的设置如下:
- `numberOfTapsRequired`:默认值(我们未更改此值)
- `numberOfTouchesRequired`:2
- `allowableMovement`:100
- `minimumPressDuration`:1
有了这些值,只有当用户按下屏幕并按住两个手指一秒钟(`minimumPressDuration`),且手指移动不超过 100 像素(`allowableMovement`)时,长按手势才会被识别。
当手势被识别时,会调用 `handleLongPressGestures:` 方法,我们可以按以下方式实现:
```objc
- (void) handleLongPressGestures:(UILongPressGestureRecognizer *)paramSender{
/* Here we want to find the midpoint of the two fingers
that caused the long press gesture to be recognized. We configured
this number using the numberOfTouchesRequired property of the
UILongPressGestureRecognizer that we instantiated in the
viewDidLoad instance method of this View Controller. If we
find that another long press gesture recognizer is using this
method as its target, we will ignore it */
if ([paramSender isEqual:self.longPressGestureRecognizer]){
if (paramSender.numberOfTouchesRequired == 2){
CGPoint touchPoint1 =
[paramSender locationOfTouch:0
inView:paramSender.view];
CGPoint touchPoint2 =
[paramSender locationOfTouch:1
inView:paramSender.view];
CGFloat midPointX = (touchPoint1.x + touchPoint2.x) / 2.0f;
CGFloat midPointY = (touchPoint1.y + touchPoint2.y) / 2.0f;
CGPoint midPoint = CGPointMake(midPointX, midPointY);
self.dummyButton.center = midPoint;
} else {
/* This is a long press gesture recognizer with more
or less than 2 fingers */
}
}
}
```
iOS 中的地图应用就是使用长按手势识别器的一个例子。在该应用中,当你查看不同位置时,将手指按在特定位置并按住一段时间而不抬起,会在该特定位置放下一个图钉。
#### 4. 点击手势识别器(UITapGestureRecognizer)
##### 4.1 问题与解决方案
如果你想检测用户点击视图的操作,可以创建一个 `UITapGestureRecognizer` 类的实例,并使用 `UIView` 类的 `addGestureRecognizer:` 实例方法将其添加到目标视图中。
视图控制器的 `.h` 文件定义如下:
```objc
#import <UIKit/UIKit.h>
@interface Detecting_Tap_GesturesViewController : UIViewController
@property (nonatomic, strong)
UITapGestureRecognizer *tapGestureRecognizer;
@end
```
视图控制器的 `viewDidLoad` 实例方法实现如下:
```objc
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
/* Create the Tap Gesture Recognizer */
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleTaps:)];
/* The number of fingers that must be on the screen */
self.tapGestureRecognizer.numberOfTouchesRequired = 2;
/* The total number of taps to be performed before the
gesture is recognized */
self.tapGestureRecognizer.numberOfTapsRequired
```
0
0
复制全文
相关推荐










