前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IOS5开发-UIScrollView添加单击事件的方法

IOS5开发-UIScrollView添加单击事件的方法

作者头像
阿新
发布2018-04-12 15:53:21
1.8K0
发布2018-04-12 15:53:21
举报
文章被收录于专栏:c#开发者

UIScrollView在开发中是一个非常常用的控件,UIScrollView具有水平、垂直滚动和缩放效果。但是尽然没有响应单击事件这个事件。而这个事件在日常的交互中是非常需要的。比如当用于单击或轻触图片的某个位置时,给于一些交互性提示。

下面我将用例子说明一下如何给UIScrollView添加一个单击的响应。

代码如下:

添加一个自定义的UIScrollView,命名:UITouchScrollView

UITouchScrollView.h代码如下

代码语言:javascript
复制
#import <Foundation/Foundation.h>
@protocol UIScrollViewTouchesDelegate
-(void)scrollViewTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event whichView:(id)scrollView;
@end

@interface UITouchScrollView : UIScrollView
@property(nonatomic,assign) id<UIScrollViewTouchesDelegate> touchesdelegate;

@end

如果要想把单击事件传递出来,那么必须新建一个@Protocol UIScrollViewTouchesDelegate,用于响应并且对事件做出回调。这里说一下IOS的事件委托(Event Delegate)相对C#的事件委托还是不一样的,似乎实现起来没有C#方便。这里就不多说了。

UITouchScrollView.m代码如下

#import "UITouchScrollView.h"

代码语言:javascript
复制
@implementation UITouchScrollView
@synthesize touchesdelegate=_touchesdelegate;
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
    
    if (!self.dragging) {
        //run at ios5 ,no effect;
        [self.nextResponder touchesEnded: touches withEvent:event]; 
        if (_touchesdelegate!=nil) {
             
                    [_touchesdelegate scrollViewTouchesEnded:touches withEvent:event whichView:self];
                    }
        NSLog(@"UITouchScrollView nextResponder touchesEnded");
    }        
    [super touchesEnded: touches withEvent: event];
    
}
@end

以上代码只是调用一下自定义的Delegate的方法。但是这里注意一下

[self.nextResponder touchesEnded:touches withEvent:event];这句话的意思是将UIScrollView上的单击事件往下传递,传递到它的父UIView。这样如果父UIView上实现了touchesEnded这个方法,也会响应到。但是这样的写法经过测试在IOS5.0以前的版本可以。但IOS5以后的(包括5)这不能往下传递,这里我也不知道为什么。希望有知道的朋友说一下。

ViewContrller.m代码如下

#pragma mark -

代码语言:javascript
复制
#pragma mark touches event
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"view touch began");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"view touch ended");
}
-(void)scrollViewTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event whichView:(id)scrollView{
    NSLog(@"scrollView  touch ended");
    
}

功能完成,记得在ViewController.h上加上UIScrollViewTouchesDelegate协议。

另外在说明一下。本来我想用UITapGestureRecognizer来实现的,但是直接引发异常。不明白为什么UITapGestureRecognizer不能注册在UIScrollViews上。

//self.tapGestureRecoginzer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTaps:)];

代码语言:javascript
复制
//self.tapGestureRecoginzer.numberOfTapsRequired=1;
    //self.tapGestureRecoginzer.numberOfTouchesRequired=2;
    //[self.view addGestureRecognizer:self.tapGestureRecoginzer];
#pragma  mark -
#pragma mark handle taps event
-(void)handleTaps:(UITapGestureRecognizer *)sender{
    NSUInteger touchCounter=0;
    for (touchCounter=0; sender.numberOfTapsRequired; touchCounter++) {
        CGPoint touchPoint=[sender locationOfTouch:touchCounter inView:sender.view];
        
        NSLog(@"touch: %i: %@",touchCounter+1,NSStringFromCGPoint(touchPoint));
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012-03-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档