旋转和移动图像(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返回起始坐标,但....带动画))
答案 0 :(得分:1)
欢迎使用Stack Overflow:)
如果您阅读UIView Class Reference:
可以动画更改多个视图属性,即更改 该属性创建一个动画,将更改传达给用户 在很短的时间内。 UIView类完成了大部分工作 执行实际动画但你必须指出哪个 您想要动画的属性更改。有两种不同的方式 启动动画:
基于块的动画方法(如 animateWithDuration:animations :)大大简化了创建 动画。使用一个方法调用,可以指定动画 执行和动画的选项。但是,基于块 动画仅在iOS 4及更高版本中可用。如果你的申请 在早期版本的iOS上运行,你必须使用 beginAnimations:context:和commitAnimations类标记方法 动画的开始和结束。
所以我建议切换到块方法,因为它肯定更容易使用。
以下是一些涵盖主题的好链接:
现在,特别针对您的问题,您是否尝试将旋转和移动放入同一个动画块中?可能是两者相互抵消,特别是因为你使用[UIView setAnimationBeginsFromCurrentState: YES];
,我认为它会中断当前正在发生的任何动画。
答案 1 :(得分:0)
尝试这样,
{
[UIView beginAnimations: @"rotate" contex: nil];
[UIView setAnimationDuration: 1];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
spaceShip.transform = CGAffineTransformMakeRotation (angle); // spaceShip is the UIView
[UIView setAnimationDidStopSelector: @selector(animationStopped)];
[UIView commitAnimations];
}
然后,
-(void)animationStopped {
[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];
}
答案 2 :(得分:0)
它是UIView中设置动画的简单代码
// transform make rotation for the view
view.transform = CGAffineTransformMakeRotation(M_PI_2*1);
[UIView commitAnimations];
// it used to move view with animation
[UIView animateWithDuration:2
delay:1.0
options: UIViewAnimationCurveEaseOut
animations:^{
//set your new frame for UIView
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
答案 3 :(得分:0)
只需替换
spaceShip.center = destination
与
spaceShip.center = CGPointMake(destination.x, destination.y);
<强> UPD 强>
并添加NSLOG(在CGPoint目的地之后)
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *tap = [[event allTouches] anyObject];
CGPoint destanation = [tap locationInView: tap.view];
NSLog(@" x=%f and y=%f", destination.x, destination.y
);
答案 4 :(得分:0)
尝试取消选中&#34;使用Autolayout&#34;在Storyboard中的ViewController(标识和类型)中。
我遇到了同样的问题,这解决了我的问题。