swift – 旋转图像-45度

前端之家收集整理的这篇文章主要介绍了swift – 旋转图像-45度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建关于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,animations: {
                self.floatingButton.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_4))
                },completion: { _ in
                    self.floatingButtonIsActive = false
            })
        }
    }
}
完成后将转换设置为CGAffineTransformIdentity.这是它最初的转变.设置变换时,无论当前处于什么角度,都会告诉它旋转到的绝对角度.
func floatingButtonTapped(sender: UITapGestureRecognizer) {

    if floatingButtonIsActive == false {

        UIView.animateWithDuration(0.5,animations: {
            self.floatingButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4))
            },completion: { _ in
                self.floatingButtonIsActive = true
        })

    } else {
        UIView.animateWithDuration(0.5,animations: {
            self.floatingButton.transform = CGAffineTransformIdentity
            },completion: { _ in
                self.floatingButtonIsActive = false
        })
    }
}
原文链接:https://www.f2er.com/swift/443719.html

猜你在找的Swift相关文章