【UI基础】手势识别

来源:互联网 发布:php curl 不返回头部 编辑:程序博客网 时间:2024/04/27 22:23

//

//  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


0 0
原创粉丝点击