http://www.techotopia.com/index.php/An_iPhone_iOS_4_Gesture_Recognition_Tutorial_%28Xcode_4%29
在上一章,我们已经概要介绍了iPhone手势识别技的概念,本章的目的是通过一个实例演示各种UIGestureRecognizer子类的用法。本章所创建的程序,将创建和设置各种不同的手势识别器,然后用标签把每个侦测到的手势的具体信息显示出来。
创建项目Creating the Gesture Recognition Project
打开Xcode,创建一个View-baseapplication项目,命名为recognizer。
为Label设置出口
在View对象中,仅有一个label组件,用于显示侦测到的用户手势类型。label上的文字在程序运行过程中会被代码所改变,因此我们需要为它连接到一个出口上。在xcode的项目导航面板中,选中recognizerViewController.h文件,将它修改为:
#import <UIKit/UIKit.h>
@interface recognizerViewController: UIViewController {
UILabel *statusLabel;
}
@property (retain, nonatomic) IBOutlet UILabel *statusLabel;
@end
接着,编辑recognizerViewController.m。在@synthesize语句中增加相应出口并在合适的地方释放它。
#import "recognizerViewController.h"
@implementation recognizerViewController
@synthesize statusLabel;
. .
- (void)dealloc {
[statusLabel release];
[super dealloc];
}
. .
- (void)viewDidUnload {
[super viewDidUnload];
self.statusLabel = nil;
}
. .
@end
设计界面
选择recognizerViewController.xib,在IB面板中编辑它。Xcode在创建项目时为我们创建了一个UIView,我们需要在这个UIView中增加一个label。从ObjectLibrary (View-> Utilities -> Object Library) 中拖一个Label对象,然后修改label属性让文本居中对齐:
图片
Ctrl+左键(或者右键)从File’sOwner 拖一条线到Label对象,然后释放按键。在弹出的菜单中选择statusLabel出口。
设置GusetureRecognizers对象
我们需要在代码中使用gesturerecognizers来识别轻击、轻扫、旋转和捏合。由于这些recognizers需要连接到view对象,因此创建它们的理想地是recognizerViewController类的viewDidLoad方法:
- (void)viewDidLoad {
UITapGestureRecognizer *doubleTap =
[[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(tapDetected:)];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
[doubleTap release];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc]
initWithTarget:self
action:@selector(pinchDetected:)];
[self.view addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc]
initWithTarget:self
action:@selector(rotationDetected:)];
[self.view addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:@selector(swipeDetected:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(longPressDetected:)];
longPressRecognizer.minimumPressDuration = 3;
longPressRecognizer.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];
[super viewDidLoad];
}
添加处理方法
设置完gesturerecognizer之后,就应当编写处理方法,处理方法将在相应手势侦测到之后被recognizer所调用。这些方法也放在了recognizerViewController.m文件里,同时这些方法会根据相应手势的具体信息来刷新label上显示的文字。
-(IBAction)longPressDetected:(UIGestureRecognizer *)sender{
statusLabel.text = @"Long Press";
}
- (IBAction)swipeDetected:(UIGestureRecognizer *)sender {
statusLabel.text = @"Right Swipe";
}
- (IBAction)tapDetected:(UIGestureRecognizer *)sender {
statusLabel.text = @"Double Tap";
}
- (IBAction)pinchDetected:(UIGestureRecognizer *)sender {
CGFloat scale = [(UIPinchGestureRecognizer *)sender scale];
CGFloat velocity = [(UIPinchGestureRecognizer *)sender velocity];
NSString *resultString = [[NSString alloc] initWithFormat:
@"Pinch - scale = %f, velocity = %f",
scale, velocity];
statusLabel.text = resultString;
[resultString release];
}
- (IBAction)rotationDetected:(UIGestureRecognizer *)sender {
CGFloat radians = [(UIRotationGestureRecognizer *)sender rotation];
CGFloat velocity = [(UIRotationGestureRecognizer *)sender velocity];
NSString *resultString = [[NSString alloc] initWithFormat:
@"Rotation - Radians = %f, velocity = %f",
radians, velocity];
statusLabel.text = resultString;
[resultString release];
}
测试程序
最后,编译和运行程序。为了能够充分测试捏合和旋转手势,最好是在物理设备中运行程序(因为模拟器上无法模拟多点触摸)。连接已激活的调试设备(参考TestingiOS 4 Apps on the iPhone – Developer Certificates and Provisioning Profiles),然后点击Xcode的Run按钮。当程序运行后,进行手势动作并观察label中显示文本的变化。
分享到:
相关推荐
iOS游戏应用源代码——xemus-cocos2d-GestureRecognizers-c87d379.zip
IOS应用源码——xemus-cocos2d-GestureRecognizers-c87d379.rar
ios源码之整合cocos2d手势识别-GestureRecognizers.rar
xemus-cocos2d-GestureRecognizers-c87d379.zipIOS应用例子源码下载xemus-cocos2d-GestureRecognizers-c87d379.zipIOS应用例子源码下载 1.适合学生学习研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
NULL 博文链接:https://poolo.iteye.com/blog/2040245
55. Identifying Gestures using iOS 10 Gesture Recognizers 56. An iOS 10 Gesture Recognition Tutorial 57. A 3D Touch Force Handling Tutorial 58. An iOS 10 3D Touch Quick Actions Tutorial 59. An iOS 10 ...
详尽的列出了UITextField的各个使用,并有汉字说明
Creating direct touch-based interfaces with multi-touch, gestures, and custom gesture recognizers Building and customizing controls in powerful ways Adding novel motion effects Alerting users via pop-...
Identifying Gestures using iOS 8 Gesture Recognizers Chapter 52. An iOS 8 Gesture Recognition Tutorial Chapter 53. Implementing TouchID Authentication in iOS 8 Apps Chapter 54. An Overview of iOS 8 ...
Identifying Gestures using iOS 9 Gesture Recognizers Chapter 56. An iOS 9 Gesture Recognition Tutorial Chapter 57. A 3D Touch Force Handling Tutorial Chapter 58. An iOS 9 3D Touch Quick Actions ...
Touches_GestureRecognizers(图片移动) TimingCurve(篮球落下弹起效果) textbtn(点击空白地方,键盘消失) TableViewImgView(image动画) TableFlipDemo(tableView翻转) TabBar+Table+CustomTitleView SplashDemo(开场...
Get working examples for implementing gesture recognizers Play audio and video files and access the iPod library Retrieve contacts and groups from the Address Book Determine camera availability and ...
Define the layout of UI elements with Cocoa Auto Layout Develop location-aware apps Get working examples for implementing gesture recognizers Use new Objective-C Runtime features Play audio and video...
有的时候我们并不想要UITextField控件在长按或双击等动作时显示复制,粘贴的那个控件,这时就用到了gestureRecognizers这个成员变量。gestureRecognizers里面存放的是textField的行为,我们想要让TextField不响应...
本文将会利用上篇教程【如何在iPhone上绘制出一个笑脸】,在原有的基础上加入【手势】进行放大缩小的功能。
用法做一个npm i react-native-gesture-recognizers ,然后尝试以下示例之一!基本平移示例import React , { Component , Text , View , Animated } from 'react-native' ;import { pannable } from 'react-native-...
这个方法一般在一个手势接收者要阻止另外一个手势接收自己的消息的时候调用,如果返回YES,则两个gesture recognizers可同时识别,如果返回NO,则并不保证两个gesture recognizers必不能同时识别,因为另外
Making the most of gesture recognizers Creating and distributing “passes” Debugging, instrumenting, and profiling apps Table of Contents Chapter 1. UIKit Dynamics Chapter 2. Core Location, MapKit, ...
he Gestures library provides gesture recognizers to detect gestures through touch events that occur when you place one or more fingers on a touch screen.
GESTURE