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

來源:互聯網
上載者:User

手勢是指你用一個或多個手指接觸螢幕開始,直到你的手指全部離開螢幕為止所發生的所有事件。手勢辨識器(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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.