Play animation wait 2 seconds and repeat - ios

I'm using UIView.animate to animate a simple UIView.
Starting conditions: width = height = 30, color = blue (I set this in the Storyboard)
I want to increase the size to w=50, h=50 and make it white, while it has an appropriate cornerRadius. Then I reset the view and do the same animation again.
Here is my code:
class ViewController: UIViewController {
#IBOutlet weak var swipeRightView: UIView!
#IBOutlet weak var doubleTab: UIView!
override func viewDidLoad() {
super.viewDidLoad()
for _ in 0...2 {
startAnimation()
sleep(5)
self.doubleTab.layer.removeAllAnimations()
startAnimation()
}
}
func startAnimation() {
self.doubleTab.layer.cornerRadius = 15
UIView.animate(withDuration: 1.5, delay: 0, options: [.curveLinear], animations: {
self.doubleTab.backgroundColor = UIColor.white
self.doubleTab.frame.size.width += 20
self.doubleTab.frame.size.height += 20
self.doubleTab.layer.cornerRadius = self.doubleTab.frame.size.width / 2
}) { (_) in
self.doubleTab.backgroundColor = UIColor(displayP3Red: 0.34, green: 0.61, blue: 0.88, alpha: 1)
self.doubleTab.frame.size = CGSize(width: 30, height: 30)
self.doubleTab.layer.cornerRadius = self.doubleTab.frame.size.width / 2
UIView.animate(withDuration: 1.5, delay: 0, options: [.curveLinear], animations: {
self.doubleTab.backgroundColor = UIColor.white
self.doubleTab.frame.size.width += 20
self.doubleTab.frame.size.height += 20
self.doubleTab.layer.cornerRadius = self.doubleTab.frame.size.width / 2
}, completion: nil)
}
}
}
This works fine but i want to repeat the whole animation. When the whole animation is finished I want to wait 2 seconds and then repeat it, then wait 2 seconds and repeat and so on. As you can see in viewDidLoad I tried to repeat the animation with a for-loop but this has no affect or a wrong behavior. A repeat - while has also no effect. So, how can i repeat this animation?
Any help is highly appreciated.

In this case, you could call the startAnimation function recursively, which means call it again after it's completed, which will call itself again after it completed and so on.
So you could basically add , completion: { _ in self.startAnimation() } in the last block where you've currently set completion: nil.
If you wanted to add a delay, you could add the delay in the function as parameter and make it 0 by default for the first animation cycle.
The full code would then be:
func startAnimation(delay: TimeInterval = 0) {
self.doubleTab.layer.cornerRadius = 15
UIView.animate(withDuration: 1.5, delay: delay, options: [.curveLinear], animations: {
self.doubleTab.backgroundColor = UIColor.white
self.doubleTab.frame.size.width += 20
self.doubleTab.frame.size.height += 20
self.doubleTab.layer.cornerRadius = self.doubleTab.frame.size.width / 2
}) { (_) in
self.doubleTab.backgroundColor = UIColor(displayP3Red: 0.34, green: 0.61, blue: 0.88, alpha: 1)
self.doubleTab.frame.size = CGSize(width: 30, height: 30)
self.doubleTab.layer.cornerRadius = self.doubleTab.frame.size.width / 2
UIView.animate(withDuration: 1.5, delay: 0, options: [.curveLinear], animations: {
self.doubleTab.backgroundColor = UIColor.white
self.doubleTab.frame.size.width += 20
self.doubleTab.frame.size.height += 20
self.doubleTab.layer.cornerRadius = self.doubleTab.frame.size.width / 2
}, completion: { _ in
self.startAnimation(delay: 2)
})
}
}

Related

Swift UIKit - Sequential animation function

I have a func UIView.animate (2 imageViews, up/down, pulsating 4 and 2 times sequentially), which is forking fine.
Problem is that i need to run this func sequentially, all animations executed -> next lap of execution -> N lap of execution for my needs (it can be 5 or it can be 25 etc).
How can i do it?
func upDown() {
runButton.isEnabled = false
UIView.animate(
withDuration: 0.5,
delay: 0,
options: [.autoreverse, .repeat]) {
UIView.modifyAnimations(withRepeatCount: 4, autoreverses: true) {
self.arrowDownImageView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.arrowDownImageView.tintColor = .green
self.downLabel.backgroundColor = .green
}
} completion: { _ in
self.arrowDownImageView.transform = .identity
self.downLabel.transform = .identity
self.downLabel.backgroundColor = .black
self.arrowDownImageView.tintColor = .black
UIView.animate(
withDuration: 0.5,
delay: 0,
options: [.autoreverse, .repeat]) {
UIView.modifyAnimations(withRepeatCount: 2, autoreverses: true) {
self.arrowUpImageView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.arrowUpImageView.tintColor = .blue
self.upLabel.backgroundColor = .blue
}
} completion: { _ in
self.arrowUpImageView.transform = .identity
self.upLabel.transform = .identity
self.upLabel.backgroundColor = .black
self.arrowUpImageView.tintColor = .black
self.runButton.isEnabled = true
}
}
}
I tried to call my func upDown() through other UIView.animate (+ .modifyAnimations) func (executed 1 time only), call it with for-in-loop (also wrong, like i clicked X-times on a button). Should i rebuild it with some other method, not UIView.animate? In this case which one? Pods are forbidden in my case. I'm planning (need) to place my upDown() inside a new func to run it sequentially. Any other ideas? Any other methods? Thanks!
It sounds like you want your upDown function called multiple times. You can add a count parameter to upDown. Then in the final completion handler you can call upDown with the next smaller number. When the count gets to zero you can exit.
Here's your upDown method with only two lines added plus the new default parameter. Added the guard at the beginning and the recursive call at the very end in the last completion block.
func upDown(count: Int = 1) {
guard count > 0 else { return } // stop when we get to zero
runButton.isEnabled = false
UIView.animate(
withDuration: 0.5,
delay: 0,
options: [.autoreverse, .repeat]) {
UIView.modifyAnimations(withRepeatCount: 4, autoreverses: true) {
self.arrowDownImageView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.arrowDownImageView.tintColor = .green
self.downLabel.backgroundColor = .green
}
} completion: { _ in
self.arrowDownImageView.transform = .identity
self.downLabel.transform = .identity
self.downLabel.backgroundColor = .black
self.arrowDownImageView.tintColor = .black
UIView.animate(
withDuration: 0.5,
delay: 0,
options: [.autoreverse, .repeat]) {
UIView.modifyAnimations(withRepeatCount: 2, autoreverses: true) {
self.arrowUpImageView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.arrowUpImageView.tintColor = .blue
self.upLabel.backgroundColor = .blue
}
} completion: { _ in
self.arrowUpImageView.transform = .identity
self.upLabel.transform = .identity
self.upLabel.backgroundColor = .black
self.arrowUpImageView.tintColor = .black
self.runButton.isEnabled = true
upDown(count - 1) // Run it again
}
}
}
With those few changes you can call upDown() if you only want it to run once or you can call it as upDown(count: 5) if you want it to run 5 times (or whatever number you pass in).

UIView keyframe animation timing not as expected

I have some animations (testable in a playground):
import UIKit
import XCPlayground
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution
let view = UIView()
view.frame = .init(x: 0, y: 0, width: 150, height: 150)
view.backgroundColor = .orange
let hand = UIView()
view.addSubview(hand)
hand.frame = .init(x: 0, y: 0, width: 10, height: 10)
hand.center = view.center
hand.backgroundColor = .green
PlaygroundPage.current.liveView = view
let fadeDuration: TimeInterval = 0.4
let translationDuration: TimeInterval = 1
let resetDuration: TimeInterval = 0.25
let duration: TimeInterval =
7 * fadeDuration +
4 * translationDuration +
3 * resetDuration
var currentTime: TimeInterval = 0.0
func addKey(_ animations: #escaping () -> Void, animationTime: TimeInterval) {
UIView.addKeyframe(withRelativeStartTime: currentTime / duration, relativeDuration: animationTime / duration, animations: animations)
currentTime += animationTime
}
func fadeIn() {
addKey({
hand.alpha = 1
}, animationTime: fadeDuration)
}
func fadeOut() {
addKey({
hand.alpha = 0
}, animationTime: fadeDuration)
}
func translate(_ direction: (CGFloat) -> CGFloat) {
let x = direction(50)
addKey({
hand.transform = .init(translationX: x, y: 0)
}, animationTime: translationDuration)
}
func reset() {
addKey({
hand.transform = .identity
}, animationTime: 0)
currentTime += resetDuration
}
UIView.animateKeyframes(withDuration: duration, delay: 0, options: .calculationModeLinear) {
translate(-)
fadeOut()
reset()
fadeIn()
translate(-)
fadeOut()
reset()
fadeIn()
translate(+)
fadeOut()
reset()
fadeIn()
translate(+)
fadeOut()
} completion: { _ in
PlaygroundPage.current.finishExecution()
}
I expect every single translate() animation to run at the same speed, but for some reason the first and last ones are especially slow, despite using .calculationModeLinear
How to make it so that translationDuration / duration is constant time ?
calculationModeLinear is different from curveLinear. It sounds like you want the latter. You are using curveEaseInOut by default so of course the movement slows near the overall start and finish.
Some hanky panky is needed to overcome Swift’s strict typing. Sample code:
let animationOptions: UIView.AnimationOptions = .curveLinear
let keyframeAnimationOptions = UIView.KeyframeAnimationOptions(rawValue: animationOptions.rawValue)
UIView.animateKeyframes(withDuration: duration, delay: 0.1, options: keyframeAnimationOptions) {

How to make loading animation in iOS Swift?

Hi I want to make animation with 3 UIView. The main problem is I can't start animation once it's stopped by removing animation from layer.
Here is the code:
class HTAnimatedTypingView: UIView {
#IBOutlet weak var view1: UIView!
#IBOutlet weak var view2: UIView!
#IBOutlet weak var view3: UIView!
func startAnimation(){
UIView.animate(withDuration: 0.5, delay: 0, options: .repeat, animations: {
self.view1.frame.origin.y = 0
}, completion: nil)
UIView.animate(withDuration: 0.3, delay: 0.5, options: .repeat, animations: {
self.view2.frame.origin.y = 0
}, completion: nil)
UIView.animate(withDuration: 0.2, delay: 1.0, options: .repeat, animations: {
self.view3.frame.origin.y = 0
}, completion: nil)
}
func stopAnimations(){
self.view1.layer.removeAllAnimations()
self.view2.layer.removeAllAnimations()
self.view3.layer.removeAllAnimations()
}
}
Output of Above Code:
Expected Animation:
How can make it work with start animation & stop animation functionality? Thanks in advance...
Since you need to add some pause in between each sequence of animations, I would personally do it using key frames as it gives you some flexibility:
class AnimationViewController: UIViewController {
private let stackView: UIStackView = {
$0.distribution = .fill
$0.axis = .horizontal
$0.alignment = .center
$0.spacing = 10
return $0
}(UIStackView())
private let circleA = UIView()
private let circleB = UIView()
private let circleC = UIView()
private lazy var circles = [circleA, circleB, circleC]
func animate() {
let jumpDuration: Double = 0.30
let delayDuration: Double = 1.25
let totalDuration: Double = delayDuration + jumpDuration*2
let jumpRelativeDuration: Double = jumpDuration / totalDuration
let jumpRelativeTime: Double = delayDuration / totalDuration
let fallRelativeTime: Double = (delayDuration + jumpDuration) / totalDuration
for (index, circle) in circles.enumerated() {
let delay = jumpDuration*2 * TimeInterval(index) / TimeInterval(circles.count)
UIView.animateKeyframes(withDuration: totalDuration, delay: delay, options: [.repeat], animations: {
UIView.addKeyframe(withRelativeStartTime: jumpRelativeTime, relativeDuration: jumpRelativeDuration) {
circle.frame.origin.y -= 30
}
UIView.addKeyframe(withRelativeStartTime: fallRelativeTime, relativeDuration: jumpRelativeDuration) {
circle.frame.origin.y += 30
}
})
}
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
circles.forEach {
$0.layer.cornerRadius = 20/2
$0.layer.masksToBounds = true
$0.backgroundColor = .systemBlue
stackView.addArrangedSubview($0)
$0.widthAnchor.constraint(equalToConstant: 20).isActive = true
$0.heightAnchor.constraint(equalTo: $0.widthAnchor).isActive = true
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
animate()
}
}
It should be pretty straightforward, but feel free to let me know if you have any questions!
And this is how the result looks like:
One way could be to use a Timer. Keep an instance of Timer in your class. When startAnimation is called, schedule it. When stopAnimation is called, invalidate it. (This means that the currently ongoing animation will be completed before the animation actually stops, which IMO makes it a nice non-abrupt stop).
On each tick of the timer, animate the dots once. Note that the animation you apply on each dot should have the same duration, as in the expected output, they all bounce at the same rate, just at different instants in time.
Some illustrative code:
// startAnimation
timer = Timer.scheduledTimer(withTimeInterval: timerInterval, repeats: true) { _ in
self.animateDotsOnce()
}
// stopAnimation
timer.invalidate()
// animateDotsOnce
UIView.animate(withDuration: animationDuration, delay: 0, animations: {
self.view1.frame.origin.y = animateHeight
}, completion: {
_ in
UIView.animate(withDuration: animationDuration) {
self.view1.frame.origin.y = 0
}
})
// plus the other two views, with different delays...
I'll leave it to you to find a suitable animateHeight, timerInterval, animationDuration and delays for each view.
I'd recommend using a CAKeyframeAnimation instead of handling completion blocks and that sorcery. Here's a quick example:
for i in 0 ..< 3 {
let bubble = UIView(frame: CGRect(x: 20 + i * 20, y: 200, width: 10, height: 10))
bubble.backgroundColor = .red
bubble.layer.cornerRadius = 5
self.view.addSubview(bubble)
let animation = CAKeyframeAnimation()
animation.keyPath = "position.y"
animation.values = [0, 10, 0]
animation.keyTimes = [0, 0.5, 1]
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
animation.duration = 1
animation.isAdditive = true
animation.repeatCount = HUGE
animation.timeOffset = CACurrentMediaTime() + 0.2 * Double(i)
bubble.layer.add(animation, forKey: "anim")
}
When you wanna remove the animation you just use bubble.layer.removeAnimation(forKey: "anim"). You might have to play around with the timing function or values and keyTimes to get the exact movement you want. But keyframes is the way to go to make a specific animation.
Side note: this example won't work in viewDidLoad cause the view doesn't have a superview yet so the animation won't work. If you test it in viewDidAppear it will work.
UIView animation is different to CALayer animation,best not to mix them.
Write locally and tested.
import UIKit
import SnapKit
class HTAnimatedTypingView: UIView {
private let view0 = UIView()
private let view1 = UIView()
private let view2 = UIView()
init() {
super.init(frame: CGRect.zero)
makeUI()
}
override init(frame: CGRect) {
super.init(frame: frame)
makeUI()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
makeUI()
}
private func makeUI() {
backgroundColor = UIColor.white
view0.backgroundColor = UIColor.red
view1.backgroundColor = UIColor.blue
view2.backgroundColor = UIColor.yellow
addSubview(view0)
addSubview(view1)
addSubview(view2)
view0.snp.makeConstraints { (make) in
make.centerY.equalTo(self.snp.centerY)
make.width.equalTo(10)
make.height.equalTo(10)
make.left.equalTo(self.snp.left)
}
view1.snp.makeConstraints { (make) in
make.centerY.equalTo(self.snp.centerY)
make.width.equalTo(10)
make.height.equalTo(10)
make.centerX.equalTo(self.snp.centerX)
}
view2.snp.makeConstraints { (make) in
make.centerY.equalTo(self.snp.centerY)
make.width.equalTo(10)
make.height.equalTo(10)
make.right.equalTo(self.snp.right)
}
}
public func startAnimation() {
let duration:CFTimeInterval = 0.5
let animation_delay:CFTimeInterval = 0.1
assert(duration >= animation_delay * 5, "animation_delay should be way smaller than duration in order to make animation natural")
let translateAnimation = CABasicAnimation(keyPath: "position.y")
translateAnimation.duration = duration
translateAnimation.repeatCount = Float.infinity
translateAnimation.toValue = 0
translateAnimation.fillMode = CAMediaTimingFillMode.both
translateAnimation.isRemovedOnCompletion = false
translateAnimation.autoreverses = true
view0.layer.add(translateAnimation, forKey: "translation")
DispatchQueue.main.asyncAfter(deadline: .now() + animation_delay) { [unowned self ] in
self.view1.layer.add(translateAnimation, forKey: "translation")
}
DispatchQueue.main.asyncAfter(deadline: .now() + animation_delay * 2) { [unowned self ] in
self.view2.layer.add(translateAnimation, forKey: "translation")
}
}
public func stopAnimation() {
self.view0.layer.removeAllAnimations()
self.view1.layer.removeAllAnimations()
self.view2.layer.removeAllAnimations()
}
}

Frames changing suddenly instead of animating [iOS]

I have a view with a scroll view in my app (a percentage calculator). I am trying to animate the view to fly out and then fly in again from the left albeit with different contents, on the press of a button.
Here's my code:
func next()
{
UIView.animateWithDuration(0.5, animations: {
self.mainView.frame = CGRectMake(-500, self.view.bounds.height/2-self.mainHeight/2, self.mainWidth, self.mainHeight)
self.mainView.center.x -= 600
}) { (success) in
self.mainView.center.x += 1200
}
UIView.animateWithDuration(0.5, animations: {
self.mainView.frame = CGRectMake(self.view.bounds.width/2-self.mainWidth/2, self.view.bounds.height/2-self.mainHeight/2, self.mainWidth, self.mainHeight)
}) { (success) in
print("Animation Successful!")
self.drawView()
}
}
I don't understand why, but the animation doesn't occur, the view disappears completely and I see "Animation Successful" in the logs.
To be clear, I have created the view programmatically.
Here's the code in viewDidLoad(); the view displays correctly when it's called:
mainHeight = self.view.bounds.height * 0.85
mainWidth = self.view.bounds.width * 0.85
let viewHeight = self.view.bounds.height
let viewWidth = self.view.bounds.width
mainView.frame = CGRectMake(viewWidth/2-mainWidth/2, viewHeight/2-mainHeight/2, mainWidth, mainHeight)
mainView.layer.cornerRadius = 8
mainView.layer.masksToBounds = true
mainView.backgroundColor = getColor(0, green: 179, blue: 164)
self.view.addSubview(mainView)
scrollView.frame = CGRectMake(0, 0, mainWidth, mainHeight)
scrollView.contentSize = CGSizeMake(mainWidth, 800)
self.mainView.addSubview(scrollView)
contentView.frame = CGRectMake(0, 0, mainWidth, 800);
contentView.backgroundColor = getColor(0, green: 179, blue: 164)
contentView.layer.masksToBounds = true
contentView.clipsToBounds = true
self.scrollView.addSubview(contentView)
Place self.layoutViewIfNeeded() before frame/center changes.
Additionally you may need to put your next animation within a completion handler of the next animation.
func next() {
UIView.animateWithDuration(0.5, animations: {
self.view.layoutIfNeeded() // add this
self.mainView.frame = CGRectMake(-500, self.view.bounds.height/2-self.mainHeight/2, self.mainWidth, self.mainHeight)
self.mainView.center.x -= 600
}) { (success) in
self.view.layoutIfNeeded() // add this
self.mainView.center.x += 1200
// your next animation within a completion handler of previous animation
UIView.animateWithDuration(0.5, animations: {
self.mainView.frame = CGRectMake(self.view.bounds.width/2-self.mainWidth/2, self.view.bounds.height/2-self.mainHeight/2, self.mainWidth, self.mainHeight)
}) { (success) in
print("Animation Successful!")
self.drawView()
}
}
}
I haven't tested it yet, perhaps some changes or lines ordering is needed.
In Swift 3, I had to set the frame and then call layoutIfNeeded().
For example, an animation with spring:
UIView.animate(withDuration: 0.3, delay: 0.1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
self.subView.frame = CGRect(x: 0, y: 0, width: 500, height: 200)
self.view.layoutIfNeeded()
}) { (_) in
// Follow up animations...
}

Create a continuously rotating square on the screen using animateKeyframesWithDuration

I tried to use the code below to create a continuously rotating square on the screen. But I don't know why the rotational speed is changing. How could I change the code to make the rotational speed invariable? I tried different UIViewKeyframeAnimationOptions, but seems none of them work.
override func viewDidLoad() {
super.viewDidLoad()
let square = UIView()
square.frame = CGRect(x: 55, y: 300, width: 40, height: 40)
square.backgroundColor = UIColor.redColor()
self.view.addSubview(square)
let duration = 1.0
let delay = 0.0
let options = UIViewKeyframeAnimationOptions.Repeat
UIView.animateKeyframesWithDuration(duration, delay: delay, options: options, animations: {
let fullRotation = CGFloat(M_PI * 2)
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1/3, animations: {
square.transform = CGAffineTransformMakeRotation(1/3 * fullRotation)
})
UIView.addKeyframeWithRelativeStartTime(1/3, relativeDuration: 1/3, animations: {
square.transform = CGAffineTransformMakeRotation(2/3 * fullRotation)
})
UIView.addKeyframeWithRelativeStartTime(2/3, relativeDuration: 1/3, animations: {
square.transform = CGAffineTransformMakeRotation(3/3 * fullRotation)
})
}, completion: {finished in
})
}
I faced same problem before, this is how I make it work:
Swift 2
let raw = UIViewKeyframeAnimationOptions.Repeat.rawValue | UIViewAnimationOptions.CurveLinear.rawValue
let options = UIViewKeyframeAnimationOptions(rawValue: raw)
Swift 3,4,5
let raw = UIView.KeyframeAnimationOptions.repeat.rawValue | UIView.AnimationOptions.curveLinear.rawValue
let options = UIView.KeyframeAnimationOptions(rawValue: raw)
I figured this problem out just before gave it up. I don't think there is doc about it, but it just work.
That's really odd... UIView.animateKeyframesWithDuration isn't working as I would expect it to with UIViewKeyframeAnimationOptions.CalculationModeLinear|UIViewKeyframeAnimationOpti‌​ons.Repeat passed in with options.
If you use the non-block method of creating a keyframe animation (see below) the rotation repeats as expected.
If I find out why the block-based option isn't working I'll try and remember to update answer here too!
override func viewDidLoad() {
super.viewDidLoad()
let square = UIView()
square.frame = CGRect(x: 55, y: 300, width: 40, height: 40)
square.backgroundColor = UIColor.redColor()
self.view.addSubview(square)
let fullRotation = CGFloat(M_PI * 2)
let animation = CAKeyframeAnimation()
animation.keyPath = "transform.rotation.z"
animation.duration = 2
animation.removedOnCompletion = false
animation.fillMode = kCAFillModeForwards
animation.repeatCount = Float.infinity
animation.values = [fullRotation/4, fullRotation/2, fullRotation*3/4, fullRotation]
square.layer.addAnimation(animation, forKey: "rotate")
}
Add UIViewKeyframeAnimationOptionCalculationModeLinear do your keyframe options. The default behavior for UIView animations is to "ease in/out" of the animation. i.e., start slow, go up to speed, then slow down again just near the end.
AFAIU, this is happening because the default animation curve is UIViewAnimationOption.CurveEaseInOut.
Unfortunately, UIViewKeyframeAnimationOptions doesn't have options for changing the curve, but you can add them manually!
Use this extension:
Swift 2
extension UIViewKeyframeAnimationOptions {
static var CurveEaseInOut: UIViewKeyframeAnimationOptions { return UIViewKeyframeAnimationOptions(rawValue: UIViewAnimationOptions.CurveEaseInOut.rawValue) }
static var CurveEaseIn: UIViewKeyframeAnimationOptions { return UIViewKeyframeAnimationOptions(rawValue: UIViewAnimationOptions.CurveEaseIn.rawValue) }
static var CurveEaseOut: UIViewKeyframeAnimationOptions { return UIViewKeyframeAnimationOptions(rawValue: UIViewAnimationOptions.CurveEaseOut.rawValue) }
static var CurveLinear: UIViewKeyframeAnimationOptions { return UIViewKeyframeAnimationOptions(rawValue: UIViewAnimationOptions.CurveLinear.rawValue) }
}
Swift 3, 4, 5
extension UIView.KeyframeAnimationOptions {
static var curveEaseInOut: UIView.KeyframeAnimationOptions { return UIView.KeyframeAnimationOptions(rawValue: UIView.AnimationOptions.curveEaseInOut.rawValue) }
static var curveEaseIn: UIView.KeyframeAnimationOptions { return UIView.KeyframeAnimationOptions(rawValue: UIView.AnimationOptions.curveEaseIn.rawValue) }
static var curveEaseOut: UIView.KeyframeAnimationOptions { return UIView.KeyframeAnimationOptions(rawValue: UIView.AnimationOptions.curveEaseOut.rawValue) }
static var curveLinear: UIView.KeyframeAnimationOptions { return UIView.KeyframeAnimationOptions(rawValue: UIView.AnimationOptions.curveLinear.rawValue) }
}
Now you can use curve options in UIView.animateKeyframesWithDuration method
Swift 2
let keyframeOptions: UIViewKeyframeAnimationOptions = [.Repeat, .CurveLinear]
UIView.animateKeyframesWithDuration(duration, delay: delay, options: keyframeOptions, animations: {
// add key frames here
}, completion: nil)
Swift 3, 4, 5
let keyframeOptions: UIView.KeyframeAnimationOptions = [.repeat, .curveLinear]
UIView.animateKeyframes(withDuration: duration, delay: delay, options: keyframeOptions, animations: {
// add key frames here
}, completion: nil)

Resources