天天看點

IOS開發系列——UIView專題之五:常用開發技巧篇 5UIView開發技巧 6參考連結

5UIView開發技巧

5.1常用技巧

5.1.1使用半透明View與不透明SubView

半透明背景視圖隻能用此種方法設定顔色,否則subView也是半透明的。

blurView.backgroundColor= [UIColorcolorWithRed:0green:0blue:0alpha:0.3];

5.1.2[super layoutSubviews]要發到layoutSubviews方法末尾位置

在自定義子View中使用layoutSubviews時應注意,[superlayoutSubviews];最好放在方法預設最後執行,不然IOS7下面可能引起挂機。

5.1.3内容自适應屬性UIViewContentMode

UIImageView的contentMode這個屬性是用來設定圖檔的顯示方式,如居中、居右,是否縮放等,有以下幾個常量可供設定:

UIViewContentModeScaleToFill

UIViewContentModeScaleAspectFit

UIViewContentModeScaleAspectFill

UIViewContentModeRedraw

UIViewContentModeCenter

UIViewContentModeTop

UIViewContentModeBottom

UIViewContentModeLeft

UIViewContentModeRight

UIViewContentModeTopLeft

UIViewContentModeTopRight

UIViewContentModeBottomLeft

UIViewContentModeBottomRight

注意以上幾個常量,凡是沒有帶Scale的,當圖檔尺寸超過ImageView尺寸時,隻有部分顯示在ImageView中。UIViewContentModeScaleToFill屬性會導緻圖檔變形。UIViewContentModeScaleAspectFit會保證圖檔比例不變,而且全部顯示在ImageView中,這意味着ImageView會有部分空白。UIViewContentModeScaleAspectFill也會證圖檔比例不變,但是是填充整個ImageView的,可能隻有部分圖檔顯示出來。

5.1.4hitTest方法以及不規則區域内觸摸事件處理方法

5.1.4.1hitTest:withEvent:方法流程

iOS系統檢測到手指觸摸(Touch)操作時會将其放入目前活動Application的事件隊列,UIApplication會從事件隊列中取出觸摸事件并傳遞給key window(目前接收使用者事件的視窗)處理,window對象首先會使用hitTest:withEvent:方法尋找此次Touch操作初始點所在的視圖(View),即需要将觸摸事件傳遞給其處理的視圖,稱之為hit-test view。

window對象會在首先在view hierarchy的頂級view上調用hitTest:withEvent:,此方法會在視圖層級結構中的每個視圖上調用pointInside:withEvent:,如果pointInside:withEvent:傳回YES,則繼續逐級調用,直到找到touch操作發生的位置,這個視圖也就是hit-test view。

hitTest:withEvent:方法的處理流程如下:

•首先調用目前視圖的pointInside:withEvent:方法判斷觸摸點是否在目前視圖内;

•若傳回NO,則hitTest:withEvent:傳回nil;

•若傳回YES,則向目前視圖的所有子視圖(subviews)發送hitTest:withEvent:消息,所有子視圖的周遊順序是從top到bottom,即從subviews數組的末尾向前周遊,直到有子視圖傳回非空對象或者全部子視圖周遊完畢;

•若第一次有子視圖傳回非空對象,則hitTest:withEvent:方法傳回此對象,處理結束;

•如所有子視圖都傳回非,則hitTest:withEvent:方法傳回自身(self)。

hitTest:withEvent:方法忽略隐藏(hidden=YES)的視圖,禁止使用者操作(userInteractionEnabled=YES)的視圖,以及alpha級别小于0.01(alpha<0.01)的視圖。如果一個子視圖的區域超過父視圖的bound區域(父視圖的clipsToBounds屬性為NO,這樣超過父視圖bound區域的子視圖内容也會顯示),那麼正常情況下對子視圖在父視圖之外區域的觸摸操作不會被識别,因為父視圖的pointInside:withEvent:方法會傳回NO,這樣就不會繼續向下周遊子視圖了。當然,也可以重寫pointInside:withEvent:方法來處理這種情況。

對于每個觸摸操作都會有一個UITouch對象,UITouch對象用來表示一個觸摸操作,即一個手指在螢幕上按下、移動、離開的整個過程。UITouch對象在觸摸操作的過程中在不斷變化,是以在使用UITouch對象時,不能直接retain,而需要使用其他手段存儲UITouch的内部資訊。UITouch對象有一個view屬性,表示此觸摸操作初始發生所在的視圖,即上面檢測到的hit-test view,此屬性在UITouch的生命周期不再改變,即使觸摸操作後續移動到其他視圖之上。

【原】ios的hitTest方法以及不規則區域内觸摸事件處理方法

http://www.cnblogs.com/wengzilin/p/4249847.html

hitTest:withEvent:方法流程

http://blog.csdn.net/jiajiayouba/article/details/23447145

5.1.4.2使用hitTest自定義響應事件

1、hitTest Hacking the responder chain

在此例子中button,scrollview同為topView的子視圖,但scrollview覆寫在button之上,這樣在在button上的觸摸操作傳回的hit-test view為scrollview,button無法響應,可以修改topView的hitTest:withEvent:方法如下:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

UIView *result = [super hitTest:pointwithEvent:event];

CGPoint buttonPoint = [underButtonconvertPoint:point fromView:self];

if ([underButton pointInside:buttonPointwithEvent:event]) {

return underButton;

}

return result;

}

這樣如果觸摸點在button的範圍内,傳回hittestView為button,從button按鈕可以響應點選事件。

2、Paging-enabled UIScrollView with Previews

BSPreviewScrollView

關于這兩個例子,可以看之前文章的說明,見Paging-enabled

UIScrollView

5.1.5通過UIView對象擷取其所屬UIViewController

通過UIView對象擷取該對象所屬的UIViewController可以使用UIResponder的nextResponder方法獲得,UIView類繼承于UIResponder,是以可以直接使用。

根據文檔描述,如果View有view controller,則通過nextResponder方法傳回,如果沒有則傳回superview。

下面是英文原文:

if the viewhas a view controller, it is returned by nextResponder.

If there is noview controller, the method will return the superview

相關代碼如下:周遊該View的樹形結構,擷取到其所屬的ViewController

•- (UIViewController*)viewController {

•for(UIView* next = [self superview]; next; next = next.superview) {

•UIResponder* nextResponder = [next nextResponder];

•if([nextResponder isKindOfClass:[UIViewControllerclass]]) {

•return(UIViewController*)nextResponder;

•}

•}

•returnnil;

}

5.1.6坐标體系轉換

•//将像素point由point所在視圖轉換到目标視圖view中,傳回在目标視圖view中的像素值

•- (CGPoint)convertPoint:(CGPoint)pointtoView:(UIView*)view;

•//将像素point從view中轉換到目前視圖中,傳回在目前視圖中的像素值

•- (CGPoint)convertPoint:(CGPoint)pointfromView:(UIView*)view;

•//将rect由rect所在視圖轉換到目标視圖view中,傳回在目标視圖view中的rect

•- (CGRect)convertRect:(CGRect)recttoView:(UIView*)view;

•//将rect從view中轉換到目前視圖中,傳回在目前視圖中的rect

•- (CGRect)convertRect:(CGRect)rectfromView:(UIView*)view;

例把UITableViewCell中的subview(btn)的frame轉換到controllerA中

1// controllerA中有一個UITableView, UITableView裡有多行UITableVieCell,cell上放有一個button

2//在controllerA中實作:

3CGRect rc = [cellconvertRect:cell.btn.frametoView:self.view];

4或

5CGRect rc = [self.viewconvertRect:cell.btn.framefromView:cell];

6//此rc為btn在controllerA中的rect

7

8或當已知btn時:

9CGRect rc = [btn.superviewconvertRect:btn.frametoView:self.view];

10或

CGRect rc = [self.viewconvertRect:btn.framefromView:btn.superview];

6參考連結

iOS開發UI篇—UIWindow簡單介紹

http://www.cnblogs.com/wendingding/p/3770052.html

iOS動畫總結----UIView動畫

http://blog.csdn.net/huifeidexin_1/article/details/7597868

UIView動畫(過渡效果)的學習筆記

http://www.cnblogs.com/lovecode/archive/2011/11/05/2237077.html

動畫UIView

animateWithDuration使用詳解

http://blog.163.com/wzi_xiang/blog/static/65982961201211104227946/

UIView動畫效果的四種調用方式

http://www.cnblogs.com/sell/archive/2013/02/09/2909522.html

(Good)iOS事件分發機制(一)hit-Testing

http://suenblog.duapp.com/blog/100031/iOS事件分發機制(一)%20hit-Testing

(Good)iOS事件分發機制(二)The Responder Chain

http://suenblog.duapp.com/blog/100032/iOS事件分發機制(二)The%20Responder%20Chain

hitTest:withEvent:方法流程

http://blog.csdn.net/jiajiayouba/article/details/23447145

iOS的UIView的hitTest的分析

http://blog.csdn.net/sanjunsheng/article/details/25080797

[IOS]hitTest的作用與用法【轉】

http://blog.csdn.net/smking/article/details/9791365

iOS開發筆記--UIView中的坐标轉換

http://blog.csdn.net/hopedark/article/details/18215083

IOS--UIView中的坐标轉換

http://blog.sina.com.cn/s/blog_a573f7990102vgui.html