设为首页收藏本站

IT技术擎 - 最棒的IT web技术交流社区

 找回密码
 注册为IT技术擎人

QQ登录

只需一步,快速开始

搜索
热搜: php h5 jquery
查看: 29|回复: 0

[未分类] iOS 常用手势

[复制链接]

8100

主题

8100

帖子

2万

积分

版主

Rank: 7Rank: 7Rank: 7

积分
26192
发表于 2018-5-16 23:35:37 | 显示全部楼层 |阅读模式
UIGestureRecognizer 对iOS的各种手势进行了封装,完全满足了用户对手势的需求。
以下是对各种手势的详细应用和说明,希望能对大家有帮助。^_^
  1. - (void)viewDidLoad { [super viewDidLoad];//Do any additional setup after loading the view. self.view.backgroundColor =[UIColor groupTableViewBackgroundColor]; _imageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-150)/2, (self.view.frame.size.height-150)/2, 150, 150)]; _imageView.userInteractionEnabled = YES;//交互使能,允许界面交互 _imageView.image = [UIImage imageNamed:@"cat.png"]; [self.view addSubview:_imageView];//单击的 TapRecognizer UITapGestureRecognizer *singleTap; singleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)]; singleTap.numberOfTapsRequired = 1; //点击的次数 =1 单击 [_imageView addGestureRecognizer:singleTap];
  2. //给对象添加一个手势监测; //双击的 TapRecognizer UITapGestureRecognizer *doubleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DoubleTap:)]; doubleTap.numberOfTapsRequired = 2; //点击的次数 =2 双击 [_imageView addGestureRecognizer:doubleTap];//给对象添加一个手势监测; /* 1.双击手势确定监测失败才会触发单击手势的相应操作,否则双击时第一击时会响应单击事件 2.会造成单击时要判断是否是双击,调用单击会有所延时。属正常现象。 */
  3. [singleTap requireGestureRecognizerToFail:doubleTap];//捏合缩放手势 Pinch UIPinchGestureRecognizer *pinch; pinch =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];// [_imageView addGestureRecognizer:pinch];
  4. //添加到_imageView的时候,是要把手指放到_imageView操作 [self.view addGestureRecognizer:pinch];//是self的时候,操作整个view都可以捏合_imageView(在响应事件中操作) pinch.delegate=self;//旋转手势 Rotation UIRotationGestureRecognizer *rotateRecognizer =[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)]; [self.view addGestureRecognizer:rotateRecognizer];
  5. //是self的时候,操作整个view都可以捏合_imageView(在响应事件中操作) rotateRecognizer.delegate=self;//滑动手势 SwipeRecognizer UISwipeGestureRecognizer *swipeRecognizer =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; [self.view addGestureRecognizer:swipeRecognizer];
  6. //是self的时候,操作整个view都可以捏合_imageView(在响应事件中操作) swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;//操作为左滑 swipeRecognizer.delegate=self;
  7. //拖动手势 PanRecognizer UIPanGestureRecognizer *panRecognizer =[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; [_imageView addGestureRecognizer:panRecognizer];
  8. //关键语句,添加一个手势监测; panRecognizer.maximumNumberOfTouches = 1; panRecognizer.delegate=self;//长按手势 LongPressRecognizer UILongPressGestureRecognizer *longPressRecognizer =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongPress:)]; [_imageView addGestureRecognizer:longPressRecognizer]; longPressRecognizer.minimumPressDuration = 1.0f;//触发长按事件时间为:1.0秒 longPressRecognizer.delegate=self;
  9. }
  10. -(void)SingleTap:(UITapGestureRecognizer*)recognizer
  11. {//处理单击操作 NSLog(@"单击操作");
  12. }
  13. -(void)DoubleTap:(UITapGestureRecognizer*)recognizer
  14. {//处理双击操作 NSLog(@"双击操作");
  15. }
  16. - (void)handlePinch:(UIPinchGestureRecognizer*)recognizer
  17. { NSLog(@"缩放操作");//处理缩放操作 //对imageview缩放 _imageView.transform =CGAffineTransformScale(_imageView.transform, recognizer.scale, recognizer.scale);//对self.view缩放,因为recognizer是添加在self.view上的 //recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale); recognizer.scale = 1;
  18. }
  19. - (void)handleRotate:(UIRotationGestureRecognizer*) recognizer
  20. { NSLog(@"旋转操作");//处理旋转操作 //对imageview旋转 _imageView.transform =CGAffineTransformRotate(_imageView.transform, recognizer.rotation);//对self.view旋转,因为recognizer是添加在self.view上的 // recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation); recognizer.rotation = 0;
  21. }
  22. - (void)handleSwipe:(UISwipeGestureRecognizer*) recognizer
  23. {//处理滑动操作 if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) { NSLog(@"左滑滑动操作"); }
  24. elseif(recognizer.direction==UISwipeGestureRecognizerDirectionRight){ NSLog(@"右滑滑动操作"); }
  25. }
  26. -(void)handlePan:(UIPanGestureRecognizer*)recognizer
  27. { NSLog(@"拖动操作");//处理拖动操作,拖动是基于imageview,如果经过旋转,拖动方向也是相对imageview上下左右移动,而不是屏幕对上下左右 CGPoint translation =[recognizer translationInView:_imageView]; recognizer.view.center = CGPointMake(recognizer.view.center.x +translation.x, recognizer.view.center.y +translation.y); [recognizer setTranslation:CGPointZero inView:_imageView];
  28. }
  29. -(void)handlelongPress:(UILongPressGestureRecognizer*)recognizer
  30. {//处理长按操作,开始结束都会调用,所以长按1次会执行2次 if(recognizer.state ==UIGestureRecognizerStateBegan){ NSLog(@"开始长按操作"); }
  31. elseif(recognizer.state ==UIGestureRecognizerStateEnded){ NSLog(@"结束长按操作"); }
  32. }
  33. - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];//Dispose of any resources that can be recreated.
  34. }
复制代码



上一篇:IE兼容性小结(IE7及以上版本)
下一篇:android + php 后台开发
该用户未在地球留下任何的痕迹

本版积分规则

QQ|小黑屋|帮助|IT技术擎 ( 沪ICP备15054863号  

GMT+8, 2018-10-18 11:51

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表