Objective c 目标C:背景和;方向更改期间的帧问题

Objective c 目标C:背景和;方向更改期间的帧问题,objective-c,ios,xcode,ipad,core-graphics,Objective C,Ios,Xcode,Ipad,Core Graphics,每当我将iPad转向横向时,我的背景和框架都会出现问题 肖像: 风景画:我不能在黄色箭头后的那一边画画,我的背景仍然是肖像画,并且还在重复 这是我的密码 在viewDidLoad上: background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"mickey.png"]]; self.view.backgroundColor = background; touchDraw = [[U

每当我将iPad转向横向时,我的背景和框架都会出现问题

肖像:

风景画:我不能在黄色箭头后的那一边画画,我的背景仍然是肖像画,并且还在重复

这是我的密码

在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;
触摸时移动:

    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时,这发生在TouchsMoved上

也许是因为这句话:

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

我以前尝试过解决这个问题,但仍然是一样的。

通过快速阅读您的代码,我猜测出了什么问题

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

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

我认为你需要做的就是:

touchDraw.autoresizingMask = UIViewAutoresizingFlexibleHeight
    | UIViewAutoresizingFlexibleWidth;
这告诉视图,当方向改变时,它应该自动调整

您可能还需要将此方法添加到视图控制器(我忘记了此方法的默认设置):

通过将以下两种方法添加到视图控制器,可以对方向更改进行更细粒度的控制:

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

请注意,如果没有边框,很难看到视图的位置。要进行调试,可以使用以下命令设置边框:

    touchDraw.layer.borderWidth = 1;
    touchDraw.layer.borderColor = [[UIColor blackColor] CGColor];
我认为要访问上面的图层属性,还需要包括以下内容#导入:

#导入
在处理视图的位置时,我经常添加边框,以便清楚地看到视图所在的位置


请注意,“Interface Builder”中可能有一个设置来设置自动旋转,但您是用C代码创建视图的,因此没有设置该视图。

谢谢您,先生!这是工作,但有一些错误,当我开始触摸我的屏幕上的横向方向//看上面我编辑的帖子。我不太明白你问题的更新。这张照片是什么样子的?你期待什么?我能想到的几个建议是:1)确保将自动调整大小的任务也添加到父视图中,并且在TouchsMoved例程中,保持视图的一致性。可以在父视图和touchDraw视图之间切换。它们应该是相同的大小,但如果它们不是,则可能存在一些一致性问题。
- (void) didRotateFromInterfaceOrientation:
    (UIInterfaceOrientation) orientation {
    touchDraw.layer.borderWidth = 1;
    touchDraw.layer.borderColor = [[UIColor blackColor] CGColor];
#import <QuartzCore/QuartzCore.h>