我正在尝试将图像视图向前移动一段距离,但似乎无法使其正常工作。我的turnRight和turnLeft方法工作正常。我只是遇到了moveForward方法的问题。以下是我正在尝试的内容:
- (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;
}
答案 0 :(得分:1)
这里有两件事要考虑。
1)确保在主线程上进行UI更改 2)使用UIView的动画方法
- (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;
}];
}