手势识别

#import "YZViewController.h"

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

@end

@implementation YZViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 移动图片
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [_imageView addGestureRecognizer:pan];
    
}

- (void)pan:(UIPanGestureRecognizer *)pan
{
    
    CGPoint trans = [pan translationInView:_imageView];
    
    _imageView.transform = CGAffineTransformTranslate(_imageView.transform, trans.x, trans.y);
    
    // 复位
    [pan setTranslation:CGPointZero inView:_imageView];
}

// Simultaneous:同时
// 默认是不支持多个
// 当你使用一个手势的时候就会调用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

- (void)addPinch
{
    // 捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    pinch.delegate = self;
    
    [_imageView addGestureRecognizer:pinch];
}

- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
    
    _imageView.transform = CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);
    // 复位
    pinch.scale = 1;
}

- (void)addRotation
{
    // 旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    rotation.delegate = self;
    [_imageView addGestureRecognizer:rotation];

}


- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
    
//    _imageView.transform = CGAffineTransformMakeRotation(rotation.rotation);
    NSLog(@"%f",rotation.rotation);
    
    _imageView.transform = CGAffineTransformRotate(_imageView.transform, rotation.rotation);
    
    // 复位
    rotation.rotation = 0;
}

// 清扫
- (void)addSwipe
{
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [_imageView addGestureRecognizer:swipe];
}


- (void)swipe:(UISwipeGestureRecognizer *)swipe
{
    NSLog(@"");
}

- (void)addLong
{
    // 长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    
    longPress.minimumPressDuration = 2;
    // 手势识别之前的最大移动范围,超出这个范围就不能识别
    longPress.allowableMovement = 10;
    
    [_imageView addGestureRecognizer:longPress];

}

- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        
        NSLog(@"longPress");
    }
}

- (void)addTap
{
    // 1.创建点按手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self action:@selector(tap:)];
    //    tap.numberOfTapsRequired = 2;
    //    tap.numberOfTouchesRequired = 2;
    tap.delegate = self;
    [_imageView addGestureRecognizer:tap];

}

// 返回NO,表示不触发手势事件
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
//{
//    CGPoint pos = [touch locationInView:_imageView];
//
//    if (pos.x < _imageView.bounds.size.width * 0.5 ) {
//        
//        return NO;
//    }
//    return YES;
//}


- (void)tap:(UITapGestureRecognizer *)tap
{
    NSLog(@"tap");
}

@end

 

posted @ 2015-11-27 12:49  Z了个L  阅读(196)  评论(0编辑  收藏  举报