總結(jié)下UIView一些比較重要的屬性方法
事件轉(zhuǎn)遞前翎,坐標(biāo)轉(zhuǎn)換
/** 當(dāng)前UIView對(duì)象上產(chǎn)生觸摸事件時(shí)觸發(fā)稚配,返回事件接收對(duì)象 */
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event;
可重寫(xiě)這個(gè)方法,來(lái)完成一些指定的事件港华。比如說(shuō)按鈕被遮到下面了道川,但是我想讓點(diǎn)擊到這塊區(qū)域的時(shí)候讓按鈕去相應(yīng)點(diǎn)擊
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
// 當(dāng)前坐標(biāo)系上的點(diǎn)轉(zhuǎn)換到按鈕上的點(diǎn)
CGPoint btnP = [self convertPoint:point toView:self.btn];
// 判斷點(diǎn)在不在按鈕上
if ([self.btn pointInside:btnP withEvent:event]) {
// 點(diǎn)在按鈕上
return self.btn;
}else{
return [super hitTest:point withEvent:event];
}
}
/** 判斷當(dāng)前的點(diǎn)擊或者觸摸事件的點(diǎn)是否在當(dāng)前的view中,默認(rèn)返回YES */
- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event;
?
?
坐標(biāo)轉(zhuǎn)換
常用于視圖在屏幕上占的坐置
/** 將像素point由point所在視圖轉(zhuǎn)換到目標(biāo)視圖view中立宜,返回在目標(biāo)視圖view中的像素值 */
- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
/** 與上方法相反*/
- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;
/** 將rect由rect所在視圖轉(zhuǎn)換到目標(biāo)視圖view中冒萄,返回在目標(biāo)視圖view中的rect */
- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
/** 與上方法相反*/
- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;
/** 返回“最佳”大小適合給定的大小 */
- (CGSize)sizeThatFits:(CGSize)size;
/** 調(diào)整為剛好合適子視圖大小 */
- (void)sizeToFit;
?
?
?
視圖層次管理
/** 獲取視圖所在的Window */
@property(nullable, nonatomic,readonly) UIWindow *window;
/** 插入子視圖(將子視圖插到siblingSubview之下) */
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
/** 插入子視圖(將子視圖插到siblingSubview之上) */
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
/** 將子視圖拉到最上面來(lái)顯示 */
- (void)bringSubviewToFront:(UIView *)view;
/** 將子視圖拉到最下面來(lái)顯示 */
- (void)sendSubviewToBack:(UIView *)view;
##pragma mark - 系統(tǒng)自動(dòng)調(diào)用(留給子類去實(shí)現(xiàn))
/** 添加自視圖完成后調(diào)用 */
- (void)didAddSubview:(UIView *)subview;
/** 將要移除自視圖時(shí)調(diào)用 */
- (void)willRemoveSubview:(UIView *)subview;
/** 對(duì)現(xiàn)在有布局有調(diào)整更改后,使用這個(gè)方法進(jìn)行更新 */
- (void)setNeedsLayout;
/** 強(qiáng)制進(jìn)行更新layout */
- (void)layoutIfNeeded;
/** 控件的frame發(fā)生改變的時(shí)候就會(huì)調(diào)用,一般在這里重寫(xiě)布局子控件的位置和尺寸 */
- (void)layoutSubviews;
setNeedsLayout代表需要更新布局橙数,會(huì)在下一次runloop循環(huán)時(shí)更新布局尊流。
而layoutIfNeeded需要與布局更新搭配使用,會(huì)在當(dāng)前runloop循環(huán)中更新布局灯帮。
具體可參考該文章:setNeedsLayout和layoutIfNeeded看我就懂崖技!
?
?
?
渲染
/** 重寫(xiě)drawRect方法逻住,在可以這里進(jìn)行繪圖操作。*/
- (void)drawRect:(CGRect)rect;
/** 標(biāo)記整個(gè)視圖的邊界矩形需要重繪, 調(diào)用這個(gè)方法會(huì)自動(dòng)調(diào)用drawRect方法 */
- (void)setNeedsDisplay;
/** 標(biāo)記在指定區(qū)域內(nèi)的視圖的邊界需要重繪, 調(diào)用這個(gè)方法會(huì)自動(dòng)調(diào)用drawRect方法 */
- (void)setNeedsDisplayInRect:(CGRect)rect;
/** 是否裁剪超出Bounds范圍的子控件迎献,默認(rèn)NO */
@property(nonatomic) BOOL clipsToBounds;
/** 內(nèi)容顯示的模式瞎访,默認(rèn)UIViewContentModeScaleToFill */
@property(nonatomic) UIViewContentMode contentMode;
/** 拉伸屬性,如圖片拉伸 */
@property(nonatomic) CGRect contentStretch NS_DEPRECATED_IOS(3_0,6_0) __TVOS_PROHIBITED;
/** 蒙板view */
@property(nullable, nonatomic,strong) UIView *maskView NS_AVAILABLE_IOS(8_0);