【问题标题】:iOS Animate Rotation at an AngleiOS动画旋转角度
【发布时间】:2014-06-01 20:20:40
【问题描述】:

我正在尝试对视图进行 360 度旋转,但倾斜 45 度。

这样

我想不通。

到目前为止,我已经做到了

CABasicAnimation* animation = [CABasicAnimation
                               animationWithKeyPath:@"transform.rotation.y"];
animation.fromValue = @(0);
animation.toValue = @(2 * M_PI);
animation.duration = 1;

[self.layer addAnimation:animation forKey:@"rotation"];

它在它的 y 轴上旋转,但我想要的是让这个 Y 轴在它旋转之前倾斜 45 度。

【问题讨论】:

    标签: ios animation rotation transform cabasicanimation


    【解决方案1】:

    试试这个;

    CABasicAnimation* animation = [CABasicAnimation
                               animationWithKeyPath:@"transform"];
    
    CATransform3D rtfm = CATransform3DMakeRotation(2 * M_PI , 1.0f, 1.0f, 0.0f);
    rtfm.m34 = -0.0015f;
    
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue =   [NSValue valueWithCATransform3D:rtfm];
    animation.duration = 1;
    
    [self.layer addAnimation:animation forKey:@"rotation"];
    

    【讨论】:

      【解决方案2】:

      1-将锚点更改为右上边缘

      self.someView.layer.anchorPoint = CGPointMake(1.0, 0.5);
      

      2- 将视图旋转 45 或 35 度

      CGFloat radians = (M_PI/180) * (-35);
      self.someView.transform = CGAffineTransformRotate(self.someView.transform, radians);
      

      3- 按 X 轴旋转或翻转视图

      CGFloat radians = (M_PI/180) * (180);
      [UIView animateWithDuration:1 animations:^{
              self.someView.layer.transform = CATransform3DRotate(self.someView.layer.transform,radians , 1, 0.0, 0.0);
      
          } completion:^(BOOL finished) {
              if(finished) {
              }
          }];
      

      像魅力一样工作:)

      【讨论】:

        【解决方案3】:

        首先,您应该看到下面的链接,它解释了“框架”和“边界”之间的区别。 UIView frame, bounds and center

        现在,这是我的答案。

        /* add the 4 lines below */
        self.transform = CGAffineTransformMakeRotation(M_PI_4);
        self.layer.bounds = CGRectMake(0.0f, 0.0f, 60.0f, 200.0f);
        self.layer.position = CGPointMake(150.0f, 300.0f);
        
        CABasicAnimation* animation = [CABasicAnimation
                                       animationWithKeyPath:@"transform.rotation.y"];
        animation.fromValue = @(0);
        animation.toValue = @(2 * M_PI);
        animation.duration = 1;
        
        [self.layer addAnimation:animation forKey:@"rotation"];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-03
          • 1970-01-01
          • 1970-01-01
          • 2014-01-23
          • 1970-01-01
          • 2018-03-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多