码迷,mamicode.com
首页 > 其他好文 > 详细

手势识别器GestureRecognizer

时间:2016-03-23 16:51:51      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:

先放在这里  改天来修改

 

 

 

#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

 

手势识别器GestureRecognizer

标签:

原文地址:http://www.cnblogs.com/kinghx/p/5311593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!