【问题标题】:Rotate image -45 degrees旋转图像 -45 度
【发布时间】:2018-06-22 12:03:33
【问题描述】:

我正在构建关于 FloatingButton 的示例。

但是我在旋转图像时遇到了一些麻烦。我希望它像这个链接一样旋转

https://github.com/yoavlt/LiquidFloatingActionButton

但我的按钮是:

如你所见,当我第一次点击时,它运行良好 :D,它像 x 一样变换,但是当我再次点击时,我希望它像 + 一样回到原始状态,但它没有工作得很好。

这是我的代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var floatingButton: UIImageView!
    var floatingButtonIsActive = false

    override func viewDidLoad() {
        super.viewDidLoad()

        addShadowToFloatingButton()
        let gesture = UITapGestureRecognizer(target: self, action: #selector(floatingButtonTapped(_:)))
        floatingButton.addGestureRecognizer(gesture)
    }

    private func addShadowToFloatingButton() {
        floatingButton.layer.shadowColor = UIColor.grayColor().CGColor
        floatingButton.layer.shadowOffset = CGSize(width: -2, height: 2)
        floatingButton.layer.shadowOpacity = 1
        floatingButton.layer.shadowRadius = 1
    }

    func floatingButtonTapped(sender: UITapGestureRecognizer) {

        if floatingButtonIsActive == false {

            UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: .CurveEaseInOut, animations: {
                self.floatingButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4))
                }, completion: { _ in
                    self.floatingButtonIsActive = true
            })

        } else {
            UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: .CurveEaseInOut, animations: {
                self.floatingButton.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_4))
                }, completion: { _ in
                    self.floatingButtonIsActive = false
            })
        }
    }
}

【问题讨论】:

  • 您可以再次旋转到 45 度。它会产生同样的效果。 self.floatingButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4))
  • 不,它不会像你说的那样工作。

标签: swift


【解决方案1】:

完成后将转换设置为CGAffineTransformIdentity。那是它最初的变换。当你设置变换时,你告诉它旋转到的绝对角度,不管它当前处于什么角度。

func floatingButtonTapped(sender: UITapGestureRecognizer) {

    if floatingButtonIsActive == false {

        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: .CurveEaseInOut, animations: {
            self.floatingButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4))
            }, completion: { _ in
                self.floatingButtonIsActive = true
        })

    } else {
        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: .CurveEaseInOut, animations: {
            self.floatingButton.transform = CGAffineTransformIdentity
            }, completion: { _ in
                self.floatingButtonIsActive = false
        })
    }
}

【讨论】:

    【解决方案2】:

    Swift 4

    180" 旋转:

    self.yourLayerName.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
    

    返回默认位置:

    self.yourLayerName.transform = .identity
    

    要改变旋转角度,您可以乘或除“pi”数。

    对于 45" 旋转,您需要在下面实现:

    self.yourLayerName.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/4))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      相关资源
      最近更新 更多