【UI基础】手势识别

//

//  ViewController.m

//  09-手势识别器

//

//  Created by styshy on 15/11/2.

//  Copyright (c) 2015 sz. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()<UIGestureRecognizerDelegate>


@property (weak, nonatomic) IBOutletUIImageView *imgView;

@end


@implementation ViewController


// 手势调节

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    // 点按收拾

//    [self addTap];

    

    // 长按收拾

//    [self longPress];

    

    // 清扫

//    [self addSwipe];

    

    // 旋转

//    [self addRotation];

    

    // 捏合收拾

//    [self addPinch];

    

    // 拖拽

    [self addPan];


}



#pragma mark - / ******** 拖拽方式实现 **********/


- (void)addPan{

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(pan:)];

    

    [self.imgViewaddGestureRecognizer:pan];

}


- (void)pan:(UIPanGestureRecognizer *)pan{

    // 获取移动的点

    

    CGPoint point = [pan translationInView:self.imgView];

    NSLog(@"%@",NSStringFromCGPoint(point));

    self.imgView.transform =CGAffineTransformTranslate(self.imgView.transform, point.x, point.y);

    // 复位

    [pan setTranslation:CGPointZeroinView:self.imgView];

}





#pragma mark - / ******** UIGestureRecoginzerDelegate方法 **********/


// 是否可以同时执行多个手势

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    return YES;

}


// 手势是否开始,参数是手势类型

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{

    NSLog(@"%@",gestureRecognizer);

    return YES;

}


// 是否允许接收Touch

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

    

    return YES;

}


#pragma mark - / ******** 捏合方式实现 **********/

- (void)addPinch{

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc] initWithTarget:selfaction:@selector(pinch:)];

    

    [self.imgViewaddGestureRecognizer:pinch];

    // 属性设置,缩放比例和缩放大小

    pinch.delegate = self;

    // 同时设置旋转

    [selfaddRotation];

}


- (void)pinch:(UIPinchGestureRecognizer *)pinch{

    self.imgView.transform =CGAffineTransformScale(self.imgView.transform, pinch.scale, pinch.scale);

    pinch.scale = 1;

}


#pragma mark - / ******** 旋转方式实现 **********/

- (void)addRotation{

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizeralloc] initWithTarget:selfaction:@selector(rotation:)];

    

    rotation.delegate = self;

    

    [self.imgViewaddGestureRecognizer:rotation];

}


// 让图片选装

- (void)rotation:(UIRotationGestureRecognizer *)rotation{

    self.imgView.transform =CGAffineTransformRotate(self.imgView.transform, rotation.rotation);

    rotation.rotation = 0;

}


#pragma mark - / ******** 清扫方式实现 **********/

- (void)addSwipe{

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(swipe:)];

    

    // 设置清扫方向

    swipe.direction =UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;

    

    [self.imgViewaddGestureRecognizer:swipe];

}

- (void)swipe:(UISwipeGestureRecognizer *)swipe{

    NSLog(@"%s",__func__);

}



#pragma mark - / ******** 点按收拾实现 **********/

- (void)longPress{

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPress:)];


    [self.imgViewaddGestureRecognizer:longPress];

}


- (void)longPress:(UILongPressGestureRecognizer *)longPress{

    // 方法默认对调用2次,解决方案是判断状态

    if(longPress.state ==UIGestureRecognizerStateBegan){

        

        NSLog(@"%s",__func__);

    }

}


#pragma mark - / ******** 点按收拾实现 **********/

- (void)addTap{

    // 点按收拾

    UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tap)];


    [self.imgViewaddGestureRecognizer:tap];

}


- (void)tap{

    NSLog(@"%s",__func__);

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值