Friday, 23 August 2013

How to update the background of a UIImageVIew?

How to update the background of a UIImageVIew?

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"];
}
}

No comments:

Post a Comment