你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS中的七種手勢小結

IOS中的七種手勢小結

編輯:IOS開發綜合

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

// 初始化一個UIimageView
UIImageView *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中的七種手勢小結,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!

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