你的位置:首页 > 操作系统

[操作系统]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;}