在Swift中无限期旋转视图360度?

时间:2015-01-03 13:43:20

标签: ios swift

我想无限期地将图像视图旋转360度。

UIView.animate(withDuration: 2, delay: 0, options: [.repeat], animations: {
    self.view.transform = CGAffineTransform(rotationAngle: 6.28318530717959)
}, completion: nil)

我该怎么做?

11 个答案:

答案 0 :(得分:36)

Swift 2.x无限期地旋转UIView的方式,从早期的答案中编译:

public override void OnActivityCreated(Bundle savedInstanceState)
		{
			base.OnActivityCreated(savedInstanceState);
			/*
			var items = new string[] { "Rock","Country", "Dance" };


			lst = View.FindViewById<ListView> (Resource.Id.lstGenres);
	
			lst.Adapter = new ArrayAdapter<string>(Activity, Resource.Layout.textViewItems,Resource.Id.textviewItems, items);
			//lst = View.FindViewById<ListView> (Resource.Id.lst_genre);

			//lst.SetAdapter(new ArrayAdapter<String>(this.Activity, Resource.Layout.GenerFragment, items));


			lst.ItemClick+= delegate(object sender, AdapterView.ItemClickEventArgs e) {};*/

			var data = new ListItemCollection<ListItemValue> () {
				new ListItemValue ("Babbage"),
				new ListItemValue ("Boole"),
				new ListItemValue ("Berners-Lee"),
				new ListItemValue ("Atanasoff"),
				new ListItemValue ("Allen"),
				new ListItemValue ("Cormack"),
				new ListItemValue ("Cray"),
				new ListItemValue ("Dijkstra"),
				new ListItemValue ("Dix"),
				new ListItemValue ("Dewey"),
				new ListItemValue ("Erdos"),
			};

			var sortedContacts = data.GetSortedData ();
			var adapter = CreateAdapter (sortedContacts);
			ListAdapter = adapter;

		}

		SeparatedListAdapter CreateAdapter<T> (Dictionary<string, List<T>> sortedObjects)
			where T : IHasLabel, IComparable<T>
		{
			var adapter = new SeparatedListAdapter (this);
			foreach (var e in sortedObjects.OrderBy (de => de.Key)) {
				var label   = e.Key;
				var section = e.Value;
				adapter.AddSection (label, new ArrayAdapter<T> (this, Resource.Layout.lstGenres, section));
			}
			return adapter;
		}

更新Swift 3.x

// 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)
    }
}

答案 1 :(得分:24)

Swift 3.0

let imgViewRing = UIImageView(image: UIImage(named: "apple"))
imgViewRing.frame = CGRect(x: 0, y: 0, width: UIImage(named: "apple")!.size.width, height: UIImage(named: "apple")!.size.height)
imgViewRing.center = CGPoint(x: self.view.frame.size.width/2.0, y: self.view.frame.size.height/2.0)
rotateAnimation(imageView: imgViewRing)
self.view.addSubview(imgViewRing)

这是动画逻辑

func rotateAnimation(imageView:UIImageView,duration: CFTimeInterval = 2.0) {
        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotateAnimation.fromValue = 0.0
        rotateAnimation.toValue = CGFloat(.pi * 2.0)
        rotateAnimation.duration = duration
        rotateAnimation.repeatCount = .greatestFiniteMagnitude

        imageView.layer.add(rotateAnimation, forKey: nil)
    }

您可以查看此link

中的输出

答案 2 :(得分:6)

使用此扩展程序将UIImageView旋转360度。

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)
}
}

比旋转UIImageView简单地使用这个方法

self.YOUR_SUBVIEW.rotate360Degrees()

答案 3 :(得分:5)

我会将其添加到rotateImage()这样的函数中,并在完成代码中再次调用rotateImage()。我认为你应该使用M_PI(或swift等效物)作为旋转量。

答案 4 :(得分:4)

这个在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")

答案 5 :(得分:3)

针对Swift 3进行了更新: 我对UIView进行了扩展,并在其中包含了以下函数:

func rotate(fromValue: CGFloat, toValue: CGFloat, duration: CFTimeInterval = 1.0, completionDelegate: Any? = nil) {

let rotateAnimation = CABasicAnimation(keyPath: >"transform.rotation")
rotateAnimation.fromValue = fromValue
rotateAnimation.toValue = toValue
rotateAnimation.duration = duration

if let delegate: Any = completionDelegate {
rotateAnimation.delegate = delegate as? CAAnimationDelegate
}
self.layer.add(rotateAnimation, forKey: nil)

}

然后您可以通过(例如我在按钮上制作的UIView)来调用该函数:

monitorButton.rotate(fromValue: 0.0, toValue: CGFloat(M_PI * 2), completionDelegate: self)

希望这有帮助!

答案 6 :(得分:1)

尝试这个适合我的,我旋转图像一次

 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)
 })

答案 7 :(得分:1)

我认为您真正想要的是使用CADisplayLink。原因是,与使用完工模块相比,这将无限平滑,因为完工模块可能会引起轻微的打and并且不那么容易取消。请参阅以下解决方案:

var displayLink : CADisplayLink?
var targetView = UIView()

func beginRotation () {

    // Setup display link
    self.displayLink = CADisplayLink(target: self, selector: #selector(onFrameInterval(displayLink:)))
    self.displayLink?.preferredFramesPerSecond = 60
    self.displayLink?.add(to: .current, forMode: RunLoop.Mode.default)
}

func stopRotation () {

    // Invalidate display link
    self.displayLink?.invalidate()
    self.displayLink = nil
}

// Called everytime the display is refreshed
@objc func onFrameInterval (displayLink: CADisplayLink) {

    // Get frames per second
    let framesPerSecond = Double(displayLink.preferredFramesPerSecond)

    // Based on fps, calculate how much target view should spin each interval
    let rotationsPerSecond = Double(3)
    let anglePerSecond = rotationsPerSecond * (2 * Double.pi)
    let anglePerInterval = CGFloat(anglePerSecond / framesPerSecond)

    // Rotate target view to match the current angle of the interval
    self.targetView.layer.transform = CATransform3DRotate(self.targetView.layer.transform, anglePerInterval, 0, 0, 1)

}

答案 8 :(得分:1)

通过递归调用避免完成闭包!

晚了一点,但是使用UIView keyFrame动画和每个keyFrame的不同旋转阶段,以及设置动画曲线效果很好。这是一个UIView类函数-

do/while

看起来很粗糙,旋转角度很奇怪,但是正如op&others发现的,您不能仅仅告诉它旋转360度

答案 9 :(得分:1)

您可以使用此功能...只需为其提供视图和持续时间

它有两个动画,第一个将视图旋转180°,另一个将其旋转到360°,然后函数调用自身,使其可以无限地继续旋转动画

func infinite360Animation(targetView: UIView, duration: Double) {
    UIView.animate(withDuration: duration/2, delay: 0, options: .curveLinear) {
        targetView.transform = CGAffineTransform.identity.rotated(by: .pi )
    } completion: { (_) in
        UIView.animate(withDuration: duration/2, delay: 0, options: .curveLinear) {
            targetView.transform = CGAffineTransform.identity.rotated(by: .pi * 2)
        } completion: { (_) in
            self.infinite360Animation(targetView: targetView, duration: duration)
        }
    }
}

然后您可以像这样调用函数

infinite360Animatio(targetView: yourView, duration: 3)

答案 10 :(得分:0)

在tabBarController上,请务必设置您的代理并执行以下 didSelect 方法:

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)
        })
    }