iOS 手勢的學習

1、.h文件中的代碼如下

//
//  LMViewController.h
//  DemoGesture
//
//  Created by 路 apple on 13-9-12.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LMViewController : UIViewController {
    UILabel *myRotationLabel;
    UIButton *myButton;
}

@property (strong, nonatomic) IBOutlet UILabel *myRotationLabel;
@property (strong, nonatomic) IBOutlet UIButton *myButton;

//ios5 支持以下手勢
//滑動手勢
@property (nonatomic,strong) UISwipeGestureRecognizer * swipeGestureRecognizer;

//旋轉手勢
@property (nonatomic,strong) UIRotationGestureRecognizer * rotationGestureRecognizer;
@property (nonatomic,unsafe_unretained) CGFloat rotationAngleInRadians;

//pich 縮放手勢
@property (nonatomic,strong) UIPinchGestureRecognizer * pichGestureRecognizer;

//pan 拖拽手勢
@property (nonatomic,strong) UIPanGestureRecognizer * panGestureRecognizer;

//Long press長按手勢
@property (nonatomic,strong) UILongPressGestureRecognizer * longpressGestureRecognizer;

//Tap點擊手勢
@property (nonatomic,strong) UITapGestureRecognizer * tapGestureRecognizer;


@property (nonatomic,assign) float currentsCale;
@end


2、.m中的代碼如下:

//
//  LMViewController.m
//  DemoGesture
//
//  Created by 路 apple on 13-9-12.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "LMViewController.h"

@implementation LMViewController
@synthesize myRotationLabel;
@synthesize myButton;

@synthesize swipeGestureRecognizer;
@synthesize rotationGestureRecognizer,rotationAngleInRadians;
@synthesize pichGestureRecognizer;
@synthesize panGestureRecognizer;
@synthesize longpressGestureRecognizer;
@synthesize tapGestureRecognizer;
@synthesize currentsCale;


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    //1、滑動手勢初始化部分
    self.swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipes:)];
    //滑動方向
    self.swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;//向左滑動
//    self.swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;//向右滑動
//    self.swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown;//向下滑動
//    self.swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;//向上滑動
    //如果以上四句全部寫出來的話,就只能識別向上滑動的手勢
    //滑動手指數
    self.swipeGestureRecognizer.numberOfTouchesRequired = 1;
    //要模擬兩個手指一起滑動的話:
    //option+左鍵          兩個手指方向相反
    //option+shift+左鍵    兩個手指方向相同
    
    //將手勢識別器添加到當前的view上面
    [self.view addGestureRecognizer:self.swipeGestureRecognizer];
    
    
    //2、旋轉手勢初始化部分
    self.rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotations:)];
    //將手勢識別器添加到當前的view上面
    [self.view addGestureRecognizer:self.rotationGestureRecognizer];
    
    
    //3、pich手勢初始化部分
    self.pichGestureRecognizer = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePiches:)];
    [self.myRotationLabel addGestureRecognizer:pichGestureRecognizer];
    [self.view addGestureRecognizer:pichGestureRecognizer];
    
    
    //4、pan 拖拽手勢。注意:如果既有swipe手勢又有pan手勢的話,不會捕獲swipe手勢了。
    self.panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePans:)];
    self.panGestureRecognizer.minimumNumberOfTouches = 1;
    self.panGestureRecognizer.maximumNumberOfTouches = 1;
    [self.view addGestureRecognizer:self.panGestureRecognizer];
    myRotationLabel.userInteractionEnabled = YES;
    myRotationLabel.exclusiveTouch = YES;
    [myRotationLabel addGestureRecognizer:panGestureRecognizer];
    
    //5、long press手勢
    self.longpressGestureRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongpress:)];
    //手指數
    self.longpressGestureRecognizer.numberOfTouchesRequired = 2;
    self.longpressGestureRecognizer.allowableMovement = 100;
    self.longpressGestureRecognizer.minimumPressDuration = 1.0;
    
//    [self.myButton addGestureRecognizer:longpressGestureRecognizer];
    [self.view addGestureRecognizer:longpressGestureRecognizer];
    
    
    //6、tap手勢
    self.tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
    self.tapGestureRecognizer.numberOfTapsRequired = 2;//監聽雙擊手勢
    [self.view addGestureRecognizer:tapGestureRecognizer];
}

-(void)handleSwipes:(UISwipeGestureRecognizer *) recognizer
{
    
    if(self.swipeGestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft)
    {
        NSLog(@"往左滑動");
    }
    else if(self.swipeGestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)
    {
        NSLog(@"往右滑動");
    }
    else if(self.swipeGestureRecognizer.direction==UISwipeGestureRecognizerDirectionDown)
    {
        NSLog(@"向下滑動");
    }
    else if(self.swipeGestureRecognizer.direction==UISwipeGestureRecognizerDirectionUp)
    {
        NSLog(@"向上滑動");
    }
}

-(void)handleRotations:(UIRotationGestureRecognizer*)recognizer
{
    NSLog(@"旋轉");
    if(self.myRotationLabel==nil)
    {
        return;
    }
    //根據當前旋轉的角度來旋轉label
    self.myRotationLabel.transform = CGAffineTransformMakeRotation(self.rotationAngleInRadians + recognizer.rotation);
    if(recognizer.state==UIGestureRecognizerStateEnded)
    {
        //每次旋轉結束保存旋轉角度
        self.rotationAngleInRadians += recognizer.rotation;
    }
    /*
     這個旋轉手勢識別器將會給我們傳遞一組旋轉的角度,旋轉手勢識別器一直還在進行一個監聽的動作,因 此他會一邊監聽我們手勢旋轉的角度,一邊把這些捕獲到的角度傳遞給我們,然後我們可以利用這些角度信 息,進行一些位置的計算,然後調整下一個顯示的位置。
     
     在 iOS SDK 中所有以"CG"開頭的都需要引入,CoreGraphice 框架包
     
     
     */
}

-(void)handlePiches:(UIPinchGestureRecognizer*)recognizer
{
    NSLog(@"pich");
    if(recognizer.state==UIGestureRecognizerStateEnded)
    {
        //聚拉結束後保存聚拉的比例
        self.currentsCale = recognizer.scale;
    }
    else if(recognizer.state==UIGestureRecognizerStateBegan&&self.currentsCale!=0.0f)
    {
        //再次開始聚拉的時候按照上一次保存的聚拉比例來聚拉,就相當於等比的縮小或者放大了
        recognizer.scale = self.currentsCale;
    }
    if(recognizer.scale!=NAN&&recognizer.scale!=0.0f)
    {
        //根據保存的聚拉比例聚拉
        recognizer.view.transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);
    }
    
    /*
     一旦聚拉的手勢動作產生了之後,我們就需要在捕獲的事件中進行一個頁面調整。其中有兩個比較重要的 變量 scale 和 velocity,前者是一個比例範圍,後者是一個變化速率的,也就是說每次變化的一個像素點。
     */
}

-(void)handlePans:(UIPanGestureRecognizer *)recognizer
{
    NSLog(@"pan");
    if(recognizer.state!=UIGestureRecognizerStateEnded&&recognizer.state!=UIGestureRecognizerStateFailed)
    {
        CGPoint location = [recognizer locationInView:recognizer.view.superview];
        recognizer.view.center = location;
//        注意要將拖拽手勢識別器添加到label上才能移動label
    }
    /*
     通過使用 locationInView 這個方法,來獲取到我們手勢的座標,爲了能夠捕獲到多個手指的位置,我們就 需 要 使 用    locationOfTouch:inView: 這 個 方 法 。 然 後 通 過 使 用    UIPanGestureRecognizer    這 個 對 象 的 minimumNumberOfTouches 和 maximumNumberOfTouches 參數。你就可以在同一個時間來捕獲多個手指的拖拽 的動作了。
     
     當手勢產生的時候,並且處於 UIGestureRecognizerStateEnded 這個狀態的時候,反饋出來的座標 X 和 Y 肯能並 不是數字類型的,可能是 NAN 空。因此在這個狀態的時候,我們一邊建議不要採用這個狀態反饋的座標值。
     */
}

-(void)handleLongpress:(UILongPressGestureRecognizer *)recognizer
{
    NSLog(@"long press");
    if(recognizer. numberOfTouchesRequired   ==2)
    {
        //第一個手指的位置
        CGPoint point1 = [recognizer locationOfTouch:0 inView:recognizer.view];
        //第二個手指的位置
        CGPoint point2 = [recognizer locationOfTouch:1 inView:recognizer.view];
        //取兩個手指的中點位置
        CGFloat midPointX = (point1.x + point2.x)/2.0;
        CGFloat midPointy = (point1.y + point2.y)/2.0;
        CGPoint midPoint = CGPointMake(midPointX,midPointy);
        myButton.center = midPoint;
    }
}

-(void)handleTapGesture:(UITapGestureRecognizer *)recognizer
{
    NSLog(@"雙擊了");
}

- (void)viewDidUnload
{
    [self setMyRotationLabel:nil];
    [self setMyButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.swipeGestureRecognizer = nil;
    self.rotationGestureRecognizer = nil;
    self.pichGestureRecognizer = nil;
    self.panGestureRecognizer = nil;
    self.longpressGestureRecognizer = nil;
    self.tapGestureRecognizer = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
/*
 
 單獨的手勢識別器可以傳遞如下狀態值。
 1. UIGestureRecognizerStatePossible 
 2. UIGestureRecognizerStateRecognized 
 3. UIGestureRecognizerStateFailed
 一組連貫的手勢組可以傳遞如下的狀態值。
 1. UIGestureRecognizerStatePossible 
 2. UIGestureRecognizerStateBegan 
 3. UIGestureRecognizerStateChanged 
 4. UIGestureRecognizerStateEnded 
 5. UIGestureRecognizerStateFailed
 
 
 rotationAngleInRadians
 這個對象就是我們在旋轉的時候需要爲我們的標籤對象設置的一個位置對象信息,當我們每一次旋轉的時 候我們都會把一個新的位置值保存在這個對象裏面,達到一個旋轉的效果。
 其實你也不用考慮太多,雖然標籤的位置並不重要,我們還是讓標籤在他的周圍進行一個旋轉的,無論我 們的視圖和標籤在什麼地方,唯一值得我們注意的就是,在不同的設備,由於屏幕尺寸(ipad,iphone)不一 樣,所以我們都是需要來自動進行計算,計算下一次旋轉的值達到旋轉的效果。
 通過使用標籤的 center 這個屬性,然後設置一箇中心位置值給我們的窗體界面,我們將會把我們的標籤居 中顯示,然後我們在旋轉這個標籤的時候也是依據這個中心位置進行一個旋轉。
 */
@end


界面如下:
ios手勢學習筆記 - 〇①世界 - 雲端的小牛牛
發佈了6 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章