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
關於這兩個例子,可以看之前文章的說明,見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中的座標轉換