繁体   English   中英

快捷栏按钮项360旋转?

[英]Swift Bar Button Item 360 Spin?

我之前在这里看到过类似的问题,但它们仅涉及简单的按钮,而不涉及条形按钮项目。 请记住,按钮项和常规按钮的属性不同,因此在这种情况下,其他帖子上的答案将无效。

有没有一种方法可以让我的刷新栏按钮项一次旋转360度(以表明发生了动作)?

为了给UIBarButtonItem设置动画,我们需要为其分配一个customView并为其设置动画。

我在下面所做的是创建一个普通的UIButton ,并将其图像分配给名为“ refresh”的刷新图标。 接下来,我将UIBarButtonItemcustomView设置为新按钮。 现在, barButton具有customView ,我可以调用我的rotateBarButton()方法来执行动画代码。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var barButton: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()

        let button = UIButton(frame: CGRectMake(0, 0, 40, 40)) // Create new button & set its frame
        button.setImage(UIImage(named: "refresh"), forState: .Normal) // Assign an image 
        barButton.customView = button // Set as barButton's customView
    }

    func rotateBarButton() {
        // Gets you half way there //
        UIView.animateWithDuration(0.05, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
            self.barButton.customView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
            }, completion: nil)
        // Rotates all the way around //
        UIView.animateWithDuration(0.5, delay: 0.5, options: UIViewAnimationOptions.CurveEaseIn, animations: {
            self.barButton.customView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 2))
            }, completion: nil)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM