1

我有旋转和移动图像(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];

它一次一个地运作良好。但是这两个动画一起不起作用。Space Ship 正在旋转,但没有朝某个方向移动。

我的错在哪里?

PS早期我已经指定了锚点(0.5、0.5)。但是当我旋转图像时,它的坐标正在改变。

升级版:

使用基于块的方法。像这样

- (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 返回起始坐标,但是....带有动画))

4

5 回答 5

1

欢迎来到堆栈溢出 :)

如果您阅读UIView 类参考

可以对多个视图属性的更改进行动画处理——也就是说,更改属性会创建一个动画,在短时间内将更改传达给用户。UIView 类完成了执行实际动画的大部分工作,但您仍然必须指出要为哪些属性更改设置动画。有两种不同的方式来启动动画:

  • 在 iOS 4 及更高版本中,使用基于块的动画方法。(推荐的)
  • 使用开始/提交动画方法。

基于块的动画方法(例如 animateWithDuration:animations:)极大地简化了动画的创建。通过一个方法调用,您可以指定要执行的动画和动画的选项。但是,基于块的动画仅在 iOS 4 及更高版本中可用。如果您的应用程序在早期版本的 iOS 上运行,您必须使用 beginAnimations:context: 和 commitAnimations 类方法来标记动画的开始和结束。

所以我建议切换到块方法,因为它肯定更容易使用。

以下是一些涵盖该主题的好链接:

现在,专门针对您的问题,您是否尝试过将旋转和移动放入同一个动画块中?这可能是两者相互抵消,特别是因为你使用[UIView setAnimationBeginsFromCurrentState: YES];它我相信会中断当前正在发生的任何动画。

于 2012-11-01T05:02:19.187 回答
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];
}
于 2012-11-01T04:59:07.550 回答
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!");
                         }];
于 2012-11-01T05:16:42.003 回答
0

尝试在 Storyboard 的 ViewController(身份和类型)中取消选中“使用自动布局”。

我有同样的问题,这解决了我的问题。

于 2014-03-31T19:43:54.007 回答
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
          );
于 2012-11-01T10:23:05.410 回答