1

I am developing iOS application in Swift and I want to rotate an image in 360 degrees with motion. Animation should be stop for 2 seconds once it completes each round.

I need 2 seconds delay after completion of every rotation. How can I set delay for it? I have done image rotation algorithm.

 func rotate360Degrees(_ duration: CFTimeInterval = 1.0, completionDelegate: CAAnimationDelegate? = nil) {
    let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
    rotateAnimation.fromValue = 0.0
    rotateAnimation.toValue = CGFloat(M_PI * 2.0)
    rotateAnimation.duration = duration

    if let delegate: CAAnimationDelegate = completionDelegate {
        rotateAnimation.delegate = delegate

    }
    rotateAnimation.repeatCount = Float.greatestFiniteMagnitude;

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

For delay I have tried this:

    let deadlineTime = DispatchTime.now() + .seconds(2)
    DispatchQueue.main.asyncAfter(deadline: deadlineTime, execute: {
        self.imgView.rotate360Degrees()
    })

it is not working.

halfer
  • 19,824
  • 17
  • 99
  • 186
user12346
  • 107
  • 1
  • 3
  • 11
  • What's not working? You're not calling `rotate360Degrees`, or that method isn't causing a rotation? Or rotation works, but there's no pause between rotations? (my best guess: the final one, and the correct answer involves `CAKeyframeAnimation` since what you're doing is expecting a delay in starting an animation somehow to put itself permanently inside the animation) – Tommy Sep 25 '17 at 21:29
  • rotation is working but I need 2 seconds delay at every rotation that part is not working. – user12346 Sep 26 '17 at 12:11

1 Answers1

0

Its very simple. Just call your function inside a body and once the rotation is finished call the same function after 2 seconds delay. And when ever you want to stop just stop remove it. Example

unowned let unownedSelf = self

let deadlineTime = DispatchTime.now() + .seconds(2)
DispatchQueue.main.asyncAfter(deadline: deadlineTime, execute: { 
 unownedSelf.functionToCall()
 })
cole
  • 3,147
  • 3
  • 15
  • 28
  • I have tried like this let deadlineTime = DispatchTime.now() + .seconds(2) DispatchQueue.main.asyncAfter(deadline: deadlineTime, execute: { var affineTransform = CGAffineTransform(rotationAngle: 0.0) affineTransform = affineTransform.rotated(by: CGFloat(M_PI)) let circlePath = UIBezierPath(arcCenter: CGPoint(x: 100 - (100/2),y: 100 - (100/2)), radius: CGFloat(100), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true) orbit.rotationMode = kCAAnimationRotateAuto }) but it s not working – user12346 Sep 25 '17 at 15:47
  • The problem is that you need to call the function after you complete the rotation for example it takes 30 sec to finish the rotation than call it after 32 seconds. Make sure when you call this function first time and are you giving it enough time to pause before next cycle starts. Its more management thing now.. you are almost there. Otherwise, let me know I can give update my code for more understanding. – cole Sep 26 '17 at 08:38
  • I have done it but when I called that function again in piece of code which you provided to me. animation is not looking smooth.it is messy. – user12346 Sep 26 '17 at 13:35
  • I was calling that function on touched end method but i removed it and it is working fine now.Thank you so much for you help :) – user12346 Sep 26 '17 at 13:42
  • if you are happy with my answer, please accept it !! – cole Sep 26 '17 at 13:44
  • hi I wanna do continuous touch input till 10 seconds.how can i detect it? – user12346 Sep 26 '17 at 16:24
  • I would advise to give clear detail what you want, how you want and what you have done so far to do it ? Otherwise it's no help to you as no one know what you want to do ? Here is a link to the question you asked. Hope it helps https://stackoverflow.com/questions/26829718/how-to-recognize-continuous-touch-in-swift – cole Sep 27 '17 at 10:09
  • I have done to provide continuous touch input to the app.If i give continuous touch input till 10 seconds then it should be prompt to the screen that's all I want to do – user12346 Sep 27 '17 at 12:03