偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

IOS UI學習 ScrollView中Touch事件作用子視圖

移動開發(fā) iOS
本文介紹的是IOS UI學習,當多個視圖進行疊加的時候,Touch事件是作用到最上面的視圖上,詳細內容來看本文介紹。

IOS UI學習 ScrollViewTouch事件作用子視圖是本文要介紹對內容,我們知道當多個視圖進行疊加的時候,touch事件是作用到最上面的視圖上,但是如果父視圖UIScrollView,如果默認,可能touch子視圖會造成UIScrollView的滾動。

UIScrollView滾動的原因,可以看UIScrollView 原理,地址:http://www.cocoachina.com/bbs/read.php?tid-40965-page-1.html

我在這里簡單的描述一下,UIScrollView的工作原理,當手指touch的時候,UIScrollView會攔截Event,會等待一段時間,在這段時間內,如果沒有手指沒有移動,當時間結束時,UIScrollView會發(fā)送tracking events到子視圖上。在時間結束前,手指發(fā)生了移動,那么UIScrollView就會進行移動,從而取笑發(fā)送tracking。

那么,UIScrollView的子類想要接受touch事件,就是用戶點擊UIScrollView上的視圖時,要先處理視圖上的touch,而不發(fā)生滾動。這時候就需要UIScrollView的子類重載touchesShouldBegin:withEvent:inContentView: ,從而決定自己是否接受子視圖中的touch事件。

上面都是理論的知識,下面看一個簡單的例子:

外面紅色是一個UIScrollView,黃色是在UIScrollView上添加的UIView。最后的效果是,當在黃色區(qū)域內touch時,touch事件會作用到UIView上,當touch紅色區(qū)域時,整個視圖上下滾動。下面是實現(xiàn)的過程。

一、創(chuàng)建工程,然后創(chuàng)建myScrollView,并且myScrollView繼承自UIScrollView。

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

具體的實現(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(@"用戶點擊了scroll上的視圖%@,是否開始滾動scroll",view);   
  28.     //返回yes 是不滾動 scroll 返回no 是滾動scroll   
  29.     return YES;   
  30. }   
  31. - (BOOL)touchesShouldCancelInContentView:(UIView *)view   
  32. {   
  33.     
  34.     NSLog(@"用戶點擊的視圖 %@",view);   
  35.      
  36.     //NO scroll不可以滾動 YES scroll可以滾動   
  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,是用戶點擊黃色區(qū)域內,先觸發(fā)這個方法,當返回YES時,touch事件作用到黃色視圖上,當返回no時,紅色可以上下滾動。

(BOOL)touchesShouldCancelInContentView:(UIView *)view是發(fā)送tracking前,先作用這個方法。

下面是點擊黃的區(qū)域的日志:

2011-06-02 10:19:42.469 scrollTouch[38255:207] 用戶點擊了scroll上的視圖<MyView: 0x4e26f90; frame = (1 3; 100 200); layer = <CALayer: 0x4e270a0>>,是否開始滾動scroll
2011-06-02 10:19:42.658 scrollTouch[38255:207] 用戶點擊的視圖 <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ā)送滾動的通知 但是就算手指移動 scroll也不會動了 YES 發(fā)送通知 scroo可以移動   
  10.     [view setCanCancelContentTouches:YES];   
  11.     [view setBounces:NO];   
  12.     // NO 立即通知touchesShouldBegin:withEvent:inContentView 看是否滾動 scroll   
  13.     [view setDelaysContentTouches:NO];   
  14.     [view setContentSize:CGSizeMake(300, 900)];   
  15.     [self.view addSubview:view];   
  16.     [view release];   
  17. }  

三、MyView視圖的實現(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 

小結:IOS UI學習 ScrollViewTouch事件作用子視圖的內容介紹我那了,希望本文對你有所幫助!

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

本文來自:http://wangjun.easymorse.com/?p=1308

【編輯推薦】

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

 

責任編輯:zhaolei 來源: 互聯(liá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開發(fā)UIWebViewTouch事件

2014-10-15 10:09:12

iOS 8Touch ID開發(fā)

2013-04-24 11:15:56

Android開發(fā)Touch事件傳遞機制

2011-08-22 10:49:42

Cocos2d 開發(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驗證iOS 8

2013-06-14 13:50:28

iOS開發(fā)移動開發(fā)警告視圖

2011-06-15 16:11:51

UIKitCocoa TouchiOS

2011-06-27 15:39:51

Cocoa Touch

2013-04-24 11:11:20

Android開發(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
點贊
收藏

51CTO技術棧公眾號