iOS手势操作简介(六)
2016-02-01 16:32
375 查看
利用UIGestureRecognizer来对手势进行处理:
@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imagView;
@end
@implementation HMViewController
(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// pan
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[_imagView addGestureRecognizer:pan];
}
(void)pan:(UIPanGestureRecognizer *)pan
{
// 获取手指移动的位置
CGPoint trans = [pan translationInView:_imagView];
_imagView.transform = CGAffineTransformTranslate(_imagView.transform, trans.x, trans.y);
// 复位
[pan setTranslation:CGPointZero inView:_imagView];
NSLog(@”%@”,NSStringFromCGPoint(trans));
}
{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
// 设置代理的原因:想要同时支持多个手势
pinch.delegate = self;
[_imagView addGestureRecognizer:pinch];
}
(void)pinch:(UIPinchGestureRecognizer *)pinch
{
_imagView.transform = CGAffineTransformScale(_imagView.transform, pinch.scale, pinch.scale);
// 复位
pinch.scale = 1;
}
// Simultaneous:同时
// 默认是不支持多个手势
// 当你使用一个手势的时候就会调用
- (BOOL)gestureRecognizer:(UIGestureRecognizer )gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer )otherGestureRecognizer
{
return YES;
}
{
// rotation
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[_imagView addGestureRecognizer:rotation];
}
(void)rotation:(UIRotationGestureRecognizer *)rotation
{
NSLog(@”%f”,rotation.rotation);
// _imagView.transform = CGAffineTransformMakeRotation(rotation.rotation);
_imagView.transform = CGAffineTransformRotate(_imagView.transform, rotation.rotation);
// 复位
rotation.rotation = 0;
}
{
// Swipe
// 一个手势只能识别一个方向
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[_imagView addGestureRecognizer:swipe];
}
(void)swipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(@”swipe”);
}
{
// longPress
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[_imagView addGestureRecognizer:longPress];
}
(void)longPress:(UILongPressGestureRecognizer *)longPress
{
// 根据状态执行事件
if (longPress.state == UIGestureRecognizerStateBegan) {
}
}
{
// tap
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
// 点按多少次才能触发手势
// tap.numberOfTapsRequired = 2;
//
// // 必须多少个手指触摸才能触发手势
// tap.numberOfTouchesRequired = 2;
tap.delegate = self;
[_imagView addGestureRecognizer:tap];
}
// 这个触摸点能否触发手势
//- (BOOL)gestureRecognizer:(UIGestureRecognizer )gestureRecognizer shouldReceiveTouch:(UITouch )touch
//{
// CGPoint currentPoint = [touch locationInView:_imagView];
//
// if (currentPoint.x < _imagView.bounds.size.width * 0.5) {
// return NO;
// }else{
//
// return YES;
// }
//}
(void)tap:(UITapGestureRecognizer *)tap
{
NSLog(@”tap”);
}
@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imagView;
@end
@implementation HMViewController
(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// pan
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[_imagView addGestureRecognizer:pan];
}
(void)pan:(UIPanGestureRecognizer *)pan
{
// 获取手指移动的位置
CGPoint trans = [pan translationInView:_imagView];
_imagView.transform = CGAffineTransformTranslate(_imagView.transform, trans.x, trans.y);
// 复位
[pan setTranslation:CGPointZero inView:_imagView];
NSLog(@”%@”,NSStringFromCGPoint(trans));
}
warning pinch
(void)addPinch{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
// 设置代理的原因:想要同时支持多个手势
pinch.delegate = self;
[_imagView addGestureRecognizer:pinch];
}
(void)pinch:(UIPinchGestureRecognizer *)pinch
{
_imagView.transform = CGAffineTransformScale(_imagView.transform, pinch.scale, pinch.scale);
// 复位
pinch.scale = 1;
}
// Simultaneous:同时
// 默认是不支持多个手势
// 当你使用一个手势的时候就会调用
- (BOOL)gestureRecognizer:(UIGestureRecognizer )gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer )otherGestureRecognizer
{
return YES;
}
warning rotation
(void)addRotation{
// rotation
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[_imagView addGestureRecognizer:rotation];
}
(void)rotation:(UIRotationGestureRecognizer *)rotation
{
NSLog(@”%f”,rotation.rotation);
// _imagView.transform = CGAffineTransformMakeRotation(rotation.rotation);
_imagView.transform = CGAffineTransformRotate(_imagView.transform, rotation.rotation);
// 复位
rotation.rotation = 0;
}
warning Swipe
(void)addSwipe{
// Swipe
// 一个手势只能识别一个方向
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[_imagView addGestureRecognizer:swipe];
}
(void)swipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(@”swipe”);
}
warning longPress
(void)addLongPress{
// longPress
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[_imagView addGestureRecognizer:longPress];
}
(void)longPress:(UILongPressGestureRecognizer *)longPress
{
// 根据状态执行事件
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"longPress");
}
}
warning tap
(void)addTap{
// tap
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
// 点按多少次才能触发手势
// tap.numberOfTapsRequired = 2;
//
// // 必须多少个手指触摸才能触发手势
// tap.numberOfTouchesRequired = 2;
tap.delegate = self;
[_imagView addGestureRecognizer:tap];
}
// 这个触摸点能否触发手势
//- (BOOL)gestureRecognizer:(UIGestureRecognizer )gestureRecognizer shouldReceiveTouch:(UITouch )touch
//{
// CGPoint currentPoint = [touch locationInView:_imagView];
//
// if (currentPoint.x < _imagView.bounds.size.width * 0.5) {
// return NO;
// }else{
//
// return YES;
// }
//}
(void)tap:(UITapGestureRecognizer *)tap
{
NSLog(@”tap”);
}
相关文章推荐
- iOS开发-FMDB的基本使用
- iOS手势操作简介(五)
- iOS 真机 Could not launch "***"
- ios测试基础八:iOS设备如何能正常使用adhoc包
- iOS手势操作简介(四)
- ios程序更新的方式
- ios9中设置状态栏颜色与隐藏
- iOS Core Animation编程指南
- iOS/mac开发的一些知名个人博客
- IOS应用版本迭代注意事项
- iOS 动画 购物Demo
- iOS 新窗口在最上层
- iOS开发之解压缩zip文件
- 无需第三方软件,在 Yosemite 下给 iOS 设备轻松录屏
- 【分享】iOS功能界面漂亮的弹出框
- 使用xcode7(或是xcode6)遇到的一些问题
- iOS 全屏手势返回代码实现
- iOS开发di第三方介绍之一MBProgressHUD
- iOS横竖屏
- <iOS> 谈谈iOS Animation