UIView animations in Swift - swift

I am trying to execute following code.
UIView.animateWithDuration(5.0, animations: {
println("animations")
}, completion: { finished in
println("completion")
})
So Here completion block is called immediately before 5 sec animation duration. Don't know what is wrong here.
Please let me know for proper code.

You should add some view for animation:
UIView.animateWithDuration(5.0, animations: {
someView.alpha = 0
println("animations")
}, completion: { finished in
println("completion")
})

You don't have any animation sequence in your animations block, try animating your current view and check it'll work fine.
UIView.animateWithDuration(5.0, animations: {
self.view.alpha = 0.5;
print("animations")
}, completion: { finished in
self.view.alpha = 1.0;
print("completion")
})

Related

Why is my button being removed from its superview before it completes its animation?

I am trying to move an my button to a certain part of the screen [as a custom button class GameBlock] and then remove it from its superview. However, it is acting weird in the sense that it removes itself from the superview before it completes the animation and I have no clue why. May somebody please help me out? I appreciate it.
UIView.animate(withDuration: movementTime,
delay: 0.0,
options: [],
animations: {
movedBlock.center = gridCoord[hitBlock.x][hitBlock.y]
},
completion: nil)
if movedBlock.center == gridCoord[hitBlock.x][hitBlock.y] {
movedBlock.removeFromSuperview()
}
If I understand your problem, this should works. Your view will be removed after the animation. The completion closure will be called at the end of the animation.
UIView.animate(withDuration: movementTime,
delay: 0.0,
options: [],
animations: {
movedBlock.center = gridCoord[hitBlock.x][hitBlock.y]
},
completion: { _ in
if movedBlock.center == gridCoord[hitBlock.x][hitBlock.y] {
movedBlock.removeFromSuperview()
}
})

Delay in DispatchQueue.main.async

I have a design like below flow. I need to set delay between 5th and 6th steps for 0.3second. I tried below options but couldn't get any result.
My question is, how can I achieve this?
Note: 13seconds for see the animation.
Flow
Task Handler // for webService request
Closure Handler // for trigger ViewController
DispatchQueue.main.async // for Update UI
First Animation
Second Animation
Navigation to next screen
Test 1
Timer.scheduledTimer(withTimeInterval: 13, repeats: false, block: {})
Test 2
UIView.animate(withDuration: 13, animations: {
// nothing should be happened
self.ivSuccessMark.alpha = 0.99 // for dummy animation diff
}, completion: { (completion) in
// navigation
})
Test 3
perform(_:with:afterDelay:)
Try this one
UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: {
// animation stuff
}, completion: { _ in
// do stuff once animation is complete
})
Try this one I hope it help you(Up to 4 seconds Stop all Action in view)
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(4), execute: {
// Put your code which should be executed with a delay here
})

Swift Animate duration not working in CGAffineTransform

When I am translating one view with an animation of 1 second it is not working, but when I am executing the transform.identity it works fine.
Here is my code:
func hideCarousel() {
UIView.animate(withDuration: 1, animations: {
self.carouselER.transform = CGAffineTransform(translationX: 0, y: 200)
})
}
func showCarousel() {
UIView.animate(withDuration: 1, animations: {
self.carouselER.transform = .identity
})
}
To solve this issue I forced the animation to run in the main thread. Every time that you have problems with the performance of your UI elements like your animations or updating your label texts, try forcing to run the UI change in the main thread.
DispatchQueue.main.async {
UIView.animate(withDuration: 1, animations: {
self.carouselER.transform = CGAffineTransform(translationX: 0, y: 200)
})
}
I have also faced that problem with one timer that updated a label, but in this issue I thought it was some kind of problem of the CGAffineTransform.

animateWithDuration Animates Once But Not Twice

I have this piece of animation in a switch statement that runs and does what it's supposed to. The animation is called successfully every twenty seconds inside a timer. However, the animation does not happen any time but the first time. In my code below you will see println statements that I used to try and highlight the problem. Each println statement prints but no animation happens. What is it that I'm missing?
func popAnimation(indexNumber: Int) {
let image = self.overlayImageView[indexNumber]
image.hidden = false
image.alpha = 0.0
UIView.animateWithDuration(0.75, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: nil, animations: { () -> Void in
println("animation ran")
println(image.alpha)
image.image = UIImage(named: "lowCountOverlay")
image.alpha = 1.0
}, completion: { (Bool) -> Void in
UIView.animateWithDuration(0.75, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: nil, animations: { () -> Void in
println("animation 2 ran")
println(image.alpha)
image.alpha = 0.0
}, completion: { (Bool) -> Void in
println("animation 3 ran")
image.hidden = true
})
})
}
UPDATE: If I remove image.hidden from the second completion block it works fine and animates repeatedly. Which is interesting because if it's not hidden it should be covering other interactive content in the view, but it's not. The other content is perfectly accessible in simulator. The UIImageView image is definitely the top layer in Storyboard.
I could be off, but it seems like you're running from an NSTimer which may or may not be in the main thread.
UI updates need to take place in the main thread, try wrapping the completion handler in a GCD dispatch directed to the main thread like the following and see if that helps? Maybe even force the whole thing to the main thread.
UIView.animateWithDuration(0.75, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: nil, animations: { () -> Void in
// First animation
}, , completion: { (Bool) -> Void in
dispatch_async(dispatch_get_main_queue(),{
// second animation
})
})
I think you need to look for your bug elsewhere. You don't seem to be missing anything in the codeā€”as I'm able to run the animation repeatedly without any issues. I've copied your code as-is to a separate project and the animation works every time.
Try replacing:
let image = self.overlayImageView[indexNumber]
image.hidden = false
image.alpha = 0.0
with
let image = self.overlayImageView[indexNumber]
image.superview.bringSubviewToFront(image)
image.hidden = false
image.alpha = 0.0

Swift: Animation effect when transition between one view to another view

I am doing wow slider animation effect in my app by using swift. I added 3 images. It is sliding one by one. But I need animation like this. I don't know how to do when transition from one image to another. Kindly guide me. Animation effect is below. Not only this animation effect, but also any animation effect.
Not exactly same , but similar animation effects can be achieved using UIViewAnimations. For example try this:
[UIView transitionWithView:textFieldimageView
duration:0.5f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
imageView.oldImage = newImage;
} completion:NULL];
Try this syntax for swift:
Swift 2
UIView.transitionWithView(self.view, duration: 0.5, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
}, completion: { (fininshed: Bool) -> () in
})
Swift 3, 4, 5
UIView.transition(with: self.view, duration: 0.5, options: UIView.AnimationOptions.transitionCrossDissolve, animations: {
}, completion: { (fininshed: Bool) -> () in
})