【问题标题】:Slide and Rotate UIView滑动和旋转 UIView
【发布时间】:2012-07-18 21:54:43
【问题描述】:

我试图在旋转 UIView 的同时滑动它。最终结果基本上是一个 UIView,它在屏幕上向左旋转约 70 度,碰到边缘,向右旋转约 50 度,最后静止(在几次摆动之后)。我能够让它滑动和旋转,但是当我尝试为第二次旋转设置动画时(一旦它碰到边缘),框架会回到原始值(回到屏幕左侧)。一旦 UIView 滑过,如何让框架粘住?

[UIView animateWithDuration:.4 delay:0.0 
                    options:UIViewAnimationCurveEaseInOut
                 animations:^{
                     CGAffineTransform slideAndRotate = CGAffineTransformMakeTranslation(300, 0);
                     slideAndRotate = CGAffineTransformRotate(slideAndRotate, RADIANS(75));

                     self.ticketImageView.transform = slideAndRotate;                 
} completion:^(BOOL finished) {
    // set the frame to the location where it ended up
    NSLog(@"%@", NSStringFromCGRect(self.ticketImageView.frame));
    [UIView animateWithDuration:.35 
                          delay:0.05 
                        options:UIViewAnimationCurveEaseInOut 
     | UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         CGAffineTransform rightAngle1 = 
                         CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-45.0));
                         [[self ticketImageView] setTransform:rightAngle1];
    } completion:^(BOOL finished) {

    }];

【问题讨论】:

    标签: ios uiview core-animation


    【解决方案1】:

    在您的第一个动画块中,您正在创建包含平移和旋转的变换。在您的第二个动画块中,您正在创建一个包含旋转但不包含平移的变换。因此,当您为第二个变换设置动画时,您的视图不再被翻译。

    我看到您使用了UIViewAnimationOptionBeginFromCurrentState 选项。您可能认为此选项使第二个转换堆栈与第一个转换堆栈相同,但事实并非如此。无论选项如何,第二个转换都会替换第一个。

    UIView 有两个影响其位置的属性。一个是您正在使用的transform。另一个是center

    通常我们使用center 属性来移动视图,并且只使用transform 属性来旋转和缩放视图。我建议你也这样做。

    既然你说你想让视图再晃动几次,我建议你试试这样:

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    
        [UIView animateWithDuration:0.4 animations:^{
            self.ticketImageView.center = CGPointMake(180, self.ticketImageView.center.y);
        }];
    
        [self wobbleWithAmount:0.4 direction:1];
    }
    
    - (void)wobbleWithAmount:(CGFloat)amount direction:(CGFloat)direction {
        [UIView animateWithDuration:amount animations:^{
            self.ticketImageView.transform = CGAffineTransformMakeRotation(amount * direction * M_PI * 0.5f);
        } completion:^(BOOL finished) {
            if (amount > 0.05f) {
                [self wobbleWithAmount:amount * 0.5f direction:-direction];
            } else {
                [UIView animateWithDuration:amount * 0.5f animations:^{
                    self.ticketImageView.transform = CGAffineTransformIdentity;
                }];
            }
        }];
    }
    

    【讨论】:

    • 感谢您的宝贵建议!使用中心绝对比尝试转换坐标容易得多!这个算法效果很好。
    【解决方案2】:

    当您创建第二个变换rightAngle1 时,您应该使用[[self ticketImageView] transform] 而不是CGAffineTransformIdentity。使用恒等变换,您基本上可以擦除第一个变换并从头开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      相关资源
      最近更新 更多