IOS中的七種手勢小結(jié)
今天為大家介紹一下IOS 的七種手勢,手勢在開發(fā)中經(jīng)常用到,所以就簡單 通俗易懂的說下, 話不多說,直接看代碼:
// 初始化一個UIimageView UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 300, 300)]; imageView.image = [UIImage imageNamed:@"12.jpg"]; // UIImageView的用戶交互是默認(rèn)關(guān)閉的,要想使他可以處理觸摸事件,我們得手動打開它 [imageView setUserInteractionEnabled:YES]; [self.window addSubview:imageView]; //初始化一個視圖(響應(yīng)者)來承載手勢 /*UIView *gestureView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; //當(dāng)前視圖放置到屏幕中央 gestureView.center = self.window.center; gestureView.backgroundColor = [UIColor yellowColor]; [self.window addSubview:gestureView];
1、輕拍手勢
//創(chuàng)建輕拍手勢 UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
1.設(shè)置觸控對象,和輕拍的次數(shù)
//設(shè)置觸控對象的個數(shù)(幾個手指) [tapGR setNumberOfTouchesRequired:1]; //設(shè)置輕拍次數(shù) [tapGR setNumberOfTapsRequired:2]; //給創(chuàng)建好的視圖添加手勢 [gestureView addGestureRecognizer:tapGR]; //輕拍手勢的回調(diào)方法 - (void)tapAction:(UITapGestureRecognizer*)sender{ //可以根據(jù)手勢得到它當(dāng)前所作用的視圖 UIImageView *imageView = (UIImageView*)sender.view; //得到textfield viewWithTag此方法的返回值為UIView類型,但是UITextField為UIView的子類,父類對象不能直接指向子類對象,所以需要強制轉(zhuǎn)換 UITextField *textField = (UITextField*)[self.window viewWithTag:1000]; //回收鍵盤,取消第一響應(yīng)者 [textField resignFirstResponder]; NSLog(@"我輕拍了gestureView"); }
2、捏合手勢
//創(chuàng)建捏合手勢 UIPinchGestureRecognizer* pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)]; pinchGR.delegate = self; // 可以在同一個視圖上實現(xiàn)多個手勢 //捏合手勢的回調(diào)方法 - (void)pinchAction:(UIPinchGestureRecognizer*)sender{ //通過捏合手勢的到縮放比率 float scale = sender.scale; //得到該手勢所作用的視圖 UIView *view = sender.view; //2D仿射變換函數(shù)中的縮放函數(shù)來實現(xiàn)視圖的放大縮小 //是在原有基礎(chǔ)上來改變當(dāng)前的視圖 //函數(shù)的第一個參數(shù):現(xiàn)有的視圖的transform值 //第二個參數(shù):x軸上的縮放比率 //第三個參數(shù):y軸上的縮放比率 //是在視圖最初的transform狀態(tài)上改變,不管執(zhí)行多少次,都是以該視圖最初的transform狀態(tài)為基礎(chǔ)來改變 view.transform = CGAffineTransformMakeScale(2, 2); view.transform = CGAffineTransformScale(view.transform, scale, scale); //每次捏合動作完畢之后,讓此捏合值復(fù)原,使得它每次都是從100%開始縮放 sender.scale = 1; }
3、旋轉(zhuǎn)手勢
//旋轉(zhuǎn)手勢 UIRotationGestureRecognizer* rotaGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotaAction:)]; rotaGR.delegate = self; //旋轉(zhuǎn)手勢回調(diào)方法 - (void)rotaAction:(UIRotationGestureRecognizer*)sender{ //通過手勢的到旋轉(zhuǎn)角度 float rota = sender.rotation; //得到該手勢作用的視圖 UIView *view = sender.view; //通過2D仿射變換函數(shù)中的旋轉(zhuǎn)函數(shù)來使得當(dāng)前視圖旋轉(zhuǎn)。 view.transform = CGAffineTransformRotate(view.transform, rota); //復(fù)原 sender.rotation = 0; }
4、平移手勢
//平移手勢 UIPanGestureRecognizer *panGP = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; //平移手勢的回調(diào)方法 - (void)panAction:(UIPanGestureRecognizer*)sender{ //得到當(dāng)前手勢所在視圖 UIView *view = sender.view; //得到我們在視圖上移動的偏移量 CGPoint currentPoint = [sender translationInView:view.superview]; //通過2D仿射變換函數(shù)中與位移有關(guān)的函數(shù)實現(xiàn)視圖位置變化 view.transform = CGAffineTransformTranslate(view.transform, currentPoint.x, currentPoint.y); //復(fù)原 // 每次都是從00點開始 [sender setTranslation:CGPointZero inView:view.superview]; }
5、邊緣輕掃手勢
//邊緣輕掃手勢 UIScreenEdgePanGestureRecognizer *edgePanGR = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanAction:)]; edgePanGR.edges = UIRectEdgeAll; //邊緣輕掃手勢回調(diào)方法 - (void)edgePanAction:(UIScreenEdgePanGestureRecognizer*)sender{ NSLog(@"我成功的觸發(fā)了屏幕邊緣手勢"); }
6、長按手勢
// ⑥長按手勢 UILongPressGestureRecognizer *longPressPR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)]; longPressPR.minimumPressDuration = 1; // ⑥長按手勢的回調(diào)方法 - (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:)]; // ⑦輕掃手勢的回調(diào)方法 - (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];
當(dāng)一個視圖上想要添加多種手勢的時候就要用到手勢的代理(重點)
pragma mark ----手勢的代理方法 // 使得多個手勢可以同時響應(yīng) - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ // 返回值為YES的時候,當(dāng)執(zhí)行一個手勢的操作的時候,也可以執(zhí)行其他手勢的操作 return YES; }
以上所述是小編給大家介紹的IOS中的七種手勢小結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
IOS開發(fā)代碼分享之用nstimer實現(xiàn)倒計時功能
在制作IOS項目中,我們經(jīng)常要用到倒計時功能,今天就分享下使用nstimer實現(xiàn)的倒計時功能的代碼,希望對大家能有所幫助2014-09-09IOS 開發(fā)中發(fā)送e-mail的幾種方法總結(jié)
這篇文章主要介紹了IOS 開發(fā)中發(fā)送e-mail的幾種方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03iOS界面跳轉(zhuǎn)時導(dǎo)航欄和tabBar的隱藏與顯示功能
這篇文章主要介紹了iOS界面跳轉(zhuǎn)時導(dǎo)航欄和tabBar的隱藏與顯示功能,需要的朋友可以參考下2017-02-02