代码之家  ›  专栏  ›  技术社区  ›  Kristina

把一个炉旋转90度?

  •  27
  • Kristina  · 技术社区  · 15 年前

    如何旋转 卡莱尔 90度?我需要旋转所有东西,包括子图层和坐标系。

    5 回复  |  直到 8 年前
        1
  •  13
  •   Rab    15 年前

    如果我正在制作动画,我会在我的应用程序中使用类似的功能:

    - (NSObject *) defineZRotation {
        // Define rotation on z axis
        float degreesVariance = 90;
        // object will always take shortest path, so that
        // a rotation of less than 180 deg will move clockwise, and more than will move counterclockwise
        float radiansToRotate = DegreesToRadians( degreesVariance );
        CATransform3D zRotation;
        zRotation = CATransform3DMakeRotation(radiansToRotate, 0, 0, 1.0);  
        // create an animation to hold "zRotation" transform
        CABasicAnimation *animateZRotation;
        animateZRotation = [CABasicAnimation animationWithKeyPath:@"transform"];
        // Assign "zRotation" to animation
        animateZRotation.toValue = [NSValue valueWithCATransform3D:zRotation];
        // Duration, repeat count, etc
        animateZRotation.duration = 1.5;//change this depending on your animation needs
        // Here set cumulative, repeatCount, kCAFillMode, and others found in
        // the CABasicAnimation Class Reference.
        return animateZRotation;
    }
    

    当然,你可以在任何地方使用它,如果它不适合你的需要,就不必从方法中返回它。

        2
  •  45
  •   Abhi Beckert    8 年前

    Obj-C:

    theLayer.transform = CATransform3DMakeRotation(90.0 / 180.0 * M_PI, 0.0, 0.0, 1.0);
    

    Swift:

    theLayer.transform = CATransform3DMakeRotation(90.0 / 180.0 * .pi, 0.0, 0.0, 1.0)
    

    也就是说,变换图层使其旋转90度(_/2弧度),100%的旋转围绕Z轴进行。

        3
  •  8
  •   fabian789    11 年前

    基本上是这样的:

    CGAffineTransform rotateTransform = CGAffineTransformMakeRotation(M_PI / 2.0); [myCALayer setAffineTransform:rotateTransform];

    编辑:它将根据平台(iOS或Mac OS)顺时针或逆时针旋转。

        4
  •  3
  •   Sam Soffes Jolly Roger    12 年前

    向右旋转90度:

    myView.transform = CGAffineTransformMakeRotation(M_PI_2);
    
        5
  •  1
  •   Arun    13 年前

    RAB演示了如何使用 CAAnimation 对象。其实比这简单:

    [myView animateWithDuration: 0.25 
      animations:
      ^{
         myView.transform = CGAffineTransformMakeRotation(M_PI/2);
       }
    ];
    

    (将变换线从 克里斯的回答 -因为他已经提供了完美的代码,所以懒得重写它。)

    克里斯 代码将在不使用动画的情况下旋转视图。我上面的代码将对动画做同样的事情。

    默认情况下,动画使用“缓入”、“缓出”计时。您可以使用稍微复杂一点的 animateWithDuration 调用(使用) animateWithDuration:delay:options:animations:completion: 相反,并在选项参数中传递所需的时间。)