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.
Related
I am trying to implement following animation for my application onboarding and it needs to be play forever and reverse back every time it finishes.
I designed following image to accomplish that animation.
The image size is 880x335. So I wrote following code to animate the image.
func startAnimation(with duration: TimeInterval, afterDelay time: TimeInterval, horizontalConstantFirstPosition: CGFloat, horizontalConstantSecondPosition: CGFloat) {
switch animationOrder {
case .first:
self.animatedImageViewAnimatableConstraint.constant = 110
UIView.animate(withDuration: duration, delay: time, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [.curveEaseOut], animations: {
self.view.layoutIfNeeded()
self.animationOrder = .second
})
case .second:
self.animatedImageViewAnimatableConstraint.constant = -110
UIView.animate(withDuration: duration, delay: time, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [.curveEaseOut], animations: {
self.view.layoutIfNeeded()
self.animationOrder = .third
})
case .third:
self.animatedImageViewAnimatableConstraint.constant = -330
UIView.animate(withDuration: duration, delay: time, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [.curveEaseOut], animations: {
self.view.layoutIfNeeded()
self.animationOrder = .fourth
})
case .fourth:
self.animatedImageViewAnimatableConstraint.constant = 110
UIView.animate(withDuration: duration, delay: time, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [.curveEaseOut], animations: {
self.view.layoutIfNeeded()
self.animationOrder = .second
})
}
}
Basically, animatedImageViewAnimatableConstraint is the center X constraint that is initialized with 330 which is the position of first image I designed. Every time my animation method is called I changed center x anchor to the positions of value 330, 110, -110, -330 and so on.
However, when I applied it is not animating as expected. It returns back from fourth portion of the image to first one, it is excepted to go from fourth position to second one as shown above.
What should I do for the correct one, or which part is looking like wrong? Thanks in advance.
#IBOutlet weak var imageView: UIImageView!
let images = [#imageLiteral(resourceName: "img1"), #imageLiteral(resourceName: "img2"), #imageLiteral(resourceName: "img3")]
imageView.animationImages = images
imageView.animationDuration = 3.0 //1 sec for each image
imageView.animationRepeatCount = 0 //Set 0 for infinite
imageView.startAnimating()
To stop just call
imageView.stopAnimating()
I am using TextFieldEffects to add text inputs effects in my textField. it is showing an error
Type 'UIView' has no member 'AnimationOptions'
Here's code.
override open func animateViewsForTextEntry() {
borderLayer.frame.origin = CGPoint(x: 0, y: font!.lineHeight)
UIView.animate(withDuration: 0.2, delay: 0.3, usingSpringWithDamping: 0.8, initialSpringVelocity: 1.0, options: UIView.AnimationOptions.beginFromCurrentState, animations: ({
self.placeholderLabel.frame.origin = CGPoint(x: self.placeholderInsets.x, y: self.borderLayer.frame.origin.y - self.placeholderLabel.bounds.height)
self.borderLayer.frame = self.rectForBorder(self.borderThickness, isFilled: true)
}), completion: { _ in
self.animationCompletionHandler?(.textEntry)
})
}
override open func animateViewsForTextDisplay() {
if text!.isEmpty {
UIView.animate(withDuration: 0.35, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 2.0, options: UIView.AnimationOptions.beginFromCurrentState, animations: ({
self.layoutPlaceholderInTextRect()
self.placeholderLabel.alpha = 1
}), completion: { _ in
self.animationCompletionHandler?(.textDisplay)
})
borderLayer.frame = rectForBorder(borderThickness, isFilled: false)
}
}
Here's a screenshot of the error.
How to solve this issue?
It should be UIViewAnimationOptions.beginFromCurrentState, at least in Xcode < version 10.
Try changing UIView.AnimationOptions.beginFromCurrentState to [.beginFromCurrentState].
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 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)
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.