UIView的setNeedsDisplay和setNeedsLayout方法。首先兩個(gè)方法都是異步執(zhí)行的。而setNeedsDisplay會(huì)調(diào)用自動(dòng)調(diào)用drawRect方法,這樣可以拿到UIGraphicsGetCurrentContext,就可以畫畫了。而setNeedsLayout會(huì)默認(rèn)調(diào)用layoutSubViews,就可以處理子視圖中的一些數(shù)據(jù)。
宗上所訴,setNeedsDisplay方便繪圖,而layoutSubViews方便出來數(shù)據(jù)。
ipad橫豎屏切換解決方案
2011年08月01日 星期一 10:09
由于ipad的橫豎屏不同,所以好的應(yīng)用,橫豎屏的頁面布局也不一樣。那么就需要橫豎屏的整體解決方案。先看一個(gè)橫豎屏布局不一樣的界面。
上面兩張圖是來自同一個(gè)界面的橫豎版的截屏。可以看出,橫豎版顯示的內(nèi)容相同,但是界面布局不同。要實(shí)現(xiàn)上述布局,主要是運(yùn)用UIView中 layoutSubviews方法。當(dāng)UIView設(shè)置為自動(dòng)適配屏幕時(shí),當(dāng)用戶旋轉(zhuǎn)設(shè)備的時(shí)候,會(huì)調(diào)用layoutSubviews方法,我們只需重寫 這個(gè)方法,然后判斷用戶屏幕的方向。在調(diào)整每個(gè)空間的位置即可。
下面是實(shí)現(xiàn)上述界面的最簡(jiǎn)單的原型:
首先分析可以知道左面是圖片,右面是一個(gè)圖片加文字的視圖。下面就實(shí)現(xiàn)一個(gè)左面視圖右面是一個(gè)圖加一段字的事例。
事例的截圖如下:
其中右面的文字和綠色部分是用一個(gè)子視圖封裝的。
整個(gè)布局是我在主視圖中添加了一個(gè)ContentView視圖,在ContentView視圖中添加了一個(gè)ArticleView視圖。
其中ArticleView和ContentView的xib文件都打開了
在ContentView中重寫layoutSubviews方法,然后根據(jù)stausbar的方向判斷當(dāng)前視圖的橫豎屏。具體代碼:
-(void)layoutSubviews{
[super layoutSubviews];
UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {
//翻轉(zhuǎn)為豎屏?xí)r
[self setVerticalFrame];
}else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) {
//翻轉(zhuǎn)為橫屏?xí)r
[self setHorizontalFrame];
}
}
-(void)setVerticalFrame
{
NSLog(@"豎屏");
[titleLable setFrame:CGRectMake(283, 0, 239, 83)];
[leftView setFrame:CGRectMake(38, 102, 384, 272)];
[rightView setFrame:CGRectMake(450, 102, 282, 198)];
}
-(void)setHorizontalFrame
{
NSLog(@"橫屏");
[titleLable setFrame:CGRectMake(183, 0, 239, 83)];
[leftView setFrame:CGRectMake(168, 122, 384, 272)];
[rightView setFrame:CGRectMake(650, 122, 282, 198)];
}
在具體的橫豎屏方法中,從新設(shè)置各個(gè)組件的坐標(biāo)即可。
接下來在ContentView中添加ArticleView視圖。
-(id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) {
NSArray *arrayContentView =[[NSBundle mainBundle] loadNibNamed:@"ArticleView" owner:self options:nil];
rightView=[arrayContentView objectAtIndex:0];
[self addSubview:rightView];
}
return self;
}
由于我用的是xib,所以初始化方法為initWithCoder,在這個(gè)中添加新的視圖。
同樣在ArticleView中設(shè)置橫豎屏相應(yīng)空間的坐標(biāo)即可。
-(void)layoutSubviews{
[super layoutSubviews];
UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication] statusBarOrientation];
CGRect rect=self.frame;
rect.size.width=282;
rect.size.height=198;
[self setFrame:rect];
if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {
//翻轉(zhuǎn)為豎屏?xí)r
[self setVerticalFrame];
}else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) {
//翻轉(zhuǎn)為橫屏?xí)r
[self setHorizontalFrame];
}
}
-(void)setVerticalFrame
{
NSLog(@"豎屏");
[contentView setFrame:CGRectMake(12, 6, 250, 125)];
[textLable setFrame:CGRectMake(50, 139, 182, 39)];
}
-(void)setHorizontalFrame
{
NSLog(@"橫屏");
[contentView setFrame:CGRectMake(12, 6, 106, 158)];
[textLable setFrame:CGRectMake(135, 11, 147, 39)];
}
layoutSubviews何時(shí)調(diào)用的問題
layoutSubviews何時(shí)調(diào)用的問題,這個(gè)方法是當(dāng)你需要在調(diào)整subview的大小的時(shí)候需要重寫(我這個(gè)翻譯不嚴(yán)謹(jǐn),以下是原文:You should override this method only if the autoresizing behaviors of the subviews do not offer the behavior you want.),但有時(shí)候經(jīng)常指望它被調(diào)用的時(shí)候沒被調(diào)用,不希望它被調(diào)用的時(shí)候被調(diào)用了,搞的很上火。根據(jù)國(guó)外社區(qū)一個(gè)人帖子,做了總結(jié)性翻譯。
layoutSubviews在以下情況下會(huì)被調(diào)用:
1、init初始化不會(huì)觸發(fā)layoutSubviews
2、addSubview會(huì)觸發(fā)layoutSubviews
3、設(shè)置view的Frame會(huì)觸發(fā)layoutSubviews,當(dāng)然前提是frame的值設(shè)置前后發(fā)生了變化
4、滾動(dòng)一個(gè)UIScrollView會(huì)觸發(fā)layoutSubviews
5、旋轉(zhuǎn)Screen會(huì)觸發(fā)父UIView上的layoutSubviews事件
6、改變一個(gè)UIView大小的時(shí)候也會(huì)觸發(fā)父UIView上的layoutSubviews事件