iOS手势以及手势冲突问题
来源:互联网 发布:数据交换平台架构 编辑:程序博客网 时间:2024/04/29 20:24
今天研究了一下iOS手势,手势包括点击,拖动,扫动,长按,捏合,旋转手势,下面上代码
#define WIDTH [UIScreen mainScreen].bounds.size.width#define HEIGHT [UIScreen mainScreen].bounds.size.height
@property (nonatomic,strong)UIImageView * image;@property (nonatomic,strong)UIAlertController * alertcontroller;@property (nonatomic,strong)UIPinchGestureRecognizer * pinch;@property (nonatomic,strong)UISwipeGestureRecognizer * swipright;@property (nonatomic,strong)UISwipeGestureRecognizer * swipleft;@property (nonatomic,strong)UILongPressGestureRecognizer * longpre;@property (nonatomic,strong)UIRotationGestureRecognizer * rote;@property (nonatomic,strong)UIPanGestureRecognizer * pan;
-(void)creatImage{ _image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]]; _image.frame = CGRectMake(0, 60, WIDTH, HEIGHT - 100); _image.userInteractionEnabled = YES; [self.view addSubview:_image]; //解决在图片上滑动时拖动手势和轻扫手势的冲突// [_pan requireGestureRecognizerToFail:_swipright];// [_pan requireGestureRecognizerToFail:_swipleft]; //解决拖动和长按手势之间的冲突 [_longpre requireGestureRecognizerToFail:_pan];}/* 1 UIGestureRecognizerStatePossible, // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态 3 UIGestureRecognizerStateBegan, // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成 4 UIGestureRecognizerStateChanged, // 手势状态发生转变 5 UIGestureRecognizerStateEnded, // 手势识别操作完成(此时已经松开手指) 6 UIGestureRecognizerStateCancelled, // 手势被取消,恢复到默认状态 7 UIGestureRecognizerStateFailed, // 手势识别失败,恢复到默认状态 8 UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded *///创建点击-(void)creatActionSheet{ _alertcontroller = [UIAlertController alertControllerWithTitle:@"标题" message:@"啦啦" preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction * action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { nil; }]; [_alertcontroller addAction:action];}-(void)setTapGestureRecognizer{ UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)]; tap.numberOfTapsRequired = 1;//点击次数 tap.cancelsTouchesInView = NO; tap.numberOfTouchesRequired = 1;//指头数 //3不行 [self.view addGestureRecognizer:tap];}-(void)setPanGestureRecognizer{ _pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panClick:)]; _pan.minimumNumberOfTouches = 0.5; //拖动的最短距离默认是0.5一般不要改 _pan.maximumNumberOfTouches = 2;//拖动的最长距离 _pan.delegate = self; [_image addGestureRecognizer:_pan];}-(void)setLongPressGestureRecognizer{ _longpre = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longClick:)]; _longpre.allowableMovement = 10; //在失败之前允许移动的 _longpre.minimumPressDuration = 1;//长按的时间 _longpre.delegate = self; [_image addGestureRecognizer:_longpre];}-(void)setPinchGestureRecognizer{ _pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchClick:)]; _pinch.delegate = self; [_image addGestureRecognizer:_pinch];}-(void)setRotationGestureRecognizer{ _rote = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotClick:)]; [_image addGestureRecognizer:_rote]; _rote.delegate = self;}-(void)setSwipeGestureRecognizer{ _swipright = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)]; _swipright.direction = UISwipeGestureRecognizerDirectionRight; [_image addGestureRecognizer:_swipright]; _swipleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)]; _swipleft.direction = UISwipeGestureRecognizerDirectionLeft; [_image addGestureRecognizer:_swipleft];}#pragma mark Action-(void)tapClick{ NSLog(@"点击"); [UIView animateWithDuration:0.2 animations:^{ _image.frame = CGRectMake(0, 0, 300, 300); }];}-(void)panClick:(UIPanGestureRecognizer*)pan{ //解决在图片上滑动时拖动手势和轻扫手势的冲突 if (pan.state==UIGestureRecognizerStateChanged) { /* 1.CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty)(平移:设置平移量) 2.CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)(缩放:设置缩放比例)仅通过设置缩放比例就可实现视图扑面而来和缩进频幕的效果。 3.CGAffineTransformMakeRotation(CGFloat angle)(旋转:设置旋转角度 */ CGPoint translation=[pan translationInView:self.view];//利用拖动手势的translationInView:方法取得在相对指定视图(这里是控制器根视图)的移动 if (translation.x > 0) { [pan requireGestureRecognizerToFail:_swipright]; }else{ [pan requireGestureRecognizerToFail:_swipleft]; } _image.transform = CGAffineTransformMakeTranslation(translation.x, translation.y); NSLog(@"%lf",[pan velocityInView:self.view].x);//移动的速率 }else if(pan.state == UIGestureRecognizerStateEnded){ [UIView animateWithDuration:0.5 animations:^{ _image.transform = CGAffineTransformIdentity; }]; }}-(void)longClick:(UILongPressGestureRecognizer*)longpress{ //解决拖动和长按手势之间的冲突,这里必须是手势包含关系,比如拖动与骚动 长按与拖动// [_longpre requireGestureRecognizerToFail:_pan]; if (longpress.state == UIGestureRecognizerStateBegan) { [self presentViewController:_alertcontroller animated:YES completion:^{ }]; }}-(void)pinchClick:(UIPinchGestureRecognizer*)pinch{ CGFloat scale = pinch.scale;//获取当前的缩放比例 pinch.view.transform = CGAffineTransformScale(pinch.view.transform, scale, scale); //在已缩放大小基础下进行累加变化;区别于:使用 CGAffineTransformMakeScale 方法就是在原大小基础下进行变化 pinch.scale = 1.0;}-(void)rotClick:(UIRotationGestureRecognizer*)rot{ if (rot.state == UIGestureRecognizerStateChanged) { _image.transform = CGAffineTransformMakeRotation(rot.rotation); }else if (rot.state == UIGestureRecognizerStateEnded){ [UIView animateWithDuration:0.8 animations:^{ _image.transform = CGAffineTransformIdentity; }]; }// _image.transform = CGAffineTransformRotate(rot.view.transform, rot.rotation);// rot.rotation = 0.0;}-(void)swip:(UISwipeGestureRecognizer*)gesture{ if (gesture == _swipright) { [self nextImage]; }else if (gesture == _swipleft){ [self lastImage]; }}-(void)itemClick{ SecondViewController * second = [[SecondViewController alloc] init]; [self.navigationController pushViewController:second animated:YES];}-(void)nextImage{ _image.image = [UIImage imageNamed:@"2"];}-(void)lastImage{ _image.image = [UIImage imageNamed:@"3"];}
0 0
- iOS手势以及手势冲突问题
- iOS手势冲突问题
- iOS 手势冲突的问题
- iOS 单击手势和双击手势冲突问题
- iOS中解决各种手势冲突问题
- ios解决手势冲突
- IOS手势冲突
- iOS,scrollview手势冲突
- iOS手势冲突
- 解决手势冲突问题
- iOS 导航控制器边缘滑动 全屏滑动 以及手势冲突问题
- iOS手势冲突相关解决
- iOS 手势冲突问题汇总(及解决办法)
- iOS 解决tableView和点击手势冲突问题
- iOS添加单击手势与tableview点击的冲突问题
- iOS端如何解决自定义滑动退出手势与TableViewCell删除手势之间的冲突问题
- iOS 手势重叠 iOS系统手势与自定义手势重叠, 手势冲突
- iOS开发,UISlider滑块不灵敏问题以及在UIScrollView上添加Slider造成手势冲突问题心得
- android布局 Android fill_parent wrap_content 和match_parent的区别
- Android源码分析-全面理解Context
- unbutu+mysql的root密码重置方法
- 内存对齐原则
- Android LRU算法 图片缓存性能改善 <9>
- iOS手势以及手势冲突问题
- 64. Minimum Path Sum
- 创建你自己的 Java 注解类
- NGUI:浅谈NGUI中UISprite和UITexture
- 每天进步一点点——五分钟理解一致性哈希算法(consistent hashing)
- Webrtc Intro - Kalman Filter of Remb
- Android 中的 packages 间共享数据的一种方法
- iOS真机运行,出现process launch failed: Security 错误
- ffmpeg x264 x265 控制台命令显示重定向到log文件