【问题标题】:UIView rotate and then moveUIView 旋转然后移动
【发布时间】: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


【解决方案1】:

欢迎来到 Stack Overflow :)

如果您阅读UIView Class Reference

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

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

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

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

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

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

【讨论】:

  • 谢谢!我尝试使用基于块的方法。它工作。但是......每次当我再次点击 UIView 从起始状态开始动画时,从起始坐标开始。也尝试使用选项:UIViewAnimationOptionBeginFromCurrentState,但它不起作用。
  • 你能发布你的新代码吗?不仅仅是动画块,它周围的代码——任何处理动画块中正在处理的变量的地方。
【解决方案2】:

试试这样,

{
    [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];
}

【讨论】:

  • 谢谢,但结果是一样的。它旋转,但不动。我尝试将 NSLog(@"test") 放入方法 animationStopped - 此文本不显示。
【解决方案3】:

在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!");
                         }];

【讨论】:

    【解决方案4】:

    只是替换

    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
              );
    

    【讨论】:

    • 谢谢,但不行。无论如何都不会改变。
    • 仅用于监控...尝试“code” NSLog(@" x= %f and y=%f", destination.x, destination.y );
    【解决方案5】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-27
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多