【问题标题】:Rotating UIButton for 180 degrees and rotating it back将 UIButton 旋转 180 度并将其旋转回来
【发布时间】:2016-11-02 20:58:29
【问题描述】:

我有UIButton,我希望向一个方向旋转 180 度,然后也返回 180 度。我用CABasicAnimation 做动画已经有一段时间了,但这次我想用变换来做。这是我写的代码:

- (IBAction)blurAction:(id)sender {
    if (self.blurActive) {
        [UIView animateWithDuration:0.1 animations:^{
            self.blurView.alpha = 0;
            self.blurButton.transform = CGAffineTransformMakeRotation(M_PI);
        } completion:^(BOOL finished) {
            self.blurActive = NO;
        }];
    }
    else {
        [UIView animateWithDuration:0.1 animations:^{
            self.blurView.alpha = 1;
            self.blurButton.transform = CGAffineTransformMakeRotation(-M_PI);
        } completion:^(BOOL finished) {
            self.blurActive = YES;
        }];
    }
}

它第一次工作,但第二次按下按钮,没有任何反应。有人可以解释我在这里做错了什么吗?

【问题讨论】:

    标签: ios objective-c uibutton rotation


    【解决方案1】:

    M_PI 和-M_PI 会有相同的视觉效果

    回到原来的位置应该是

    self.blurButton.transform = CGAffineTransformMakeRotation(0);
    

    --编辑--

    要动画逆时针旋转,你可以使用-2*M_PI

    self.blurButton.transform = CGAffineTransformMakeRotation(-2*M_PI);
    

    【讨论】:

    • 不是真的,如果我输入0,它会在同一方向再旋转180度,这将是原始位置,但它就像顺时针旋转360度的按钮。另一方面,我想顺时针旋转 180 度,然后逆时针旋转 180 度
    • 逆时针旋转可以使用-2*M_PI
    • 不确定,只是在这里测试,它工作正常
    • 我对最后一个不好,我没有提交代码。将此答案放在评论中,以便我接受。
    【解决方案2】:

    要“取消旋转”视图,请将变换设置为常数 CGAffineTransformIdentity ,这是一个零变换。这就是使用变换的美妙之处,您无需计算返回的路径,只需撤消您已经完成的操作。

    【讨论】:

    • UIButton 还是同方向旋转,所以就像同方向旋转了 2 180 度
    【解决方案3】:

    实现这种效果最简单的方法就是旋转一个小于 180° 的微小量:

    - (IBAction)toggle:(UIButton *)sender {
        [UIView animateWithDuration:0.2 animations:^{
            if (CGAffineTransformEqualToTransform(sender.transform, CGAffineTransformIdentity)) {
                sender.transform = CGAffineTransformMakeRotation(M_PI * 0.999);
            } else {
                sender.transform = CGAffineTransformIdentity;
            }
        }];
    }
    

    结果:

    【讨论】:

    • 我喜欢你的方法。这是 Swift 3 代码: UIView.animate(withDuration: 0.5, animations: { if self.transform == CGAffineTransform.identity { self.transform = CGAffineTransform(rotationAngle: (180.0 * .pi) / 180.0) } else { self.变换 = CGAffineTransform.identity } })
    【解决方案4】:

    这是@rob mayoff 解决方案的Swift3 代码。

       @IBAction func fooButton(_ sender: Any) {
            UIView.animate(withDuration:0.1, animations: { () -> Void in
                if sender.transform == .identity {
                    sender.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI * 0.999))
                } else {
                    sender.transform = .identity
                }
            })
        }
    

    Swift4

    M_PI 已弃用并替换为 Double.pi

    @IBAction func fooButton(_ sender: Any) {
        UIView.animate(withDuration:0.1, animations: { () -> Void in
            if sender.transform == .identity {
                sender.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi * 0.999))
            } else {
                sender.transform = .identity
            }
        })
     }
    

    【讨论】:

      【解决方案5】:

      您实际上无法通过转换来实现这一点,因为转换仅包含有关最终状态的信息。他们没有告诉我们应该使用哪个方向来制作例如旋转。

      实现 180 度前后旋转行为的最佳选择是使用CABasicAnimation。

      示例如下:

      func rotateImage(forward: Bool) {
          let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
          rotationAnimation.fromValue = forward ? 0.0 : CGFloat.pi
          rotationAnimation.toValue = forward ? CGFloat.pi : 0.0
          rotationAnimation.fillMode = kCAFillModeForwards
          rotationAnimation.isRemovedOnCompletion = false
          rotationAnimation.duration = 0.5
      
          handleImageView.layer.add(rotationAnimation, forKey: "rotationAnimation")
      }
      

      【讨论】:

        【解决方案6】:

        将 UIButton 旋转 180º

        UIButton.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
        

        将 UIButton 旋转 0º 或再次返回

        UIButton.transform = CGAffineTransform(rotationAngle: 0)
        

        动画示例

        if cardViewModel.getBottomMenuVisibility() {
                    [UIView .transition(
                        with: bottomMenuView,
                        duration: 0.3,
                        options: .transitionCrossDissolve,
                        animations: {
                            // Turn UIButton upside down
                            self.bottomMenu.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
                    },
                        completion: nil)]
            }
            else {
                [UIView .transition(
                    with: bottomMenuView,
                    duration: 0.3,
                    options: .transitionCrossDissolve,
                    animations: {
                        // Turn UIButton back to normal
                        self.bottomMenu.transform = CGAffineTransform(rotationAngle: 0)
                },
                    completion: nil)]
            }
        

        你可以查看我的代码这个例子: https://github.com/issuran/Scrum-Geek-Poker/blob/0d032a4b4edcf75cebae1524131f4fe6be3a5edf/Scrum%20Geek%20Poker/Features/CardCollection/View/MainScreenViewController.swift

        【讨论】:

          【解决方案7】:

          Swift 4+

          @IBAction private func maximizeButtonAction(_ sender: UIButton) {
                  UIView.animate(withDuration: 0.2, animations: {
                      sender.transform = sender.transform == .identity ? CGAffineTransform(rotationAngle: CGFloat(Double.pi * 0.999)) : .identity
                  })
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-09-20
            • 1970-01-01
            • 2011-11-19
            • 2019-11-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多