IOS5開發-UIScrollView添加單擊事件的方法


UIScrollView在開發中是一個非常常用的控件,UIScrollView具有水平、垂直滾動和縮放效果。但是盡然沒有響應單擊事件這個事件。而這個事件在日常的交互中是非常需要的。比如當用於單擊或輕觸圖片的某個位置時,給於一些交互性提示。

下面我將用例子說明一下如何給UIScrollView添加一個單擊的響應。

代碼如下:

添加一個自定義的UIScrollView,命名:UITouchScrollView

UITouchScrollView.h代碼如下

#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"


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

#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:)];

     // 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));
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM