加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 旋转图像-45度

发布时间:2020-12-14 02:23:21 所属栏目:百科 来源:网络整理
导读:我正在构建关于FloatingButton的示例. 但是旋转图像有些麻烦.我希望它像这个链接一样旋转 https://github.com/yoavlt/LiquidFloatingActionButton 但我的按钮是: 正如你所看到的,当我第一次点击时,它运行良好:D,它变换为x,但是当我再次点击时,我希望它恢复
我正在构建关于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
        })
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读