您的位置:首页 > 其它

常用手势(敲击)(拖拽)(长按)(轻扫)(捏合)(选转)

2015-08-25 21:41 441 查看
/*****************************************************
*
* 一、使用手势时,一定要开启 !!!!!!!!!!!!!!!!
*   1 .userInteractionEnabled (用户交互活着)
*   2 .multipleTouchEnabled (多点触控)
*
* 二、几种常用的手势:
*   1.UITapGestureRecognizer  敲击(轻点)
*
*   2.UIPanGestureRecognizer  拖拽
*
*   3.UILongPressGestureRecognizer 长按
*
*   4.UISwipeGestureRecognizer 轻扫
*
*   5.UIPinchGestureRecognizer 捏合
*
*   6.UIRotationGestureRecognizer 旋转
*
* 三、基本使用方式
*   1.创建手势和添加监听方法
*
*   2.将添加的手势 添加到 (要使用的手势View)上。
*
*   3.在使用监听方法时,   操作完成后,手势属性一定要恢复到初始值。
*
*/
#import "ViewController.h"

@interface ViewController () <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView* imageView;

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/**
*  所有手势的几个属性,在监听方法中进行解释和说明
*/
//1.轻敲
UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
[self.imageView addGestureRecognizer:tapGestureRecognizer];

//2.拖拽
UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
[self.imageView addGestureRecognizer:panGesture];

//3.长按
UILongPressGestureRecognizer* longPresssGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
//作用的view
[self.imageView addGestureRecognizer:longPresssGesture];

//4.轻扫效果
UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipGesture:)];
[self.imageView addGestureRecognizer:swipe];

//5.捏合手势
UIPinchGestureRecognizer* pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureRecognizerAction:)];
//设置代理
pinchGesture.delegate = self;
[self.imageView addGestureRecognizer:pinchGesture];

//6.旋转
UIRotationGestureRecognizer* rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[self.imageView addGestureRecognizer:rotation];
}

//6.旋转手势触发方法
- (void)rotationAction:(UIRotationGestureRecognizer*)rotationGesture
{
// ===属性解释===
//1.rotation 旋转角度
//2.velocity 旋转速度-只读
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotationGesture.rotation);

//旋转角度恢复为0
rotationGesture.rotation = 0;

NSLog(@"内容 %f", rotationGesture.rotation);
NSLog(@"内容 %f", rotationGesture.velocity);
}

//5.捏合手势触发方法
- (void)pinchGestureRecognizerAction:(UIPinchGestureRecognizer*)pinchGesture
{
//属性解释
//scale 缩放比例
//    NSLog(@" %lf", pinchGesture.scale);

// 获取image的bounds,为了进行设置数值。最后再赋值回去。
CGRect imagebounds = self.imageView.bounds;
imagebounds.size = CGSizeMake(imagebounds.size.width * pinchGesture.scale, imagebounds.size.height * pinchGesture.scale);
self.imageView.bounds = imagebounds;

//恢复到捏合手势的初始状态。
pinchGesture.scale = 1;
NSLog(@"捏合手势触发方法 ");
}

//4.轻扫
- (void)swipGesture:(UISwipeGestureRecognizer*)swipe
{
//属性解释============================
//direction 轻扫方向

//    UISwipeGestureRecognizerDirectionRight = 1 << 0,//方向向右边触发
//    UISwipeGestureRecognizerDirectionLeft  = 1 << 1,//方向向左边触发
//    UISwipeGestureRecognizerDirectionUp    = 1 << 2,//方向向上触发
//    UISwipeGestureRecognizerDirectionDown  = 1 << 3//方向向下触发
swipe.direction = UISwipeGestureRecognizerDirectionDown;

NSLog(@"UISwipeGestureRecognizerDirectionRight ");
}

//3.长按手势触发方法
- (void)longPressGesture:(UILongPressGestureRecognizer*)longGesture
{
//    属性解释
//    minimumPressDuration  长按时间
//
//    allowableMovement  抖动距离
//长按时间
longGesture.minimumPressDuration = 2;
//抖动偏移量
longGesture.allowableMovement = 10;

NSLog(@"长按手势 ");
}

//2.拖拽手势触发方法
- (void)panGestureAction:(UIPanGestureRecognizer*)panGesture
{

//1.方式一:CGAffineTransformMakeTranslation 每次都是通过image的初试状态开始.(会出现闪的问题)
//    CGoint point = [panGesture translationInView:panGesture.view];
//    [panGesture setTranslation:CGPointZero inView:panGesture.view];
//    self.imageView.transform = CGAffineTransformMakeTranslation(point.x, point.y);

//2.方式二:在移动的位置上,进行接着拖拽.(特点是要加setTranslation:inView:方法)
CGPoint point = [panGesture translationInView:panGesture.view];
[panGesture setTranslation:CGPointZero inView:panGesture.view];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);

// 拖拽时,在View上的速率:- (CGPoint)velocityInView:(UIView *)view;
}

//1.轻巧手势触发方法
- (void)tapGesture:(UITapGestureRecognizer*)tapGesture
{

//属性解释
//numberOfTapsRequired  点击次数
//numberOfTouchesRequired 手指个数
tapGesture.numberOfTapsRequired = 2;
tapGesture.numberOfTouchesRequired = 2;

NSLog(@"轻敲");
}

#pragma mark - 代理
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer
{
return YES;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: