ios开发,6种手势的创建方法
来源:互联网 发布:国产 cpu.知乎 编辑:程序博客网 时间:2024/05/17 09:20
在 ios 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureRecognizer 的衍生类別来进行判断。用UIGestureRecognizer 的好处在于有现成的手势,开发者不用自己计算手指移动轨迹。UIGestureRecognizer的衍生类別有以下几种:
UITapGestureRecognizer // Tap(点一下)
UIPinchGestureRecognizer // Pinch(二指往內或往外拨动)
UIRotationGestureRecognizer // Rotation(旋转)
UISwipeGestureRecognizer // Swipe(滑动,快速移动)
UIPanGestureRecognizer // Pan (拖移,慢速移动)
UILongPressGestureRecognizer // LongPress(长按)
通过内置的手势识别器来处理一个手势的基本流程如下所示:
· 1.创建一个具有正确类型的对象,作为想要实现的手势识别器;
· 2.把该对象作为手势识别器添加到需要接收手势的视图中;
· 3.书写一个方法,该方法当手势事件发生时被调用,由其来执行应用所需的动作。
初始化
- (instancetype)initWithTarget:(id)targetaction:(SEL)action
添加一个点击手势
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
image.frame = CGRectMake(50, 50, 100, 160);
UITapGestureRecognizer *tap = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[image addGestureRecognizer:tag];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:image];
[image release];
[pinch release];
添加一个捏合手势
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
image.frame = CGRectMake(50, 50, 100, 160);
UIPinchGestureRecognizer *pinch = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[image addGestureRecognizer:pinch];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:pinch];
[pinch release];
触发事件:
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
// 通过tag值传值
UIView *view = [self.view viewWithTag:100];
// 设定放大倍数,让它长宽等比例放大
view.transform =CGAffineTransformScale(view.transform, pinch.scale, pinch.scale);
// 每次放大后还原放大倍数,
pinch.scale = 1;
}
添加一个旋转手势
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
image.frame = CGRectMake(50, 50, 100, 160);
UIRotationGestureRecognizer *rotation = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[image addGestureRecognizer:rotation];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:rotation];
[rotation release];
触发事件:
- (void)rotationAction:(UIRotationGestureRecognizer *)rotate
{
UIView *view = [self.view viewWithTag:100];
// 设定旋转角度
view.transform =CGAffineTransformRotate(view.transform, rotate.rotation);
// 每次旋转完之后旋转角度清零
rotate.rotation = 0;
}
添加一个滑动手势
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
image.frame = CGRectMake(50, 50, 100, 160);
UISwipeGestureRecognizer *swipe = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
[image addGestureRecognizer:swipe];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:swipe];
[swipe release];
添加一个拖拽手势
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
image.frame = CGRectMake(50, 50, 100, 160);
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[image addGestureRecognizer:pan];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:pan];
[pan release];
触发事件:
- (void)panAction:(UIPanGestureRecognizer *)pan
{
UIView *view = [self.view viewWithTag:100];
// 获取当前视图的偏移量
CGPoint point = [pan translationInView:view];
// 设置偏移量
view.transform=CGAffineTransformTranslate(view.transform, point.x, point.y);
// 偏移量清零
[pan setTranslation:CGPointMake(0, 0)inView:view];
}
添加一个长按手势
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
image.frame = CGRectMake(50, 50, 100, 160);
UILongPressGestureRecognizer *longPress = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
[image addGestureRecognizer:longPress];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:longPress];
[longPress release];
- ios开发,6种手势的创建方法
- IOS开发中手势滑动的6种技巧
- ios六大手势的创建使用
- ios开发创建单例的两种方法
- iOS开发(Swift):创建UINavigationView的三种方法
- 【iOS开发-多线程】多线程创建的几种方法对比
- iOS - 6种手势
- iOS中创建数组的简易方法与手势的使用
- 手势识别的 创建 与 方法
- IOS-开发中手势的处理
- iOS开发-手势返回失效的解决方案
- IOS开发之手势的学习
- IOS 开发中手势的基本用法
- 【iOS开发】 UISegmentedControl 几种手势应用
- iOS开发之常用六种手势
- IOS 开发 手势使用
- IOS 开发手势识别
- IOS开发之手势
- uva11020 Efficient Solutions mutiset
- C语言数组
- VC求一组数的最大值最小值总和以及平均值
- Myeclipse 使用最新Spring....或其他框架方法
- R语言学习(三)
- ios开发,6种手势的创建方法
- HHKB pro2 type s入手纪念~
- java Socket(阻塞) api记录点
- Unity3D工程MAC导入Windows报错
- C语言递归的应用之回文字符
- CentOS卸载并安装新版jdk(tar,rpm)
- Tomcat7配置数据源
- ASM 下 SPFILE 启动问题
- 2.17 数组循环移位