2013-10-07 36 views
0

我想使用drawRect繪製6個框,將用作6個標籤輸出的背景我已經設置 - 當設備的方向更改時,我需要更改框的位置拉......我在想這樣做的if-else語句象下面這樣的:根據設備方向更改drawRect輸出

- (void)drawRect:(CGRect)rect 
{ 
// Drawing code 
if (UIInterfaceOrientationIsPortrait(orientation)) { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBFillColor(context, 0.611, 0.505, 0.321, 1.0); // Goldie brown colour 
    CGContextFillRect(context, CGRectMake(70, 277, 35, 50)); // Years 
    CGContextFillRect(context, CGRectMake(145, 277, 35, 50)); // Months 
    CGContextFillRect(context, CGRectMake(220, 277, 35, 50)); // Days 
    CGContextFillRect(context, CGRectMake(70, 393, 35, 50)); // Hours 
    CGContextFillRect(context, CGRectMake(145, 393, 35, 50)); // Minutes 
    CGContextFillRect(context, CGRectMake(220, 393, 35, 50)); // Seconds 
} else { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBFillColor(context, 0.611, 0.505, 0.321, 1.0); // Goldie brown colour 
    CGContextFillRect(context, CGRectMake(87, 221, 35, 50)); // Years 
    CGContextFillRect(context, CGRectMake(162, 221, 35, 50)); // Months 
    CGContextFillRect(context, CGRectMake(234, 221, 35, 50)); // Days 
    CGContextFillRect(context, CGRectMake(308, 221, 35, 50)); // Hours 
    CGContextFillRect(context, CGRectMake(385, 221, 35, 50)); // Minutes 
    CGContextFillRect(context, CGRectMake(466, 221, 35, 50)); // Seconds 
} 

,但是這不會工作,因爲(UIInterfaceOrientationIsPortrait(方向))需要一個參數/常量聲明&它並不像我能做到這一點這裏 - 有什麼我能做的嗎?

回答

1

您可以通過使用

if(UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) 

或通過在視圖控制器didRotateFromInterfaceOrientation:改變的標誌(例如BOOL shouldDrawPortrait)做到這一點。

0

設備何時改變方向,在didRotateFromInterfaceOrientation中,您必須致電方法setNeedDisplayUIView

ex:[box1 setNeedDisplay];