UIGestureRecognizer 手势

当你看到 UIGestureRecognizer 手势的时候。也许你会觉得这个东西应该很叼,但是感觉一般工程上也用不怎么到,最多来个单击屏幕上的时候可能会用到。

但是,当你在做 UIWebView与 Javascript 之间的交互的时候 你可能就会用到了。因为也许你要根据滑动的距离来调用 Javascript 的方法。(该部分本文章暂不讨论)。


UIGestureRecognizer概念

先说说 UIGestureRecognizer 到底是个什么东西。

UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势。UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为。

很简单一句话。UIGestureRecognizer 绝对是个好东西,它可以监听 当你对界面进行操作时

    1、点击 UITapGestureRecognizer (任意次数的拍击)  
    2、向里或向外捏UIPinchGestureRecognizer (用于缩放)  
    3、摇动或者拖拽UIPanGestureRecognizer  (跟擦碰可能有点类似 但是是任意方向的)
    4、擦碰UISwipeGestureRecognizer (上下左右滑动手势)  
    5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)  
    6、长按UILongPressGestureRecognizer 


我自己的代码 UIPanGestureRecognizer

假如你是在webView中添加的手势 下述一段代码必须要!

//必须要添加的代码 否则手势在webView中无效
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}


然后你就可以在viewDidLoad里放入

    UIPanGestureRecognizer * singleDown = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleDown:)];
    singleDown.delegate= self;
    [self.webView addGestureRecognizer:singleDown];
-(void)handleSingleDown:(UIPanGestureRecognizer *)sender{
    //当手指触碰到屏幕 开始拖动时
    if (sender.state == UIGestureRecognizerStateBegan) {
        _send = YES;
        _startLocation = [sender locationInView:self.view];
        if (_startLocation.y >1) {
            _tempY = _startLocation.y;
        }
    }
    
    else if (sender.state == UIGestureRecognizerStateEnded) {
    
        _send = YES;
        NSLog(@"%d",[[NSNumber numberWithFloat: _stopLocation.y - _tempY ] intValue]);
        if (0 == _contentOffset.y && [[NSNumber numberWithFloat: _stopLocation.y - _tempY ] intValue] >0) {
                [self.webView stringByEvaluatingJavaScriptFromString:@"_blank(0)"];
            }
    
    }
    //当正在拖动的过程中
    else if (sender.state == UIGestureRecognizerStateChanged){
        _stopLocation = [sender locationInView:self.view];
        
        if (0 == _contentOffset.y) {
            
            if ([[NSNumber numberWithFloat: _stopLocation.y - _tempY ] intValue] <140 && [[NSNumber numberWithFloat: _stopLocation.y - _tempY ] intValue] >0) {
                [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"_blank(%d)",[[NSNumber numberWithFloat: _stopLocation.y - _tempY ] intValue] ]];
            }else if (_send == YES && [[NSNumber numberWithFloat: _stopLocation.y - _tempY ] intValue] >= 140){
                _send = NO;
                [self.webView stringByEvaluatingJavaScriptFromString:@"_blank(140)"];
            }
        }
        
        if ( 75 < _startLocation.y  && _startLocation.y < 115) {
            if ( _stopLocation.x - _startLocation.x >0) {
                 NSLog(@"%d",[[NSNumber numberWithFloat:  _stopLocation.x - _startLocation.x ] intValue]);
                [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"scroll_type(0,(%d))",[[NSNumber numberWithFloat:  _stopLocation.x - _startLocation.x ] intValue] ]];
            }else if ( _stopLocation.x - _startLocation.x < 0){
                NSLog(@"%d",[[NSNumber numberWithFloat:  _startLocation.x - _stopLocation.x ] intValue]);
                [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"scroll_type(1,(%d))",[[NSNumber numberWithFloat:  _startLocation.x - _stopLocation.x ] intValue] ]];
            }
        }
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    _contentOffset.y = scrollView.contentOffset.y;

}

在代码中我并没有做多注释,而且可能由于这不是一个单纯的demo导致可能某些代码运行不到。但是不要紧。在这里面 你只需要注意几点:

[sender locationInView:self.view]  

- (CGPoint)locationInView:(UIView*)view;    返回的是CGPoint的类型。

 CGPoint _startLocation;  _startLocation = [sender locationInView:self.view];  _startLocation.y  _startLocation.x

你懂了吧!这个时候你就可以获得到你碰到屏幕的位置的x, y。当然 这个值是基于你添加到的父视图上的。

因此  你可以判断只有在哪个位置 手势才生效!这个点非常有趣!

这个方法不止是拖动,当然是包括了任何手势都可以使用!


 UIGestureRecognizerState      

typedef enum {  
    UIGestureRecognizerStatePossible,   
    UIGestureRecognizerStateBegan,   
    UIGestureRecognizerStateChanged,   
    UIGestureRecognizerStateEnded,   
    UIGestureRecognizerStateCancelled,  
    UIGestureRecognizerStateFailed,    
    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded   
} UIGestureRecognizerState;

你可以看到  手势都有几个状态。其中  一眼看过去就知道个大概了。我要说的是 

sender.state == UIGestureRecognizerStateChanged

经过我的测试,我觉得这个状态是 手势进行中执行的,所以 你就可以在滑动的过程中做些事情了!同时 在这里调用 [sender locationInView:self.view] ,这个值将是不断变化的。


NSNumber

 当你要获取到位置的数据时候。CGFloat。没错,这是浮点型小数。但是我们在某些情况转为整型怎么办。我在网上找了方法。可能还有更简单的吧。但是我感觉我用到的这个方法  让我对NSNumber有了一定的认识 没错。

NSLog(@"%d",[[NSNumber numberWithFloat_stopLocation.x - _startLocation.x ] intValue]);

你应该懂了。

你百度百度NSNumber  会发现NSNumber其实也是一个挺实用的东西。在此工程中的功能:

NSNumber
+ (NSNumber *)numberWithInt:(int)value;
+ (NSNumber *)numberWithDouble:(double)value;
- (int)intValue;
- (double)doubleValue;

NSNumber可以 将基本数据类型包装起来,形成一个对象,这样就可以给其发送消息,装入NSArray中等等。
NSNumber * intNumber=[NSNumber  numberWithInt:100];
NSNumber *floatNumber=[NSNUmber  numberWithFloat:100.00];
int i=[intNumber  intValue];
if([intNumber isEqualToNumber:floatNumber]) ....
NSNumber继承NSObject ,可以使用比较 compare: isEqual等消息。

其它的属性其它的手势

UIGestureRecognizer 有的那几个手势已经很全了,并且每个手势还有相对应的属性,可以满足你的非常非常多的条件。
比如 手指触摸的个数,触摸的方向等等。 这些就不再介绍,毕竟我也没遇到,只是看了看相关的文档,但是还是挺简单的。
总之,手势是个挺有意思的东西!而且很简单的代码实现的功能就会让人觉得很神奇!当一个app偶尔想要一两个炫丽的功能的时候 感觉很牛B!






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值