你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS學習筆記——手勢(Gesture)

iOS學習筆記——手勢(Gesture)

編輯:IOS開發綜合

手勢是指你用一個或多個手指接觸屏幕開始,直到你的手指全部離開屏幕為止所發生的所有事件。手勢識別器(UIGestureRecognizer)是一個對象,知道如何觀察用戶生成的事件流,並識別用戶何時以與預定義的手勢相匹配的方式進行拉觸摸和拖動。UIGestureRecognizer類封裝了查找手勢的工作。在模擬器中,按“option”鍵,可模擬兩個手指的手勢。

在.h文件裡創建一個視圖對象,所有的手勢都是在視圖上進行的:

@interface LinViewController : UIViewController
//創建視圖對象
@property (retain, nonatomic) UIView * mView;

@end
在.m文件裡,創建各個手勢的對象,編寫各個手勢的實現方法:

@implementation LinViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //---點擊手勢識別器---//
    //創建點擊手勢的對象
    UITapGestureRecognizer * pSingleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleGesture:)];
    //將手勢添加到當前視圖中
    [self.view addGestureRecognizer:pSingleTap];
    
    //創建點擊手勢的對象
    UITapGestureRecognizer * pDoubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleGesture:)];
    //設置點擊次數為2次,默認為1次,屬於單擊操作
    pDoubleTap.numberOfTapsRequired = 2;
    //將手勢添加到當前視圖中
    [self.view addGestureRecognizer:pDoubleTap];
    //雙擊的時候讓單擊放棄響應
    [pSingleTap requireGestureRecognizerToFail:pDoubleTap];
    //釋放創建的對象
    [pDoubleTap release];
    [pSingleTap release];
    
    //---滑動手勢識別器---//
    //創建滑動手勢的對象
    UISwipeGestureRecognizer * pSwip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipGesture:)];
    //設置滑動的方向,此處為向右,共有4個方向
    pSwip.direction = UISwipeGestureRecognizerDirectionRight;
    //將手勢添加到當前視圖中
    [self.view addGestureRecognizer:pSwip];
    //釋放創建的對象
    [pSwip release];
    
    //---拖動手勢識別器--//
    //創建拖動手勢對象
    UIPanGestureRecognizer * pPan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
    //將手勢添加到當前視圖中
    [self.view addGestureRecognizer:pPan];
    //釋放創建的對象
    [pPan release];
    
    //---長按手勢識別器--//
    //創建長按手勢的對象
    UILongPressGestureRecognizer * pLongPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)];
    //設置長按的時間,2秒以上判定為長按
    pLongPress.minimumPressDuration = 2;
    //將手勢添加到當前視圖中
    [self.view addGestureRecognizer:pLongPress];
    //釋放創建的對象
    [pLongPress release];
    
    //---旋轉手勢識別器---//
    //創建旋轉手勢對象
    UIRotationGestureRecognizer * pRotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];
    //將手勢添加到當前視圖中
    [self.view addGestureRecognizer:pRotation];
    //釋放創建的對象
    [pRotation release];
}
#pragma mark--------tapGesture>>>single  單擊
- (void)singleGesture:(UITapGestureRecognizer *)tap
{
    //單擊手勢對應的操作
    NSLog(@"單擊操作");
}
#pragma mark--------tapGesture>>>double  雙擊
- (void)doubleGesture:(UITapGestureRecognizer *)tap
{
    //雙擊手勢對應的操作
    NSLog(@"雙擊操作");
}
#pragma mark--------tapGesture>>>Swip    滑動
- (void)swipGesture:(UISwipeGestureRecognizer *)swip
{
    //滑動,劃屏手勢對應的操作
    NSLog(@"滑動操作,分方向,此處向右滑動");
}
#pragma mark--------tapGesture>>>Pan     平移
- (void)panGesture:(UIPanGestureRecognizer *)pan
{
    //獲取平移手勢在視圖上的坐標
    CGPoint point = [pan locationInView:self.view];
    //把視圖的中心點確定在平移手勢的坐標上,即把視圖拖動到平移點
    self.mView.center = point;
    NSLog(@"%@",NSStringFromCGPoint(point));
}
#pragma mark--------tapGesture>>>LongPress 長按
- (void)longPressGesture:(UILongPressGestureRecognizer *)longPress
{
    //長按手勢對應的操作
    NSLog(@"長按操作");
}
#pragma mark--------tapGesture>>>Rotation  旋轉
- (void)rotationGesture:(UIRotationGestureRecognizer *)rotation
{
    //確定手勢旋轉的大小,角度,使視圖做出相應的反應
    float degree = rotation.rotation *(180/M_PI);
    NSLog(@"旋轉的角度為%.2f",degree);
}
//釋放創建的對象
- (void)dealloc
{
    [_mView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved