IOS UI學(xué)習(xí) ScrollView中Touch事件作用子視圖
IOS UI學(xué)習(xí) ScrollView中Touch事件作用子視圖是本文要介紹對(duì)內(nèi)容,我們知道當(dāng)多個(gè)視圖進(jìn)行疊加的時(shí)候,touch事件是作用到最上面的視圖上,但是如果父視圖是UIScrollView,如果默認(rèn),可能touch子視圖會(huì)造成UIScrollView的滾動(dòng)。
UIScrollView滾動(dòng)的原因,可以看UIScrollView 原理,地址:http://www.cocoachina.com/bbs/read.php?tid-40965-page-1.html
我在這里簡(jiǎn)單的描述一下,UIScrollView的工作原理,當(dāng)手指touch的時(shí)候,UIScrollView會(huì)攔截Event,會(huì)等待一段時(shí)間,在這段時(shí)間內(nèi),如果沒(méi)有手指沒(méi)有移動(dòng),當(dāng)時(shí)間結(jié)束時(shí),UIScrollView會(huì)發(fā)送tracking events到子視圖上。在時(shí)間結(jié)束前,手指發(fā)生了移動(dòng),那么UIScrollView就會(huì)進(jìn)行移動(dòng),從而取笑發(fā)送tracking。
那么,UIScrollView的子類想要接受touch事件,就是用戶點(diǎn)擊UIScrollView上的視圖時(shí),要先處理視圖上的touch,而不發(fā)生滾動(dòng)。這時(shí)候就需要UIScrollView的子類重載touchesShouldBegin:withEvent:inContentView: ,從而決定自己是否接受子視圖中的touch事件。
上面都是理論的知識(shí),下面看一個(gè)簡(jiǎn)單的例子:
外面紅色是一個(gè)UIScrollView,黃色是在UIScrollView上添加的UIView。最后的效果是,當(dāng)在黃色區(qū)域內(nèi)touch時(shí),touch事件會(huì)作用到UIView上,當(dāng)touch紅色區(qū)域時(shí),整個(gè)視圖上下滾動(dòng)。下面是實(shí)現(xiàn)的過(guò)程。
一、創(chuàng)建工程,然后創(chuàng)建myScrollView,并且myScrollView繼承自UIScrollView。
- #import <UIKit/UIKit.h>
- @interface myScrollView : UIScrollView {
- }
- @end
具體的實(shí)現(xiàn):
- #import "myScrollView.h"
- #import "MyView.h"
- @implementation myScrollView
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self setBackgroundColor:[UIColor redColor]];
- MyView *myView=[[MyView alloc] initWithFrame:CGRectMake(1, 3, 100, 200)];
- [self addSubview:myView];
- [myView release];
- }
- return self;
- }
- - (void)dealloc
- {
- [super dealloc];
- }
- - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
- {
- NSLog(@"用戶點(diǎn)擊了scroll上的視圖%@,是否開(kāi)始滾動(dòng)scroll",view);
- //返回yes 是不滾動(dòng) scroll 返回no 是滾動(dòng)scroll
- return YES;
- }
- - (BOOL)touchesShouldCancelInContentView:(UIView *)view
- {
- NSLog(@"用戶點(diǎn)擊的視圖 %@",view);
- //NO scroll不可以滾動(dòng) YES scroll可以滾動(dòng)
- return NO;
- }
- @end
重寫了- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view方法和- (BOOL)touchesShouldCancelInContentView:(UIView *)view方法。
其中(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view,是用戶點(diǎn)擊黃色區(qū)域內(nèi),先觸發(fā)這個(gè)方法,當(dāng)返回YES時(shí),touch事件作用到黃色視圖上,當(dāng)返回no時(shí),紅色可以上下滾動(dòng)。
(BOOL)touchesShouldCancelInContentView:(UIView *)view是發(fā)送tracking前,先作用這個(gè)方法。
下面是點(diǎn)擊黃的區(qū)域的日志:
2011-06-02 10:19:42.469 scrollTouch[38255:207] 用戶點(diǎn)擊了scroll上的視圖<MyView: 0x4e26f90; frame = (1 3; 100 200); layer = <CALayer: 0x4e270a0>>,是否開(kāi)始滾動(dòng)scroll
2011-06-02 10:19:42.658 scrollTouch[38255:207] 用戶點(diǎn)擊的視圖 <MyView: 0x4e26f90; frame = (1 3; 100 200); layer = <CALayer: 0x4e270a0>>
二、添加mySrollView到根視圖上
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- myScrollView *view=[[myScrollView alloc] initWithFrame:CGRectMake(10, 9, 300, 400)];
- [view setUserInteractionEnabled:YES];
- [view setScrollEnabled:YES];
- //NO 發(fā)送滾動(dòng)的通知 但是就算手指移動(dòng) scroll也不會(huì)動(dòng)了 YES 發(fā)送通知 scroo可以移動(dòng)
- [view setCanCancelContentTouches:YES];
- [view setBounces:NO];
- // NO 立即通知touchesShouldBegin:withEvent:inContentView 看是否滾動(dòng) scroll
- [view setDelaysContentTouches:NO];
- [view setContentSize:CGSizeMake(300, 900)];
- [self.view addSubview:view];
- [view release];
- }
三、MyView視圖的實(shí)現(xiàn)
- #import "MyView.h"
- @implementation MyView
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self setBackgroundColor:[UIColor yellowColor]];
- }
- return self;
- }
- - (void)dealloc
- {
- [super dealloc];
- }
- @end
小結(jié):IOS UI學(xué)習(xí) ScrollView中Touch事件作用子視圖的內(nèi)容介紹我那了,希望本文對(duì)你有所幫助!
源代碼:https://easymorse-iphone.googlecode.com/svn/trunk/scrollTouch/
本文來(lái)自:http://wangjun.easymorse.com/?p=1308
【編輯推薦】
- iOS學(xué)習(xí)筆記 多核編程和內(nèi)存管理
- iOS學(xué)習(xí)之路 獲取日期間隔方法
- iOS學(xué)習(xí)之路 窗口操作
- iOS 4.2支持HTML5新特性
- iOS開(kāi)發(fā) UIViewController內(nèi)存管理