Rotate a view for 360 degrees indefinitely in Swift?

后端 未结 10 1163
闹比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:33

    Use this extension to rotate UIImageView 360 degrees.

    extension UIView {
    func rotate360Degrees(duration: CFTimeInterval = 1.0, completionDelegate: AnyObject? = nil) {
        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotateAnimation.fromValue = 0.0
        rotateAnimation.toValue = CGFloat(M_PI)
        rotateAnimation.duration = duration
    
        if let delegate: CAAnimationDelegate = completionDelegate as! CAAnimationDelegate? {
            rotateAnimation.delegate = delegate
        }
        self.layer.addAnimation(rotateAnimation, forKey: nil)
    }
    }
    

    Than to rotate UIImageView simply use this method

    self.YOUR_SUBVIEW.rotate360Degrees()
    
    0 讨论(0)
  • 2020-12-05 13:36

    I would stick it in a function like rotateImage() and in the completion code just call rotateImage() again. I think you should use M_PI (or the swift equivalent) for the rotation amount, though.

    0 讨论(0)
  • 2020-12-05 13:39

    This one work for me in Swift 2.2:

    let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
    rotationAnimation.fromValue = 0.0
    rotationAnimation.toValue = 360 * CGFloat(M_PI/180)
    let innerAnimationDuration : CGFloat = 1.0
    rotationAnimation.duration = Double(innerAnimationDuration)
    rotationAnimation.repeatCount = HUGE
    self.imageView.addAnimation(rotationAnimation, forKey: "rotateInner")
    
    0 讨论(0)
  • 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)
            })
        }
    
    0 讨论(0)
  • 2020-12-05 13:46

    Try this one it works for me, i am rotating image for once

     UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveLinear, animations: {
    
           self.imgViewReload.transform = CGAffineTransformRotate(self.imgViewReload.transform, CGFloat(M_PI))
    
     }, completion: {
        (value: Bool) in
    
              UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveLinear, animations: {
              self.imgViewReload.transform = CGAffineTransformIdentity
    
             }, completion: nil)
     })
    
    0 讨论(0)
  • 2020-12-05 13:53

    Swift 2.x way to rotate UIView indefinitely, compiled from earlier answers:

    // Rotate <targetView> indefinitely
    private func rotateView(targetView: UIView, duration: Double = 1.0) {
        UIView.animateWithDuration(duration, delay: 0.0, options: .CurveLinear, animations: {
            targetView.transform = CGAffineTransformRotate(targetView.transform, CGFloat(M_PI))
        }) { finished in
            self.rotateView(targetView, duration: duration)
        }
    }
    

    UPDATE Swift 3.x

    // Rotate <targetView> indefinitely
    private func rotateView(targetView: UIView, duration: Double = 1.0) {
        UIView.animate(withDuration: duration, delay: 0.0, options: .curveLinear, animations: {
            targetView.transform = targetView.transform.rotated(by: CGFloat(M_PI))
        }) { finished in
            self.rotateView(targetView: targetView, duration: duration)
        }
    }
    
    0 讨论(0)
提交回复
热议问题