Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C: Background & Frame Issue During Orientation Change

I am having a problem with my background and frame whenever I turn my iPad to landscape.

Portrait:

enter image description here

Landscape: I can't paint on that side after the yellow arrow and my background is still in portrait and repeating itself.

enter image description here

Here's my code

on 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;

on 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;
    }

It's for my school project.

UPDATE:

When I applied the autoResizingMask this happens on touchesMoved.

enter image description here

maybe because of this line:

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

I tried to fix that before but still the same.

like image 610
SeongHo Avatar asked Dec 21 '25 07:12

SeongHo


1 Answers

From quickly reading over your code, here is my guess as to what is wrong.

Your main view is responding to the orientation change which is why it is redrawing when you rotate your device. You painted a pattern, and patterns repeat. If you want to have a background that doesn't repeat, you would need to create a UIImageView and set its image to "mickey.png"

The view that you create (touchDraw) is being given a frame when it is created, and it is not dynamically updating when the iPad is rotated.

I think that all you should need to do is:

touchDraw.autoresizingMask = UIViewAutoresizingFlexibleHeight
    | UIViewAutoresizingFlexibleWidth;

This tells the view that it should auto-adjust when orientations change.

You might also need to add this method to your view controller (I forget what the default is for this):

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

You can have more fine-grained control over orientation changes by adding these two methods to your view controller:

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

and

- (void) didRotateFromInterfaceOrientation:
    (UIInterfaceOrientation) orientation {

Note that it is hard to see where your views are without a border. For debugging, you can set a border using:

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

I think to access the layer property above, you will also need to include this #import:

#import <QuartzCore/QuartzCore.h>

When I'm playing around with the locations for my views, I often add the borders so I can clearly see where my views are.

Note that there is probably a setting in "Interface Builder" to set the auto-rotation, but you are creating your view in C-code, so it is not being set.

like image 73
Ron Avatar answered Dec 22 '25 21:12

Ron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!