tap是指輕觸手勢。類似滑鼠操作的點選。從iOS 3.2版本開始支援完善的手勢api:
- tap:輕觸
- long press:在一點上長按
- pinch:兩個指頭捏或者放的操作
- pan:手指的拖動
- swipe:手指在螢幕上很快的滑動
- rotation:手指反向操作
這為開發者編寫手勢識别操作,提供了很大的友善,想想之前用android寫手勢滑動的代碼(編寫android簡單的手勢切換視圖示例),尤其感到幸福。
這裡寫一個簡單的tap操作。在下面視圖的藍色視圖内增加對tap的識别:

當用手指tap藍色視圖的時候,列印日志輸出:
代碼很簡單,首先要聲明tap的recognizer:
UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
[infoView addGestureRecognizer:recognizer];
[recognizer release];
在這裡:
- initWithTarget:self,要引用到Controller,因為一般這部分代碼寫在controller中,用self;
- action:@selector(handleTapFrom:),指派一個方法名,用于當手勢事件發生後的回調;
- [infoView addGestureRecognizer:recognizer],為view注冊這個手勢識别對象,這樣當手指在該視圖區域内,可引發手勢,之外則不會引發
對應的回調方法:
-(void)handleTapFrom:(UITapGestureRecognizer *)recognizer{
NSLog(@">>>tap it");
}
controller相關方法完整的代碼(包含了一些與本文無關的視圖建構代碼):
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
//去掉最頂端的狀态攔
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
UIImage *image=[UIImage imageNamed:@"3.jpg"];
//建立背景視圖
self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
UIImageView *backgroudView=[[UIImageView alloc] initWithImage:image];
[self.view addSubview:backgroudView];
/*
UIToolbar *toolBar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 1024-70, 768, 70)];
toolBar.alpha=0.8;
toolBar.tintColor = [UIColor colorWithRed:.3 green:.5 blue:.6 alpha:.1];
NSArray *items=[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithTitle:@"test" style:UIBarButtonItemStyleDone target:self action:nil],nil];
toolBar.items=items;
[self.view addSubview:toolBar];
*/
UIView *bottomView=[[UIView alloc] initWithFrame:CGRectMake(0, 1024-70, 768, 70)];
bottomView.backgroundColor=[UIColor grayColor];
bottomView.alpha=0.8;
//UIButton *backButton=[[UIButton alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
UIButton *backButton=[UIButton buttonWithType: UIButtonTypeRoundedRect];
[backButton setTitle:@"ok" forState:UIControlStateNormal];
backButton.frame=CGRectMake(10, 15, 100, 40);
[bottomView addSubview:backButton];
[self.view addSubview:bottomView];
UIView *infoView=[[UIView alloc] initWithFrame:CGRectMake(200, 700, 768-400, 70)];
infoView.backgroundColor=[UIColor blueColor];
infoView.alpha=0.6;
infoView.layer.cornerRadius=6;
infoView.layer.masksToBounds=YES;
[self.view addSubview:infoView];
UITapGestureRecognizer *recognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
[infoView addGestureRecognizer:recognizer];
[recognizer release];
翻頁效果,類似下面的樣子:
![]()
UISwipeGestureRecognizer ---手指動作 在電子書應用中會很常見。這裡需要兩個要點:![]()
UISwipeGestureRecognizer ---手指動作
- 翻頁動畫
- 手勢上下輕掃(swipe)的處理
先說一下輕掃(swipe)的實作,可以參考編寫簡單的手勢示例:Tap了解手勢種類。
在viewDidLoad方法中注冊了對上、下、左、右四個方向輕松的處理方法:
- (void)viewDidLoad {
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:recognizer];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[super viewDidLoad];
可以看到,都是同一個方法,handleSwipeFrom。
在該方法中,再識别具體是哪個方向的輕掃手勢,比如判斷是向下的輕掃:
判斷是向上的輕掃:-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"Swipe received.");
if (recognizer.direction==UISwipeGestureRecognizerDirectionDown) {
NSLog(@"swipe down");
有關動畫的處理,比如向下(往回)翻頁,類似這樣:if (recognizer.direction==UISwipeGestureRecognizerDirectionUp) {
NSLog(@"swipe up");
向上(向前)翻頁,隻需改為:[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:0.7f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[currentView removeFromSuperview];
[self.view addSubview:contentView];
[UIView commitAnimations];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];如果是電子書,還需要考慮一個問題,就是有多個頁面(圖形),比如50頁。那麼需要有一個資料結構來儲存這些頁面的圖檔路徑:
- objc資料結構,比如數組
- sqlite資料庫表
這樣,寫一套翻頁代碼和加載什麼圖形之間就可以解耦。
本文示例使用的是數組,類似這樣:
pages=[[NSArray alloc] initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",
nil];
圖檔儲存在resources下。
為了能讓上頁下頁翻頁的時候找到關聯的頁面,采用了如下機制:
比如這樣,當應用加載的時候顯示第一頁:
- 将圖檔封裝為UIImageView顯示
- 可以為UIImageView設定一個tag值,值為數組下标+1
- 這樣,上級view有方法能根據tag查詢到UIImageView,比如:UIView *currentView=[self.view viewWithTag:currentTag];
- 設定一個成員變量currentTag儲存目前的tag值