我想使用drawRect绘制6个框,这些框将用作我设置的6个标签输出的背景-当设备的方向改变时,我需要更改绘制框的位置...我在想执行如下 if-else 语句:
- (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(orientation)) 需要参数/常量声明,而且看起来我不能在这里做到这一点 - 有类似的事情我可以做吗?
最佳答案
你可以使用
if(UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
或者通过更改 View Controller 的 didRotateFromInterfaceOrientation:
中的标志(例如 BOOL shouldDrawPortrait
)。
关于ios - 根据设备方向更改绘制矩形输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19229577/