成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

IOS UI學(xué)習(xí) ScrollView中Touch事件作用子視圖

移動(dòng)開(kāi)發(fā) iOS
本文介紹的是IOS UI學(xué)習(xí),當(dāng)多個(gè)視圖進(jìn)行疊加的時(shí)候,Touch事件是作用到最上面的視圖上,詳細(xì)內(nèi)容來(lái)看本文介紹。

IOS UI學(xué)習(xí) ScrollViewTouch事件作用子視圖是本文要介紹對(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。

  1. #import <UIKit/UIKit.h> 
  2. @interface myScrollView : UIScrollView {   
  3. }  
  4. @end 

具體的實(shí)現(xiàn):

  1. #import "myScrollView.h"  
  2.  
  3. #import "MyView.h"  
  4.  
  5. @implementation myScrollView  
  6.  
  7. - (id)initWithFrame:(CGRect)frame   
  8. {   
  9.     self = [super initWithFrame:frame];   
  10.     if (self) {   
  11.         [self setBackgroundColor:[UIColor redColor]];   
  12.           
  13.         MyView *myView=[[MyView alloc] initWithFrame:CGRectMake(1, 3, 100, 200)];   
  14.         [self addSubview:myView];   
  15.         [myView release];   
  16.     }   
  17.     return self;   
  18. }  
  19.  
  20. - (void)dealloc   
  21. {   
  22.     [super dealloc];   
  23. }  
  24.  
  25. - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view   
  26. {   
  27.     NSLog(@"用戶點(diǎn)擊了scroll上的視圖%@,是否開(kāi)始滾動(dòng)scroll",view);   
  28.     //返回yes 是不滾動(dòng) scroll 返回no 是滾動(dòng)scroll   
  29.     return YES;   
  30. }   
  31. - (BOOL)touchesShouldCancelInContentView:(UIView *)view   
  32. {   
  33.     
  34.     NSLog(@"用戶點(diǎn)擊的視圖 %@",view);   
  35.      
  36.     //NO scroll不可以滾動(dòng) YES scroll可以滾動(dòng)   
  37.     return NO;   
  38. }   
  39. @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到根視圖上

  1. - (void)viewDidLoad   
  2. {   
  3.     [super viewDidLoad];   
  4.       
  5.     myScrollView *view=[[myScrollView alloc] initWithFrame:CGRectMake(10, 9, 300, 400)];   
  6.     [view setUserInteractionEnabled:YES];   
  7.     [view setScrollEnabled:YES];   
  8.       
  9.     //NO 發(fā)送滾動(dòng)的通知 但是就算手指移動(dòng) scroll也不會(huì)動(dòng)了 YES 發(fā)送通知 scroo可以移動(dòng)   
  10.     [view setCanCancelContentTouches:YES];   
  11.     [view setBounces:NO];   
  12.     // NO 立即通知touchesShouldBegin:withEvent:inContentView 看是否滾動(dòng) scroll   
  13.     [view setDelaysContentTouches:NO];   
  14.     [view setContentSize:CGSizeMake(300, 900)];   
  15.     [self.view addSubview:view];   
  16.     [view release];   
  17. }  

三、MyView視圖的實(shí)現(xiàn)

  1. #import "MyView.h"  
  2. @implementation MyView  
  3. - (id)initWithFrame:(CGRect)frame   
  4. {   
  5.     self = [super initWithFrame:frame];   
  6.     if (self) {   
  7.         [self setBackgroundColor:[UIColor yellowColor]];   
  8.     }   
  9.     return self;   
  10. }  
  11.  
  12. - (void)dealloc   
  13. {   
  14.     [super dealloc];   
  15. }  
  16. @end 

小結(jié):IOS UI學(xué)習(xí) ScrollViewTouch事件作用子視圖的內(nèi)容介紹我那了,希望本文對(duì)你有所幫助!

源代碼:https://easymorse-iphone.googlecode.com/svn/trunk/scrollTouch/

本文來(lái)自:http://wangjun.easymorse.com/?p=1308

【編輯推薦】

  1. iOS學(xué)習(xí)筆記 多核編程和內(nèi)存管理
  2. iOS學(xué)習(xí)之路 獲取日期間隔方法
  3. iOS學(xué)習(xí)之路 窗口操作
  4. iOS 4.2支持HTML5新特性
  5. iOS開(kāi)發(fā) UIViewController內(nèi)存管理

 

責(zé)任編輯:zhaolei 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-08-03 17:32:17

IOS UIScrollVi touch

2011-09-05 12:49:59

Sencha Touc事件

2013-05-21 09:54:39

Web前端

2011-08-17 10:09:25

iPhone開(kāi)發(fā)UIWebViewTouch事件

2014-10-15 10:09:12

iOS 8Touch ID開(kāi)發(fā)

2013-04-24 11:15:56

Android開(kāi)發(fā)Touch事件傳遞機(jī)制

2011-08-22 10:49:42

Cocos2d 開(kāi)發(fā)CCLayerTouch事件

2011-05-11 10:28:03

2015-07-08 16:46:05

iOS鍵盤

2011-04-02 17:21:29

sql server視圖

2011-10-26 10:32:05

Sencha Touc數(shù)據(jù)視圖

2014-10-13 09:57:31

SwiftTouch ID驗(yàn)證iOS 8

2013-06-14 13:50:28

iOS開(kāi)發(fā)移動(dòng)開(kāi)發(fā)警告視圖

2011-06-15 16:11:51

UIKitCocoa TouchiOS

2011-06-27 15:39:51

Cocoa Touch

2013-04-24 11:11:20

Android開(kāi)發(fā)touch事件發(fā)生傳遞

2009-08-16 19:43:07

linux中touchtouch命令linux命令行參數(shù)

2011-05-31 15:41:00

Cocoa TouchCocoaiOS

2019-12-04 14:30:43

事件日志Windows 10Windows

2017-01-04 10:18:00

React NativScrollViewAndroid
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 特黄毛片 | 九九伊人sl水蜜桃色推荐 | 国产精品久久久一区二区三区 | 日韩黄色免费 | 久久高清亚洲 | 亚洲国产一区视频 | 欧美专区在线 | 日韩中文字幕久久 | 久久精品欧美一区二区三区麻豆 | 国产精品久久久久久久久免费 | 欧美日韩91 | 在线国产小视频 | 国产精品区二区三区日本 | 一级片片 | 国产视频精品区 | 99精品久久久国产一区二区三 | 亚洲国产欧美一区 | 日韩精品激情 | 国产精品久久久久久影院8一贰佰 | 欧美视频精品 | 久久99精品久久久97夜夜嗨 | 久久精品欧美电影 | 91就要激情 | 91观看| 在线免费观看日本视频 | 国产欧美精品 | 欧美日韩国产一区二区三区 | 久久久噜噜噜久久中文字幕色伊伊 | 欧美成人精品一区 | 一区欧美 | 国产精品资源在线 | 伊人亚洲 | a视频在线观看 | 久久午夜精品福利一区二区 | 日韩视频一区二区 | 成人免费视频久久 | 日韩三级 | 中文字幕一区二区三区不卡 | 久久久久国产一区二区三区 | 欧美日韩不卡 | 国产有码|