I have some problem about animation options with array in Swift 2. I can add multiple options in array like this : [.Repeat, UIViewAnimationOptions.Autoreverse] and this is if I add in animation function (first example) :
UIView.animateWithDuration(1, delay: 0, options: [.Repeat, UIViewAnimationOptions.Autoreverse], animations: {
}) { (true) in
}
But I cannot add animation options in array like this (second example) :
var animationOptions = [UIViewAnimationOptions]()
animationOptions.append(UIViewAnimationOptions.Repeat)
animationOptions.append(UIViewAnimationOptions.Autoreverse)
UIView.animateWithDuration(1, delay: 0, options: animationOptions, animations: {
}) { (true) in
}
Can someone help me make animation options in array like second example ?
var animationOptions:UIViewAnimationOptions = .repeat
animationOptions.insert(.autoreverse)
UIView.animate(withDuration: 0.1, delay: 0.1, options: animationOptions, animations: {
}) { (success:Bool) in
}
You need to use the insert from the SetAlgebra Protocol, which the OptionSet conforms to. In the question you are using the Array Object instead of the UIViewAnimationOptions.
You can use animation option like this:
Swift 2
UIView.animateWithDuration(0.2, delay: 0.0, options: [.Repeat,
.Autoreverse, .CurveLinear, .CurveEaseOut, .CurveEaseInOut,
.TransitionCurlUp, .TransitionCurlDown,
.TransitionFlipFromBottom,.TransitionFlipFromLeft,.TransitionFlipFromRight,
.BeginFromCurrentState, .CurveEaseIn], animations: {}, completion: nil)
Swift 3, 4, 5
UIView.animate(withDuration: 0.2, delay: 0.0, options: [.repeat,
.autoreverse, .curveLinear, .curveEaseOut, .curveEaseInOut,
.transitionCurlUp, .transitionCurlDown,
.transitionFlipFromBottom,.transitionFlipFromLeft,.transitionFlipFromRight,
.beginFromCurrentState, .curveEaseIn], animations: {}, completion: nil)
Related
I use this code in viewDidAppear method
UIView.animate(withDuration: 0,5, delay: 0.5,
usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0,
options: [], animations: {
self.loginButton.alpha = CGFloat(1.0)
}
, completion: nil)
in the picture below I have a problem which I can't fix
problem
Little mistake withDuration parameter 0,5 would be 0.5.
I'm animating the scale of a button using animateKeyframes in Swift 3 using the following code:
UIView.animateKeyframes(withDuration: 4, delay: 1, options: [.repeat, .allowUserInteraction, .calculationModeCubic], animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.1, animations: {
self.myButton.transform = CGAffineTransform(scaleX: 1,y: 1)
})
UIView.addKeyframe(withRelativeStartTime: 0.1, relativeDuration: 0.1, animations: {
self.myButton.transform = CGAffineTransform(scaleX: 0.9,y: 0.9)
})
}, completion: nil)
What I need to do is add easing to each keyframe. I looked up something that uses the following code that can then be passed to animateKeyframes
let animationOptions: UIViewAnimationOptions = .curveEaseIn
let keyframeAnimationOptions: UIViewKeyframeAnimationOptions = UIViewKeyframeAnimationOptions(rawValue: animationOptions.rawValue)
However, it doesn't quite say exactly how to pass the keyframeAnimationOptions variable, and I can't seem to figure it out myself! Can anybody help?
You can use below code to call the keyframeAnimationOptions variable.
let keyframeAnimationOption=UIViewKeyframeAnimationOptions.CalculationModeCubic
UIView.animateKeyframesWithDuration(duration, delay: delay, options: keyframeAnimationOption, animations:
{
/////Your code //////
}, completion: nil)
I'm trying to implement this animation where a UIButton fades out when a user saves a file. While the button fades out, a UIImageView of a check mark image fades in place of the button. Once the imageview has fully faded in, then it fades out and the button fades back in.
UIView.animateWithDuration(0.60, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.recButton.alpha = 0.0
}, completion: {
(value: Bool) in
UIView.animateWithDuration(0.60, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.checkView.alpha = 1.0
}, completion: {
(value: Bool) in
UIView.animateWithDuration(0.60, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.checkView.alpha = 0.0
}, completion: {
(value: Bool) in
UIView.animateWithDuration(0.60, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.recButton.alpha = 1.0
self.recButton.enabled = true
}, completion: nil)
})
})
})
While the method above does work, its not as smooth as I would like. Is there a better way to go about this?
You need four keyframes for the animation you want: button fade out, image fade in, image fade out, button fade in.
let button: UIButton
let checkmarkImage: UIImageView
button.alpha = 1.0
checkmarkImage = 0.0
UIView.animateKeyframesWithDuration(2.4, delay: 0, options: .CalculationModeLinear, animations: {
UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 0.25, animations: {
button.alpha = 0.0
})
UIView.addKeyframeWithRelativeStartTime(0.25, relativeDuration: 0.25, animations: {
checkmarkImage.alpha = 1.0
})
UIView.addKeyframeWithRelativeStartTime(0.5, relativeDuration: 0.25, animations: {
checkmarkImage.alpha = 0.0
})
UIView.addKeyframeWithRelativeStartTime(0.75, relativeDuration: 0.25, animations: {
button.alpha = 1.0
})
}, completion: nil)
This question raised my curiosity - this keyframe thing is pretty cool.
I am trying to have a guy move to the left, turn around and then move to the right. I know how to do this in Spritekit, which is using SKAction sequence. However, I am really having a hard time doing it in a app rather than a game. What I have now will result in the guy moving and turning at the same time:
UIView.animateWithDuration(5.0, delay: 0.0, options: [ .Repeat, .Autoreverse, .CurveEaseInOut], animations: {
self.guy.center.x -= 130
}, completion: nil)
//
UIView.animateWithDuration(5.0, delay: 5.0, options: [ .CurveEaseInOut, .Repeat, .Autoreverse ], animations: {
self.guy.transform = CGAffineTransformMakeScale(-1, 1)
}, completion:nil )
Thanks!
You can use the completion handler if the first animation to fire the second animation.
Put that into a function and fire this function with a timer to create a loop
UIView.animateWithDuration(5.0, delay: 0.0, options: [ .Repeat, .Autoreverse, .CurveEaseInOut], animations: {
self.guy.center.x -= 130
}, completion: {
UIView.animateWithDuration(5.0, delay: 0.0, options: [ .CurveEaseInOut, .Repeat, .Autoreverse ], animations: {
self.guy.transform = CGAffineTransformMakeScale(-1, 1)
}, completion:nil )
})
class Example: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
var aView : UIView!
UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.8, options: nil, animations: {
self.aView.transform = CGAffineTransformIdentity //This line is throwing the error mentioned in the Title
}, completion: { finished in
transitionContext.completeTransition(true)
})
}
This was working in earlier version of Swift but failing in version 2 not sure why
You just have to change
UIView.animateWithDuration(duration,
delay: 0.0,
usingSpringWithDamping: 0.8,
initialSpringVelocity: 0.8,
options: nil,
animations: {
with:
UIView.animateWithDuration(duration,
delay: 0.0,
usingSpringWithDamping: 0.8,
initialSpringVelocity: 0.8,
options: [],
animations: {
There is just the "options" to change.