Reputation: 3600
I am trying to move an image view forward by a set distance but can't seem to get it working. My turnRight and turnLeft methods work fine. I'm just having issues with the moveForward method. Here is what I'm trying:
- (IBAction)moveForward:(id)sender
{
if (turtle.image.imageOrientation == UIImageOrientationRight)
{
turtle.frame = CGRectMake(turtle.frame.origin.x + 10, turtle.frame.origin.y, 75, 75);
}
else if (turtle.image.imageOrientation == UIImageOrientationLeft)
{
turtle.frame = CGRectMake(turtle.frame.origin.x - 10, turtle.frame.origin.y, 75, 75);
}
else if (turtle.image.imageOrientation == UIImageOrientationUp)
{
turtle.frame = CGRectMake(turtle.frame.origin.x, turtle.frame.origin.y + 10, 75, 75);
}
else if (turtle.image.imageOrientation == UIImageOrientationDown)
{
turtle.frame = CGRectMake(turtle.frame.origin.x, turtle.frame.origin.y - 10, 75, 75);
}
}
- (IBAction)turnRight:(id)sender
{
UIImage *newImage;
switch (turtle.image.imageOrientation)
{
case UIImageOrientationUp:
newImage = [UIImage imageWithCGImage:[turtle.image CGImage] scale:1.0f orientation:UIImageOrientationRight];
turtle.contentMode = UIViewContentModeScaleAspectFit;
break;
case UIImageOrientationLeft:
newImage = [UIImage imageWithCGImage:[turtle.image CGImage]scale:1.0f orientation:UIImageOrientationUp];
turtle.contentMode = UIViewContentModeScaleAspectFill;
break;
case UIImageOrientationDown:
newImage = [UIImage imageWithCGImage:[turtle.image CGImage] scale:1.0f orientation:UIImageOrientationLeft];
turtle.contentMode = UIViewContentModeScaleAspectFit;
break;
case UIImageOrientationRight:
newImage = [UIImage imageWithCGImage:[turtle.image CGImage]scale:1.0f orientation:UIImageOrientationDown];
turtle.contentMode = UIViewContentModeScaleAspectFill;
break;
default:
break;
}
turtle.image = newImage;
}
- (IBAction)turnLeft:(id)sender
{
UIImage *newImage;
switch (turtle.image.imageOrientation)
{
case UIImageOrientationUp:
newImage = [UIImage imageWithCGImage:[turtle.image CGImage] scale:1.0f orientation:UIImageOrientationLeft];
turtle.contentMode = UIViewContentModeScaleAspectFit;
break;
case UIImageOrientationLeft:
newImage = [UIImage imageWithCGImage:[turtle.image CGImage]scale:1.0f orientation:UIImageOrientationDown];
turtle.contentMode = UIViewContentModeScaleAspectFill;
break;
case UIImageOrientationDown:
newImage = [UIImage imageWithCGImage:[turtle.image CGImage] scale:1.0f orientation:UIImageOrientationRight];
turtle.contentMode = UIViewContentModeScaleAspectFit;
break;
case UIImageOrientationRight:
newImage = [UIImage imageWithCGImage:[turtle.image CGImage]scale:1.0f orientation:UIImageOrientationUp];
turtle.contentMode = UIViewContentModeScaleAspectFill;
break;
default:
break;
}
turtle.image = newImage;
}
Upvotes: 0
Views: 57
Reputation: 4040
There are 2 things to consider here.
1) Make sure you are making UI changes on the main thread.
2) Use UIView's animation methods
- (IBAction)moveForward:(id)sender
{
CGRect frame = turtle.frame;
switch (turtle.image.imageOrientation)
{
case UIImageOrientationUp:
frame.origin.y + 10;
break;
case UIImageOrientationLeft:
frame.origin.x - 10;
break;
case UIImageOrientationDown:
frame.origin.y + 10;
break;
case UIImageOrientationRight:
frame.origin.x + 10;
break;
default:
break;
}
[UIView animateWithDuration:1.0 // The duration in seconds for the animation to complete.
animations:^{
turtle.frame = frame;
}];
}
Upvotes: 1