ios - 根据设备方向更改绘制矩形输出

标签 ios uiview orientation drawrect

我想使用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/

相关文章:

ios - 如果显式实现一个 getter,是否仍然创建了 ivar?

Swift:使 UIView 的轮廓看起来像草图

IOS:如何在 4 侧(顶部、右侧、底部和左侧)为 UIView 制作阴影

java - 自定义 Android 对话框方向更改

position - 澄清 OpenAL 监听器方向

ios - 在 Firebase 中通过电子邮件查找 UID

ios - 如何在SKSpriteNode中 "center"SKTexture

ios - Xcode 收到非 HTTP 200 响应 (400)

iphone - 具有 Xib 文件的 UIView 的子类

Android 操作栏方向更改怪癖 - 调用 tabSeleceted 和 ItemSelected 方法