1

SOLVED: Turn off auto-layout and use springs-struts

I have a Pong type game I've been experimenting with which uses three UIImageView objects from a xib file. One UIImageView is the ball, bouncing around the screen via NSTimer object, and the other two are the "paddles".

When the user taps the screen, the ball starts moving. Dragging a finger up-and-down moves the right side paddle. The paddle just uses a 10x60 PNG file as the background. When the user taps the screen, with the ball in motion, I wanted to temporarily change the background on the paddle as a visual effect. Problem is, changing the background of the paddle seems to reset the positions of the UIImageViews on the screen. Is there some esoteric property of iOS that resets the view or something when the background is changed or am I just doing it wrong?

.h file

@property (weak, nonatomic) IBOutlet UIImageView *paddleLeft;
@property (weak, nonatomic) IBOutlet UIImageView *paddleBall;
@property (weak, nonatomic) IBOutlet UIImageView *paddleRight;

.m file

@synthesize paddleRight;
@synthesize paddleLeft;
@synthesize paddleBall;

...

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog (@"touches began");

    if (self.paused)
    {
        self.paused = NO;
        NSLog (@"ball launch");
    }
    else
    {
        // hilite the paddle with different 10x60 image
        self.paddleRight.image = [UIImage imageNamed:@"rect_2.png"];
    }
}
Community
  • 1
  • 1
wufoo
  • 13,571
  • 12
  • 53
  • 78
  • As long are they are the same size setting `self.paddleRight.image` should not change the frame. – Justin Meiners Aug 23 '13 at 21:16
  • 1
    This could be an auto layout problem. If it is turned on, and you're changing frames, anything (like changing the image) that cause the system to layout the subviews will reset the position of object to that set by the constraints. – rdelmar Aug 23 '13 at 21:18
  • rdelmar - You were correct! After removing auto-layout from the xib and using springs/struts to adjust the contstraints it is no longer resetting the position of the views when changing images. Thanks! – wufoo Aug 26 '13 at 14:10
  • Justin - I've read that too. All my images are the same size however. Thanks anyway. – wufoo Aug 26 '13 at 14:11
  • Please write an answer instead of editing the question. – nhahtdh Aug 26 '13 at 14:19

1 Answers1

0

Turn off auto-layout and use springs-struts

wufoo
  • 13,571
  • 12
  • 53
  • 78