欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

IOS - 七大手势操作

发布时间:2024/3/12 编程问答 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 IOS - 七大手势操作 小编觉得挺不错的,现在分享给大家,帮大家做个参考.


#import "ViewController.h"@interface ViewController ()@property (nonatomic,retain)UIImageView *imageView; @property (nonatomic,assign)NSInteger index;//下标 @property (nonatomic,retain)NSMutableArray *images;//图片名 字数组@end@implementation ViewController #加载视图-(void)viewDidLoad {[super viewDidLoad]; //布局imageView [self layoutImageView]; //1.创建轻拍手势 [self tapGestureRecognizer]; //2.创建清扫手势 [self swipeGestureRecognizer]; //3.创建长安手势 [self longPressGestureRecognizer]; //4.创建平移手势 [self panGestureTecognizer]; //5.创建捏合手势 [self pinchGestureRecognizer]; //6.创建 旋转手势 [self rotationGestureRecognizer] //7.创建边缘手势 [self screenEdgePanGestureRecognizer]; }

##布局ImageView-(void)layoutImageView {//创建对象 UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)]; //配置属性 imageView.backgroundColor =[UIColor purpleColor]; //设置图片 imageView.image =[UIImage imageNamed:@"car1.jpg"]; //添加父视图 [self.view addSubview:imageView]; //将创建的图片视图对象 给属性赋值 self.imageView =imageView; //打开用户交互 self.imageView.userInteractionEnabled =YES; self.images =[NSMutableArray array]; for (int i = 1; i<9; i++) {NSString * imageName =[NSString stringWithFormat:@"car%d.jpg",i];[_images addObject:imageName]; } // _index =1;}#pragma 轻怕手势 //创建轻拍手势 -(void)tapGestureRecognizer {//创建手势对象 UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]; //配置属性 //轻拍次数 tap.numberOfTapsRequired =1; //轻拍手指个数 tap.numberOfTouchesRequired =1; //讲手势添加到指定的视图上 [_imageView addGestureRecognizer:tap];}//轻拍事件-(void)tapAction:(UITapGestureRecognizer *)tap {//图片切换 NSLog(@"拍一下"); _index ++; if (_index == 8) {_index = 1; } self.imageView.image =[UIImage imageNamed:_images[_index]];} #pragma 清扫手势//清扫手势 -(void)swipeGestureRecognizer {UISwipeGestureRecognizer *swipe =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)]; //配置属性 //一个清扫手势 只能有两个方向(上和下) 或者 (左或右) //如果想支持上下左右清扫 那么一个手势不能实现 需要创建两个手势 swipe.direction =UISwipeGestureRecognizerDirectionLeft; //添加到指定视图 [_imageView addGestureRecognizer:swipe]; UISwipeGestureRecognizer *swipe2 =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)]; swipe2.direction =UISwipeGestureRecognizerDirectionRight; [_imageView addGestureRecognizer:swipe2];} //清扫事件 -(void)swipeAction:(UISwipeGestureRecognizer *)swipe {NSLog(@"扫一下"); if (swipe.direction ==UISwipeGestureRecognizerDirectionRight) {NSLog(@"右清扫");_index --;if (_index < 1) {_index =7;}_imageView.image =[UIImage imageNamed:_images[_index]]; }else if(swipe.direction == UISwipeGestureRecognizerDirectionLeft){NSLog(@"左扫一下");_index ++;if (_index == 8) {_index=1;}_imageView.image =[UIImage imageNamed:_images[_index]]; }} #pragma 长按手势 //创建长按手势 -(void)longPressGestureRecognizer {UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)]; //最短长按时间 longPress.minimumPressDuration =2; //允许移动最大距离 longPress.allowableMovement =1; //添加到指定视图 [_imageView addGestureRecognizer:longPress];} //长按事件 -(void)longPressAction:(UILongPressGestureRecognizer *)longPress {//NSLog(@"长按"); //对于长安有开始和 结束状态 if (longPress.state == UIGestureRecognizerStateBegan) {NSLog(@"长按开始");//将图片保存到相册UIImage *image =[UIImage imageNamed:_images[_index]];UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); }else if (longPress.state == UIGestureRecognizerStateEnded) {NSLog(@"长按结束"); }}#pragma 平移手势 //创建平移手势 -(void)panGestureTecognizer {UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)]; //添加到指定视图 [_imageView addGestureRecognizer:pan];} //创建平移事件 -(void)panAction:(UIPanGestureRecognizer *)pan {//获取手势的位置 CGPoint position =[pan translationInView:_imageView];//通过stransform 进行平移交换 _imageView.transform = CGAffineTransformTranslate(_imageView.transform, position.x, position.y); //将增量置为零 [pan setTranslation:CGPointZero inView:_imageView];}#pragma 捏合手势 -(void)pinchGestureRecognizer {UIPinchGestureRecognizer *pinch =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)]; //添加到指定视图 [_imageView addGestureRecognizer:pinch];} //添加捏合事件 -(void)pinchAction:(UIPinchGestureRecognizer *)pinch {//通过 transform(改变) 进行视图的视图的捏合 _imageView.transform =CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale); //设置比例 为 1 pinch.scale = 1;} #pragma 旋转手势//创建旋转手势 -(void)rotationGestureRecognizer {UIRotationGestureRecognizer *rotation =[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)]; //添加到指定的视图 [_imageView addGestureRecognizer:rotation];} //旋转事件-(void)rotationAction:(UIRotationGestureRecognizer *)rote {//通过transform 进行旋转变换 _imageView.transform = CGAffineTransformRotate(_imageView.transform, rote.rotation); //将旋转角度 置为 0 rote.rotation = 0;} #pragma 边缘手势//创建边缘手势 -(void)screenEdgePanGestureRecognizer {UIScreenEdgePanGestureRecognizer *screenPan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenPanAction:)]; [_imageView addGestureRecognizer:screenPan];} //创建边缘事件 -(void)screenPanAction:(UIScreenEdgePanGestureRecognizer *)screenPan {NSLog(@"边缘");}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }@end }



总结

以上是生活随笔为你收集整理的IOS - 七大手势操作的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。