【问题标题】:Animation of CGAffineTransform in iOS8 looks different than in iOS7iOS8 中 CGAffineTransform 的动画看起来与 iOS7 不同
【发布时间】:2014-08-13 12:20:36
【问题描述】:

我正在尝试找出 UIView 转换属性的动画在 iOS 8 和 iOS 6/7 中看起来不同的原因。

举个简单的例子,在 iOS 8 之前:

myView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, 1.57);
[UIView animateWithDuration:5 animations:^{
    myView.transform = CGAffineTransformTranslate(plane.transform, 100, 0);
}];

给出了预期的结果,“myView”旋转了 90 度并向下移动,但在 iOS8 中,当翻译动画时,它从我找不到解释的点开始(这会破坏动画)。

有人知道它的解释吗?提前致谢!

【问题讨论】:

  • 设备是横向模式还是纵向模式?
  • 我在横向上也有类似的问题,我的动画在 iOS 8 中似乎被反转了。反转的意思是我在“animatioWithDuration”中设置的值由于某种原因被设置为起始值的动画,它正在动画到它的初始值,而不是像 iOS6/7 上的相反。对此的帮助将不胜感激。
  • 我认为这与 UIKit 从 iOS 8 开始使用附加动画的方式有关,但我没有时间调查。

标签: ios objective-c animation ios8 cgaffinetransform


【解决方案1】:

CGAffineTransformIdentity 在 ios7 和 ios8 上的行为不同。这与自动布局和大小类有关。解决办法是去掉与ios7上的动画冲突的约束。

// solve the constraint-animation problem
if(NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) {
    // iOS7 remove constraints that conflict with animation
    if (self.centerYAlignment != nil) {
        self.view.removeConstraint(self.centerYAlignment) //is an IBOutlet 
    }
} else {
    // iOS8 constraint animations are fine
}

【讨论】:

  • 这里也一样,但在我的复杂动画开始在 iOS 7 中工作之前,我必须移除所有约束并关闭视图的自动布局。感谢您为我指明正确的方向。
【解决方案2】:

我认为原因只是 iOS8 错误,但我改用 CAAnimation,它在 iOS8 上按预期工作。

【讨论】:

  • 您能给我们举一个 CAAnimation 的例子吗?您可以指出苹果的任何错误吗?
【解决方案3】:

我在 iOS7 中也遇到了生涩的旋转变换问题。通过将我的旋转视图嵌套在容器内并将旋转视图居中来解决这个问题。

【讨论】:

  • 谢谢,这是唯一适合我的问题的解决方案。 TransformScale + Autolayout + iOS 7 = 视图位置损坏。
【解决方案4】:

我也遇到了同样的缩放问题。我想这可能与旋转相同。你能试试这个吗?

myView.transform = CGAffineTransformConcat(myView.transform , CGAffineTransformMakeRotate(1.57));
[UIView animateWithDuration:5 animations:^{
    myView.transform = CGAffineTransformTranslate(plane.transform, 100, 0);
}];

也许还需要使用 CGAffineTransformMakeTranslate 和 CGAffineTransformConcat,我不确定。

最糟糕的部分是:您必须在 iOS 版本上执行 if/else,因为这在 iOS 7 上看起来很奇怪。我希望 Apple 在 iOS 8 版本之前或之后修复此问题。

【讨论】:

    【解决方案5】:

    我同意 Pbk 的观点,即它与 io8 中的尺寸等级有关。 uiviewcontroller 需要根据设备方向使用 uitraitcollections 调整大小。否则,当您尝试旋转手机时,您会在手机处于横向模式时获得纵向模式的 uiviewcontroller。所以正确的步骤是旋转并覆盖 uitraitcollections

    【讨论】:

      【解决方案6】:

      这并不完全相关,但我正在努力解决 CGAffineTransformScale 在相当复杂的动画中根本无法在 iOS7 上工作的问题。原来我的问题是iOS7不能同时计算CGAffineTransformScaleCGAffineTransformRotate。在 iOS7 中,您进行的最后一个动画调用是唯一的动画调用,因此只有旋转发生。此错误已在 iOS8 中修复。

      我的解决方案是为 iOS7 简化我的动画,只在 iOS8 中打开花哨的东西:

      //Pre-animation setup:
      CGFloat radians = (M_PI/180) * (-15);  //Get a human-readable number in degrees
      self.badgeImage.alpha = 0;  //Start the image as invisible
      self.badgeImage.transform = CGAffineTransformScale(self.badgeImage.transform, 1.5, 1.5);  //Start the image as scaled bigger than normal
      if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {  //See below. We will not be rotating the image in iOS7
          self.badgeImage.transform = CGAffineTransformRotate(self.badgeImage.transform, radians);  //Rotate the image if iOS8
      }
      
      //Animation Pieces:
      //Fade in
      [UIView animateWithDuration: 0.5
                      delay:0
                      options:0
                      animations:^{
                          self.badgeImage.alpha = 1.0f; //Return image to opaque
                      }
                      completion:NULL];
      //Scale with bounce
      [UIView animateWithDuration: 1.1
                      delay:0
                      usingSpringWithDamping:0.3  //Not as good as Android's bounce interpolator, but I'll take it
                      initialSpringVelocity:-1.0f //A negative velocity here makes the animation appear more like gravity than spring
                      options:0
                      animations:^{
                          self.badgeImage.transform = CGAffineTransformScale(self.badgeImage.transform, 0.67, 0.67);  //Return image to its original size. These arguments are relative to its current scale.
                      }
                      completion:NULL];
      //Rotation
      if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) { //This second animation call negates the first one on iOS7, so remove it.
          [UIView animateWithDuration: 0.9
                          delay:0
                          options:UIViewAnimationOptionCurveEaseOut
                          animations:^{
                              self.badgeImage.transform = CGAffineTransformRotate(self.badgeImage.transform, (radians * -1)); //Rotate the image back to its original orientation if iOS8
                          }
                          completion:NULL];
      }
      

      当然,在 iOS7 中你仍然可以组合多种效果,如果你使用了名字容易混淆的 CGAffineTransformMakeScale() 函数。例如,在预动画设置中,您可以设置旋转和缩放,然后设置调用 CGAffineTransformMakeScale(1,1) 将图像重置为其原始指标(MakeScale 的参数是特定的,而不是相对的 - 更令人困惑!)。这并不总是可取的,例如我上面的示例,“反弹”动画也会反弹旋转。

      【讨论】:

        猜你喜欢
        • 2014-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-29
        • 2017-07-06
        • 1970-01-01
        • 1970-01-01
        • 2014-12-08
        相关资源
        最近更新 更多