我正在建设示例约FloatingButton。旋转图像-45度
但我有一些旋转图像的麻烦。我希望它旋转这样的链接
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)) –
不,它不会像你说的那样工作。 – Khuong