手势
2015-08-04 19:49
253 查看
// 1.点击
// 点击方法
// 2.长按
// 长按方法
// 3.旋转
// 旋转方法
// 4.捏合
// 捏合方法
// 5.拖拽
// 拖拽方法
// 6. 轻扫
// 轻扫手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)]; // 设置触发方法所需点击次数 tap.numberOfTapsRequired = 2; // 设置触发方法所需手指个数 tap.numberOfTouchesRequired = 2; // 将手势添加到对应的图片上 [self.imageView addGestureRecognizer:tap]; [tap release];
// 点击方法
- (void)click:(UITapGestureRecognizer *)tap { self.imageView.image = [UIImage imageNamed:@"12345.jpg"]; }
// 2.长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)]; // 设置长按触发所需最小时间 longPress.minimumPressDuration = 2; // 允许用户手指在长按过程中允许移动的距离 longPress.allowableMovement = 200; // 把手势添加到imageView中 [self.imageView addGestureRecognizer:longPress]; [longPress release];
// 长按方法
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress { // 长按弹出AlertView self.alertview = [[UIAlertView alloc] initWithTitle:@"测试" message:@"测试" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil]; [self.alertview show]; [self.alertview release]; }
// 3.旋转
// 创建旋转手势 UIRotationGestureRecognizer *revolve = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(revolveAction:)]; // 把手势放入对应的imageView中 [self.imageView addGestureRecognizer:revolve]; // 释放 [revolve release];
// 旋转方法
- (void)revolveAction:(UIRotationGestureRecognizer *)revolve { // 可通过手势获取手势添加的视图是哪个 UIImageView *imageView = (UIImageView *)revolve.view; // 进行旋转操作 // 通过视图transform属性,对视图进行旋转 imageView.transform = CGAffineTransformRotate(imageView.transform, revolve.rotation); revolve.rotation = 0; }
// 4.捏合
// 创建 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)]; // 将手势添加到图片上 [self.view addGestureRecognizer:pinch]; // 释放 [pinch release];
// 捏合方法
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch { UIImageView *imageView = (UIImageView *)pinch.view; imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale); // 防止图片消失 pinch.scale = 1; }
// 5.拖拽
// 创建 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; // 添加 [self.view addGestureRecognizer:pan]; // 释放 [pan release];
// 拖拽方法
- (void)panAction:(UIPanGestureRecognizer *)pan { UIImageView *imageView = (UIImageView *)pan.view; // 通过手势获得经过的点 CGPoint p = [pan translationInView:imageView]; // 设置移动的位置 imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y); // 为了防止手势在操作时视图消失 [pan setTranslation:CGPointZero inView:imageView]; }
// 6. 轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)]; [self.view addGestureRecognizer:swipe]; [swipe release]; // 轻扫方向 swipe.direction = UISwipeGestureRecognizerDirectionRight;
// 轻扫手势
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe { if (swipe.direction == UISwipeGestureRecognizerDirectionRight) NSLog(@"测试"); }
相关文章推荐
- 手势Gesture
- ios圆角提高性能
- 二分法查找问题
- HDU 1543:Avoid The Lakes 【深搜】
- ios学习笔记--(oc基础3)
- In App Purchases 入门
- css3常用基础
- 常见版本控制器使用
- hdu1285(确定比赛名次)初学拓扑
- FZU 1752 A^B mod C
- VNC Viewer连接成功后闪退的解决办法
- 如何进行表的复制
- C++封装互斥量和条件变量
- android_error:R cannot be resolved to a variable
- String、StringBuffer与StringBuilder之间区别
- 第三周遗忘总结
- OC简单小功能的实现-利用三个代表三原色的滑块控制背景颜色变化
- Lua打印table升级版
- “哎哟!蛮吊的” 之 Android BroadCastReceiver详解
- 音频播放,gif图的播放,毛玻璃效果 UISlider,UIStepper,UISegmentedControl