Rotate a view for 360 degrees indefinitely in Swift?

后端 未结 10 1209
闹比i
闹比i 2020-12-05 13:06

I want to rotate an image view for 360 degrees indefinitely.

UIView.animate(withDuration: 2, delay: 0, options: [.repeat], animations: {
    self.view.transf         


        
10条回答
  •  星月不相逢
    2020-12-05 13:45

    On your tabBarController, make sure to set your delegate and do the following didSelect method below:

    func tabBarController(_ tabBarController: UITabBarController,
                              didSelect viewController: UIViewController) {
            if let selectedItem:UITabBarItem = tabBarController.tabBar.selectedItem {
                animated(tabBar: tabBarController.tabBar, selectedItem: selectedItem)
            }
        }
    
    
    
    
    fileprivate func animated(tabBar: UITabBar, selectedItem:UITabBarItem){
            if let view:UIView = selectedItem.value(forKey: "view") as? UIView {
                if let currentImageView = view.subviews.first as? UIImageView {
                    rotate(imageView: currentImageView, completion: { (completed) in
                        self.restore(imageView: currentImageView, completion: nil)
                    })
                }
            }
        }
    
        fileprivate func rotate(imageView:UIImageView, completion:((Bool) ->Void)?){
            UIView.animate(withDuration: animationSpeed, delay: 0.0, options: .curveLinear, animations: {
                imageView.transform = CGAffineTransform.init(rotationAngle: CGFloat.pi)
            }, completion: {
                (value: Bool) in
                completion?(value)
            })
        }
    
        fileprivate func restore(imageView:UIImageView, completion:((Bool) ->Void)?){
            UIView.animate(withDuration: animationSpeed, delay: 0.0, options: .curveLinear, animations: {
                imageView.transform = CGAffineTransform.identity
            }, completion: {
                (value: Bool) in
                completion?(value)
            })
        }
    

提交回复
热议问题