IOS中的七種手勢小結_IOS

來源:互聯網
上載者:User

今天為大家介紹一下IOS 的七種手勢,手勢在開發中經常用到,所以就簡單 通俗易懂的說下, 話不多說,直接看代碼:

// 初始化一個UIimageViewUIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 300, 300)];imageView.image = [UIImage imageNamed:@"12.jpg"];// UIImageView的使用者互動是預設關閉的,要想使他可以處理觸摸事件,我們得手動開啟它[imageView setUserInteractionEnabled:YES];[self.window addSubview:imageView];//初始化一個視圖(響應者)來承載手勢/*UIView *gestureView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];//當前視圖放置到螢幕中央gestureView.center = self.window.center;gestureView.backgroundColor = [UIColor yellowColor];[self.window addSubview:gestureView];

1、輕拍手勢

//建立輕拍手勢UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

1.設定觸控對象,和輕拍的次數

//設定觸控對象的個數(幾個手指)[tapGR setNumberOfTouchesRequired:1];//設定輕拍次數[tapGR setNumberOfTapsRequired:2];//給建立好的視圖添加手勢[gestureView addGestureRecognizer:tapGR];//輕拍手勢的回調方法- (void)tapAction:(UITapGestureRecognizer*)sender{//可以根據手勢得到它當前所作用的視圖UIImageView *imageView = (UIImageView*)sender.view;//得到textfield viewWithTag此方法的傳回值為UIView類型,但是UITextField為UIView的子類,父類對象不能直接指向子類對象,所以需要強制轉換UITextField *textField = (UITextField*)[self.window viewWithTag:1000];//回收鍵盤,取消第一響應者[textField resignFirstResponder];NSLog(@"我輕拍了gestureView");}

2、捏合手勢

//建立捏合手勢UIPinchGestureRecognizer* pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];pinchGR.delegate = self; // 可以在同一個視圖上實現多個手勢//捏合手勢的回調方法- (void)pinchAction:(UIPinchGestureRecognizer*)sender{//通過捏合手勢的到縮放比率float scale = sender.scale;//得到該手勢所作用的視圖UIView *view = sender.view;//2D仿射變換函數中的縮放函數來實現視圖的放大縮小//是在原有基礎上來改變當前的視圖//函數的第一個參數:現有的視圖的transform值//第二個參數:x軸上的縮放比率//第三個參數:y軸上的縮放比率//是在視圖最初的transform狀態上改變,不管執行多少次,都是以該視圖最初的transform狀態為基礎來改變view.transform = CGAffineTransformMakeScale(2, 2);view.transform = CGAffineTransformScale(view.transform, scale, scale);//每次捏合動作完畢之後,讓此捏合值複原,使得它每次都是從100%開始縮放sender.scale = 1;}

3、旋轉手勢

//旋轉手勢UIRotationGestureRecognizer* rotaGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotaAction:)];rotaGR.delegate = self;//旋轉手勢回調方法- (void)rotaAction:(UIRotationGestureRecognizer*)sender{//通過手勢的到旋轉角度float rota = sender.rotation;//得到該手勢作用的視圖UIView *view = sender.view;//通過2D仿射變換函數中的旋轉函數來使得當前視圖旋轉。view.transform = CGAffineTransformRotate(view.transform, rota);//複原sender.rotation = 0;}

4、平移手勢

//平移手勢UIPanGestureRecognizer *panGP = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];//平移手勢的回調方法- (void)panAction:(UIPanGestureRecognizer*)sender{//得到當前手勢所在視圖UIView *view = sender.view;//得到我們在視圖上移動的位移量CGPoint currentPoint = [sender translationInView:view.superview];//通過2D仿射變換函數中與位移有關的函數實現視圖位置變化view.transform = CGAffineTransformTranslate(view.transform, currentPoint.x, currentPoint.y);//複原 // 每次都是從00點開始[sender setTranslation:CGPointZero inView:view.superview];}

5、邊緣撥動手勢

//邊緣撥動手勢UIScreenEdgePanGestureRecognizer *edgePanGR = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanAction:)];edgePanGR.edges = UIRectEdgeAll;//邊緣撥動手勢回調方法- (void)edgePanAction:(UIScreenEdgePanGestureRecognizer*)sender{NSLog(@"我成功的觸發了螢幕側邊手勢");}

6、長按手勢

// ⑥長按手勢UILongPressGestureRecognizer *longPressPR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];longPressPR.minimumPressDuration = 1;// ⑥長按手勢的回調方法- (void)longPressAction:(UILongPressGestureRecognizer *)sender{if (sender.state == UIGestureRecognizerStateEnded) {UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"看你麻痹" message:@"不服你咬死我" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];[alertView show];}}

7、撥動手勢

// ⑦撥動手勢UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];// ⑦撥動手勢的回調方法- (void)swipeAction:(UISwipeGestureRecognizer *)sender{if (sender.state == UIGestureRecognizerStateEnded) {UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"槽尼瑪" delegate:self cancelButtonTitle:@"紙張" destructiveButtonTitle:@"哈哈哈" otherButtonTitles:@"切毛毛", nil];[actionSheet showInView:self.window];}}

給imageView視圖添加手勢

// 3.給圖片添加手勢 一個視圖可以添加多種手勢,但是一個手勢,只能添加到一個視圖上[imageView addGestureRecognizer:tapGR];[imageView addGestureRecognizer:pinchGR];[imageView addGestureRecognizer:rotaGR];[imageView addGestureRecognizer:panGR];[imageView addGestureRecognizer:edgePanGR];[imageView addGestureRecognizer:longPressPR];[imageView addGestureRecognizer:swipeGR];

當一個視圖上想要添加多種手勢的時候就要用到手勢的代理(重點)

pragma mark ----手勢的代理方法// 使得多個手勢可以同時響應- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{// 傳回值為YES的時候,當執行一個手勢的操作的時候,也可以執行其他手勢的操作return YES;}

以上所述是小編給大家介紹的IOS中的七種手勢小結,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.