2016-05-01 136 views
4

我正在建设示例约FloatingButton旋转图像-45度

但我有一些旋转图像的麻烦。我希望它旋转这样的链接

https://github.com/yoavlt/LiquidFloatingActionButton

但我的按钮是:

enter image description here

正如你可以看到,当我点击了第一次,它运行良好: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 
      }) 
     } 
    } 
} 
+0

您可以再次旋转到45度。它会产生同样的效果。 self.floatingButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4)) –

+0

不,它不会像你说的那样工作。 – Khuong

回答

9

设置完成后转换到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 
     }) 
    } 
} 
0

夫特4

180" 旋转:

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

用于返回到默认位置:

self.yourLayerName.transform = .identity 

为了改变旋转角度可以乘以或“pi”号。

45" 旋转:

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