objective-c - Objective-C : Background & Frame Issue During Orientation Change

标签 objective-c ios xcode ipad core-graphics

每当我将 iPad 切换为横屏模式时,背景和框架都会出现问题。

肖像:

enter image description here

风景:我无法在黄色箭头后的那一侧绘画,我的背景仍然是肖像并不断重复。

enter image description here

这是我的代码

在 viewDidLoad 上:

    background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"mickey.png"]];
    self.view.backgroundColor = background;

    touchDraw = [[UIImageView alloc] initWithImage:nil];
    touchDraw.frame = self.view.frame;
    [self.view addSubview:touchDraw];
    [self.view sendSubviewToBack:touchDraw];
    touchMoved = 0;

在 touchesMoved 上:

    touchSwiped = YES;
    UITouch *touch = [touches anyObject];   
    currentTouch = [touch locationInView:self.view];
    currentTouch.y -= 20;
    UIGraphicsBeginImageContext(self.view.frame.size);
    [touchDraw.image drawInRect:CGRectMake(0, 0, touchDraw.bounds.size.width, touchDraw.bounds.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), redAmt, greenAmt, blueAmt, alpha);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), endingPoint.x, endingPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentTouch.x, currentTouch.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    touchDraw.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    endingPoint = currentTouch;

    touchMoved++;

    if (touchMoved == 10) {
        touchMoved = 0;
    }

这是我的学校项目。

更新:

当我应用 autoResizingMask 时,这种情况发生在 touchesMoved 上。

enter image description here

可能是因为这条线:

[touchDraw.image drawInRect:CGRectMake(0, 0, touchDraw.bounds.size.width, touchDraw.bounds.size.height)];

我之前尝试过修复它,但还是一样。

最佳答案

通过快速阅读您的代码,这是我对问题所在的猜测。

您的主视图正在响应方向变化,这就是当您旋转设备时它会重新绘制的原因。你画了一个图案,图案重复。如果你想要一个不重复的背景,你需要创建一个 UIImageView 并将其图像设置为“mickey.png”

您创建的 View (touchDraw) 在创建时会被赋予一个框架,并且当 iPad 旋转时它不会动态更新。

我认为你需要做的就是:

touchDraw.autoresizingMask = UIViewAutoresizingFlexibleHeight
    | UIViewAutoresizingFlexibleWidth;

这告诉 View 它应该在方向改变时自动调整。

您可能还需要将此方法添加到您的 View Controller (我忘记了默认设置是什么):

- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

通过将这两个方法添加到您的 View Controller ,您可以对方向变化进行更细粒度的控制:

- (void)willRotateToInterfaceOrientation: (UIInterfaceOrientation) orient
                                duration: (NSTimeInterval) duration

- (void) didRotateFromInterfaceOrientation:
    (UIInterfaceOrientation) orientation {

请注意,如果没有边框,很难看到您的 View 在哪里。对于调试,您可以使用以下方法设置边框:

    touchDraw.layer.borderWidth = 1;
    touchDraw.layer.borderColor = [[UIColor blackColor] CGColor];

我认为要访问上面的层属性,您还需要包含这个#import:

#import <QuartzCore/QuartzCore.h>

当我调整我的 View 的位置时,我经常添加边框,这样我可以清楚地看到我的 View 在哪里。

请注意,“Interface Builder”中可能有一个用于设置自动旋转的设置,但您是在 C 代码中创建 View ,因此未被设置。

关于objective-c - Objective-C : Background & Frame Issue During Orientation Change,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8177604/

相关文章:

objective-c - ld : warning : Xcode 8. 3.2 构建上传成功但在 iTunes 连接事件中看不到构建

ios - Swift - 如何在 UIStackview 上设置点击事件

ios - 从图库加载照片

objective-c - iOS 多 View 应用程序

iphone - 返回 'self',但在我的自定义分配方法中未将其设置为 '[(super or self) init...]' 的结果

iphone - 从充当 CMS 的 Wordpress 下载数据的最佳方式是什么

ios - 本地化 Storyboard

xcode - NSImages 到 PDF 并在 Swift 中合并它们

ios - 如何在 Scene Kit 的 3D SCNNode 中添加 2D View

iphone - UITableViewCell 去除每行之间的白色并显示 UIView 的背景图像