Whats the Swift animate WithDuration syntax?
Swift 3/4 Syntax
Here's an update with the Swift 3 Syntax:
UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: {
self.username.center.x += self.view.bounds.width
}, completion: nil)
If you need to add a completion handler just add a closure like so:
UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: {
// animation stuff
}, completion: { _ in
// do stuff once animation is complete
})
Old Answer:
It turns out it's a very simple fix, just change options: nil
to options: []
.
Swift 2.2 Syntax:
UIView.animateWithDuration(0.5, delay: 0.3, options: [], animations: {
self.username.center.x += self.view.bounds.width
}, completion: nil)
What changed?
Swift 2 got rid of the C-Style comma-delimited list of options in favor of option sets (see: OptionSetType). In my original question, I passed in nil
for my options, which was valid prior to Swift 2. With the updated syntax, we now see an empty option list as an empty set: []
.
An example of animateWithDuration with some options would be this:
UIView.animateWithDuration(0.5, delay: 0.3, options: [.Repeat, .CurveEaseOut, .Autoreverse], animations: {
self.username.center.x += self.view.bounds.width
}, completion: nil)
Swift UIView.animateWithDuration
The error doesn't seem related to the real problem here, with Swift 2.0 options
are now OptionSetType
and you should pass an empty Array
if you want to specify no options. Also seems I had to specify the closure for animations
(non-nullable).
This should work :
UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.3, options: [], animations: {}, completion: nil)
UIView.animateWithDuration in Swift 2.0?
You are passing enum of type UIViewAnimationTransition
to an argument which requires type UIViewAnimationOptions
(options
argument)
Here is the correct syntax with the correct enum value:
func moveSideBarToXposition(iXposition: Float) {
let convertedXposition = CGFloat(iXposition)
UIView.animateWithDuration(0.5, delay: 1.0, options: UIViewAnimationOptions.TransitionNone, animations: { () -> Void in
self.contentView.frame = CGRectMake(convertedXposition, 20, self.contentView.frame.size.width, self.contentView.frame.size.height)
}, completion: { (finished: Bool) -> Void in
// you can do this in a shorter, more concise way by setting the value to its opposite, NOT value
isMenuHidden = !isMenuHidden
})
}
UIView.animateWithDuration swift 3
//Animate on key press... (For Swift 3.0)
UIView.animate(withDuration: 0.2, animations: {
button.transform = CGAffineTransform(scaleX: 2.0, y: 2.0)
}, completion:{ _ in
button.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
})
Showing error while using 'animateWithDuration'
Like this:
UIView.animate(withDuration: 0.35) {
// animate things
return
}
UIView.animateWithDuration completion
I presume you mean popToRootViewControllerAnimated
in your first snippet, since returnToRootViewController
isn't a method on UUNavigationController
.
Your problem is that popToRootViewControllerAnimated
has a return value (the array of view controllers removed from the navigation stack). This causes trouble even though you're trying to discard the return value.
When Swift sees a function/method call with a return value as the last line of a closure, it assumes you're using the closure shorthand syntax for implicit return values. (The kind that lets you write things like someStrings.map({ $0.uppercaseString })
.) Then, because you have a closure that returns something in a place where you're expected to pass a closure that returns void, the method call fails to type-check. Type checking errors tend to produce bad diagnostic messages — I'm sure it'd help if you filed a bug with the code you have and the error message it's producing.
Anyhow, you can work around this by making the last line of the closure not be an expression with a value. I favor an explicit return
:
UIView.animateWithDuration(0.3, animations: {
self.view.layoutIfNeeded()
}, completion: { (complete: Bool) in
self.navigationController.popToRootViewControllerAnimated(true)
return
})
You can also assign that popToRootViewControllerAnimated
call to an unused variable or put an expression that does nothing after it, but I think the return
statement is clearest.
Can't put transform change in animation block of animateWithDuration in Swift
You could omit the Void -> in
altogether in your case; the real culprit that is causing the problem is this line:
self.zigZag!.layer.mask.transform = CGAffineTransformMakeTranslation(100, 0)
because there is a type mismatch.
It should be:
Swift 2
UIView.animateWithDuration(0.5, delay: 0.05, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.view!.layer.mask.transform = CATransform3DMakeTranslation(100.0, 0.0, 0.0)
}, completion: nil)
Swift 3, 4, 5
UIView.animate(withDuration: 0.5, delay: 0.05, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.5, options: UIView.AnimationOptions.curveEaseIn, animations: {
self.view!.layer.mask.transform = CATransform3DMakeTranslation(100.0, 0.0, 0.0)
}, completion: nil)
Blocks on Swift (animateWithDuration:animations:completion:)
The completion parameter in animateWithDuration
takes a block which takes one boolean parameter. In Swift, like in Obj-C blocks, you must specify the parameters that a closure takes:
UIView.animateWithDuration(0.2, animations: {
self.blurBg.alpha = 1
}, completion: {
(value: Bool) in
self.blurBg.hidden = true
})
The important part here is the (value: Bool) in
. That tells the compiler that this closure takes a Bool labeled 'value' and returns Void.
For reference, if you wanted to write a closure that returned a Bool, the syntax would be
{(value: Bool) -> bool in
//your stuff
}
Adding two UIViewAnimationOptions to an animation (Swift)
In Swift 2
you have to write in this way. The OptionSetType
has an updated syntax.
UIView.animateWithDuration(0.33, delay: 0, options:[UIViewAnimationOptions.CurveEaseOut, UIViewAnimationOptions.AllowUserInteraction], animations: { () -> Void in
self.myTableView.setContentOffset(CGPoint(x: 0, y: 8), animated: false)
}, completion: nil)
Related Topics
Swift Xcode Index Freezing or Slow
What Is the Real Benefit of Using Raycast in Arkit and Realitykit
How to Get User Home Directory Path (Users/"User Name") Without Knowing the Username in Swift3
How to Create a Fixed-Size Array of Objects
Difference Between 2 Dates in Weeks and Days Using Swift 3 and Xcode 8
Swift 2: Expression Pattern of Type 'Bool' Cannot Match Values of Type 'Int'
How to Use Generic Types to Get Object with Same Type
Compress Image in iOS 12. How Will This Code Be Updated
Ui Test Deleting Text in Text Field
How to Convert Range in Nsrange
Swiftui - Is There a Popviewcontroller Equivalent in Swiftui
Swift - How to Mutate a Struct Object When Iterating Over It
Compile Error in Swift 4 on Parameter Passing
How to Make Class Methods/Properties in Swift
Building Pure Swift Cocoa Touch Framework
Using Foreach Loop with Binding Causes Index Out of Range When Array Shrinks (Swiftui)