Swift按钮单击完全重启动画

时间:2015-12-26 19:27:18

标签: ios swift core-animation

所以我想做的就是重新启动动画,当它完成并按下“重启”按钮时。

这就是我现在所拥有的。 它就是这样开始的:

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var fillView: UIView!

    @IBAction func startAnimation(sender: AnyObject) {
        self.animationBackground(fillView, animationTime: Float(15))
    }

    @IBAction func restartAnimation(sender: AnyObject) {
        let layerRectangle = fillView.layer
       restartLayer(layerRectangle)
    }

    func animationBackground(view:UIView,animationTime:Float){
        UIView.animateWithDuration(NSTimeInterval(animationTime), delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: {
            view.transform = CGAffineTransformMakeTranslation(0.0,  self.screenSize.height-67)
        },completion:nil)
    }

    func restartLayer(layer:CALayer){
        layer.beginTime = 0.0
        layer.speed = 0.0        
    }
}

重启功能仅在播放动画时有效。但是当它完成时不会从头开始重新启动动画。有关如何在按钮单击时重新启动动画的任何提示,但仅限于已经完成的时候?

3 个答案:

答案 0 :(得分:4)

我已经在上面写过..顺便说一下:

> 2015-12-26 23:06:32,876 | WARN  | stackref=151df5b0a0f
> java.lang.NullPointerException    at
> org.apache.activemq.apollo.broker.QueueEntry$Swapped.swapped_in(QueueEntry.scala:809)
>   at
> org.apache.activemq.apollo.broker.QueueEntry$Swapped$$anonfun$swap_in$1$$anonfun$apply$2.apply$mcV$sp(QueueEntry.scala:764)
>   at
> org.apache.activemq.apollo.util.DeferringDispatched$$anon$1.run(Dispatched.scala:38)
>   at
> org.apache.activemq.apollo.util.DeferringDispatched$$anonfun$dispatch_queue_task_source$1$$anonfun$apply$mcV$sp$1.apply(Dispatched.scala:45)
>   at
> org.apache.activemq.apollo.util.DeferringDispatched$$anonfun$dispatch_queue_task_source$1$$anonfun$apply$mcV$sp$1.apply(Dispatched.scala:45)
>   at scala.collection.immutable.List.foreach(List.scala:309)  at
> scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
>   at scala.collection.mutable.ListBuffer.foreach(ListBuffer.scala:45)
>   at
> org.apache.activemq.apollo.util.DeferringDispatched$$anonfun$dispatch_queue_task_source$1.apply$mcV$sp(Dispatched.scala:45)
>   at
> org.fusesource.hawtdispatch.package$$anon$4.run(hawtdispatch.scala:357)
>   at
> org.fusesource.hawtdispatch.internal.HawtCustomDispatchSource$1.run(HawtCustomDispatchSource.java:127)
>   at
> org.fusesource.hawtdispatch.internal.SerialDispatchQueue.run(SerialDispatchQueue.java:100)
>   at
> org.fusesource.hawtdispatch.internal.pool.SimpleThread.run(SimpleThread.java:77)

答案 1 :(得分:0)

您已将位置和框架(位置)进行了转换。因此,一旦动画完成,您必须将帧恢复到其原始位置。您可以在动画的完成块中恢复帧。

答案 2 :(得分:0)

这里的例子是:

 UIView.animateWithDuration(1, animations: { () -> Void in
     self.fillView.transform = CGAffineTransformMakeTranslation(0, 40)
      }) { (end) -> Void in
             self.fillView.transform = CGAffineTransformIdentity
 }

您可以在开始动画之前将变换设置为CGAffineTransformIdentity