手势识别器GestureRecognizer

先放在这里  改天来修改

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _imgView.userInteractionEnabled = YES;

//    [self addTap];
    [self addPan];
//    [self addSwipe];
//    [self addLong];
//    [self addRotation];
//    [self addPinch];

}

- (void)addPinch{

    //设置缩放比例
//    @property (nonatomic)          CGFloat scale;               // scale relative to the touch points in screen coordinates
    //设置捏合速度
//    @property (nonatomic,readonly) CGFloat velocity;            // velocity of the pinch in scale/second

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] init];
    [pinch addTarget:self action:@selector(pinch:)];
    [_imgView addGestureRecognizer:pinch];
}

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

    gesture.view.transform = CGAffineTransformScale(gesture.view.transform, gesture.scale, gesture.scale);
    gesture.scale = 1;

}

- (void)addRotation{

//    @property (nonatomic)          CGFloat rotation;            // rotation in radians
//    @property (nonatomic,readonly) CGFloat velocity;            // velocity of the pinch in radians/second

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] init];
    [rotation addTarget:self action:@selector(rotation:)];
    [_imgView addGestureRecognizer:rotation];

}

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

    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);

    rotation.rotation = 0;

    NSLog(@"%lf",rotation.velocity);
}

- (void)addLong{

    //设置触发前的点击次数
//    @property (nonatomic) NSUInteger numberOfTapsRequired;      // Default is 0. The number of full taps required before the press for gesture to be recognized
    //设置触发的触摸点数
//    @property (nonatomic) NSUInteger numberOfTouchesRequired __TVOS_PROHIBITED;   // Default is 1. Number of fingers that must be held down for the gesture to be recognized
//    //设置最短的长按时间
//    @property (nonatomic) CFTimeInterval minimumPressDuration; // Default is 0.5. Time in seconds the fingers must be held down for the gesture to be recognized
    //设置在按触时时允许移动的最大距离 默认为10像素
//    @property (nonatomic) CGFloat allowableMovement;           // Default is 10. Maximum movement in pixels allowed before the gesture fails. Once recognized (after minimumPressDuration) there is no limit on finger movement for the remainder of the touch tracking

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];
    [longPress addTarget:self action:@selector(longPress:)];
    longPress.minimumPressDuration = 1;

    [_imgView addGestureRecognizer:longPress];
}

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

    NSLog(@"%@",gesture);
}

- (void)addSwipe{

//    触发点个数
//    @property(nonatomic) NSUInteger                        numberOfTouchesRequired __TVOS_PROHIBITED; // default is 1. the number of fingers that must swipe
//    轻扫方向
//    @property(nonatomic) UISwipeGestureRecognizerDirection direction;               // default is UISwipeGestureRecognizerDirectionRight. the desired direction of the swipe. multiple directions may be specified if they will result in the same behavior (for example, UITableView swipe delete)

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc ]init];
    [swipe addTarget:self action:@selector(swipe:)];

    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp;

    [_imgView addGestureRecognizer:swipe];

}

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

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"Began");
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    NSLog(@"Moved");
}

- (void)addTap{

    // 轻击 多少次
//    @property (nonatomic) NSUInteger  numberOfTapsRequired;       // Default is 1. The number of taps required to match
    // 几个 手指 轻击
//    @property (nonatomic) NSUInteger  numberOfTouchesRequired __TVOS_PROHIBITED;    // Default is 1. The number of fingers required to match

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(haha)];
    [tap addTarget:self action:@selector(tapGesture:)];
    //点击多少次(双击)
    //tap.numberOfTapsRequired = 2;
    //tap.numberOfTouchesRequired = 2;

    [_imgView addGestureRecognizer:tap];

    tap.cancelsTouchesInView = NO;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    NSLog(@"ended");
}

- (void)addPan{

//    设置拖拽的最少触发点,默认为1
//    @property (nonatomic)          NSUInteger minimumNumberOfTouches __TVOS_PROHIBITED;   // default is 1. the minimum number of touches required to match
//    设置最多触发点
//    @property (nonatomic)          NSUInteger maximumNumberOfTouches __TVOS_PROHIBITED;   // default is UINT_MAX. the maximum number of touches that can be down
//    获取当前位置
//    - (CGPoint)translationInView:(nullable UIView *)view;                        // translation in the coordinate system of the specified view
//    设置当前位置
//    - (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;
//
//    获取拖拽速度
//    - (CGPoint)velocityInView:(nullable UIView *)view;                           // velocity of the pan in pixels/second in the coordinate system of the specified view

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init];
    [pan addTarget:self action:@selector(panGesture:)];

    // 取消向发送touchMove,touchBegin等等发送消息
//    pan.cancelsTouchesInView = NO;
//    默认为NO;如果为YES,那么成功识别则不执行触摸开始事件,失败则执行触摸开始事件;如果为NO,则不管成功与否都执行触摸开始事件;
//    pan.delaysTouchesBegan = YES;
//    延时掉用TouchesEnded
//    pan.delaysTouchesEnded = NO;
    [_imgView addGestureRecognizer:pan];

}
//当触摸序列被诸如电话呼入这样的系统事件所取消时,发送touchesCancelled:withEvent:消息。
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

}

- (void)haha{

    NSLog(@"hahah");
}

- (void)panGesture:(UIPanGestureRecognizer *)ges{

//    NSLog(@"translationInView  - %@",NSStringFromCGPoint([ges translationInView:ges.view.superview]));
//    NSLog(@"velocityInView    - %@",NSStringFromCGPoint([ges velocityInView:ges.view]));
    NSLog(@"pan");

    CGPoint movePoint = [ges translationInView:ges.view.superview];
    ges.view.center = CGPointMake(movePoint.x+ges.view.center.x, movePoint.y+ges.view.center.y);
    [ges setTranslation:CGPointZero inView:ges.view.superview];

}

- (void)tapGesture:(UIGestureRecognizer *)ges{

    // 手势是否可用
//    ges.enabled = NO;

    NSLog(@"tap %ld",ges.state);

}

@end
时间: 2024-11-10 01:24:15

手势识别器GestureRecognizer的相关文章

UIKit框架(16)手势识别器

UIGestureRecognizer 利用手势识别器,能够轻松识别用户在某个view上面做一些常见的手势 UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势 UITapGestureRecognizer                敲击 UIPinchGestureRecognizer            捏合手势 UIPanGestureRecognizer               拖拽 UISwipeGestureRecog

iOS常用手势识别器

手势识别状态: typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { // 没有触摸事件发生,所有手势识别的默认状态 UIGestureRecognizerStatePossible, // 一个手势已经开始但尚未改变或者完成时 UIGestureRecognizerStateBegan, // 手势状态改变 UIGestureRecognizerStateChanged, // 手势完成 UIGestureRecognizerStateE

我的IOS学习之路(三):手势识别器

在iOS的学习中,对于手势的处理是极为重要的,如对于图片,我们经常需要进行旋转,缩放以及移动等.这里做一下总结,详见代码. 1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 UIImage *image = [UIImage imageNamed:@"018.png"]; 5 UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 6 imageView.

手势识别器的使用111

//     1.轻拍手势识别器的使用 UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)]; //设置需要轻拍的次数 tapGesture.numberOfTapsRequired=1; //设置需要触摸的个数: tapGesture.numberOfTouchesRequired=2;//使用Alt /

IOS开发UI篇—手势识别器(拖拽+旋转+缩放)

IOS开发UI篇—手势识别器(拖拽+旋转+缩放) 一.拖拽 示例代码: 1 // 2 // YYViewController.m 3 // 06-拖拽事件 4 // 5 // Created by apple on 14-6-19. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController ()

[iOS UI进阶 - 3.2] 手势识别器UIGestureRecognizer

A.系统提供的手势识别器 1.敲击手势 UITapGestureRecognizer numberOfTapsRequired: 敲击次数 numberOfTouchesRequired: 同时敲击触碰数(手指数) 1 - (void) testTap { 2 // 创建手势识别器 3 UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapR

UI开发----target/action设计模式和代理设计模式以及手势识别器

//  Created By 郭仔  2015年04月16日21:50:33 眼睛有点痛,视力也在急速下降,心灵之窗,注意保护! ================================================ 耦合是衡量?一个程序写的好坏的标准之?一, 耦合是衡量模块与模块之间关联程度的指标 "?高内聚,低耦合"是?面向对象编程的核⼼心思想: ================ 设计模式思想很重要的 ================== target/action设计模

手势识别器

UIImageView 这个类是iOS专门用来显示图片的类,几乎所有的图片,都是用这个类显示的. 初始化方法:initWithImage: UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.png"]]; img.frame = CGRectMake(100, 100, 200, 200); 通过图片名来初始化一个UIImageView类. 注意: ImageView的默认是NO,是

第五讲 设计模式 手势识别器

一.target/action设计模式 二.代理设计模式(delegate模式) delegate代理模式实现的步骤: 1.建立protocol协议文件,在其中声明想要执行的动作或事件 2.将协议引入要通过代理去实现的文件(在.h文件声明,在.m文件写需要通过代理去完成的事件) 3.让代理人(及代理别人执行文件的一方)遵守协议(.m文件),并实现相应的代理事件 实例代码: 练习:通过代理让自定义视图控制器帮助changeview(其是rootview子视图)完成变换颜色事件 Appdelegat