【发布时间】:2012-11-01 04:43:06
【问题描述】:
我在旋转和移动图像 (UIView) 时遇到问题。
如果我用这个来旋转:
[UIView beginAnimations: @"rotate" contex: nil];
[UIView setAnimationDuration: 1];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
spaceShip.transform = CGAffineTransformMakeRotation (angle); // spaceShip is the UIView
[UIView commitAnimations];
这个要移动:
[UIView beginAnimations: @"move" contex: nil];
[UIView setAnimationDuration: 1];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
spaceShip.center = destanation // destanation is a point where user tap
[UIView commitAnimations];
它一次运行良好。但是这两个动画一起不起作用。太空船正在旋转,但没有朝某个方向移动。
我的错在哪里?
P.S. 早期我已经指定了锚点(0.5,0.5)。但是当我旋转图像时,它的坐标正在改变。
UPD:
使用基于块的方法。像这样
- (void) viewDidLoad
{
[spaceShip.layer setAnchorPoint: CGPointMake(0.5, 0.5)];
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *tap = [[event allTouches] anyObject];
destanationPoint = [tap locationInView: tap.view];
NSLog(@"%0.3f", angle*180/M_PI);//Just for monitoring
[UIView animateWithDuration:1.0
delay: 0.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
spaceShip.transform = CGAffineTransformMakeRotation(angle);
}
completion:^(BOOL finished){
// Wait one second and then fade in the view
[UIView animateWithDuration:1.0
delay: 1.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
spaceShip.center = destanationPoint;
}
completion:nil];
}];
}
该方法有效。但是每个新的点击 UIImage 都会改变起始位置的坐标。好的,我尝试添加:
options: UIViewAnimationOptionCurveEaseOut |
UIViewAnimationOptionBeginFromCurrentState
但这无济于事。
UPD2
试试这个:
[UIView animateWithDuration:1.0
delay: 0.0
options: UIViewAnimationOptionCurveEaseOut |
UIViewAnimationOptionBeginFromCurrentState
animations:^{
spaceShip.transform = CGAffineTransformMakeRotation(angle);
spaceShip.center = destanationPoint;
}
completion:^(BOOL finished){
// Wait one second and then fade in the view
[UIView animateWithDuration:1.0
delay: 1.0
options: UIViewAnimationOptionCurveEaseOut |
UIViewAnimationOptionBeginFromCurrentState
animations:^{
spaceShip.center = destanationPoint;
}
completion:nil];
}];
现在我的船旋转并移动到点击点。在另一个点的第二次点击中,UIView 返回起始坐标,但是....带有动画))
【问题讨论】:
-
嗯,尝试在您的完成块中执行
spaceShip.center = destanationPoint;(即使 BOOL 参数为 NO)以及动画块。如果这不起作用,我将不得不对动画进行更多研究才能为您提供帮助。 -
在这种情况下,最简单的方法是将船舶视图放置到另一个 UIView 中。将移动动画设置为运送视图父视图,并将旋转设置为运送视图。
标签: iphone objective-c ios