UIView is not showing except for a button action in swift - ios

I have created a UI in storyboard which has top, center, and bottom UIViews.
Below the bottom screen there is another UIView which will show only for a button action in bottom, this comes as .CurveLinear from bottom to top ,
This works perfectly fine for a button action, but I want to happen this for a viewDidAppear, but its not working:
UIView.animateWithDuration(0.5, delay: 0.1, options: .CurveLinear , animations: {
self.infoView.frame = CGRectMake(0, 300, self.infoView.frame.size.width, self.infoView.frame.size.height)
}) { (true) in
}
This is the code block that I execute to bring the view from bottom to top, how come it only works for a button action?

Found the issue , this animation didn't work until i use NSTimer , i think i had to wait until the view renders properly , but viewDidAppear executes after the view renders , so too many unanswered questions

Related

Swift - removeAllAnimations() - One animation after the other

So I animated a button to move to a specific point (CGPoint) on the screen when the view loads. I also added a swipeGestureRecogniser to make it that if the user swipes left, than the animation will stop and a new one will start that takes the button to a different CGPoint. I did this by calling the removeAllAnimations() method on the button. The problem is that when the user swipes left, the button finishes the first animation without animating. So it doesn't stop and start the new animation, but rather it jumps to the end of the first animation and then continues to the next animation. Is there a way to stop the first animation and seamlessly start the next?
This is the code:
It's working as expected. At the time you call removeAllAnimations(), there's only one animation to be removed - the second one will be added in first's completion block. So instead of nesting animations, you can try something like that:
// first animation
UIView.animate(withDuration: 5, delay: 0, options: .curveLinear, animations: { /... }, completion: { //... })
// second animation
UIView.animate(withDuration: 5, delay: 5, animations: { /... })

Animate NSLayoutConstraint & view simultaneously in separate blocks

I have two relatively simple animations. One is animating the top constraint of a UIButton so that it slides up. The other is animating the background color on a UIView.
self.buttonAnimationConstant.constant = 0
self.view.layoutIfNeeded() // ensure the constraint is at 0
self.buttonAnimationConstraint.constant = 100
UIView.animateWithDuration(0.25, delay: 0.0) {
self.view.layoutIfNeeded()
}
and my color animation:
UIView.animateWithDuration(0.25, delay: 0.0) {
self.colorView.backgroundColor = UIColor.purpleColor()
}
If I try executing them at the same time, then the button will animate up but it cancels the color animation, presumably because of view.layoutIfNeeded. Note that these two animations are in separate places so they can't be joined into one block (one sits at the view controller level, the other embedded inside a custom view but both within the same view controller). How can I animate both a constraint and a view property such that one doesn't cancel the other?
Essentially the issue is how do you go about animating both a constraint and a view that sit in the same view hierarchy?
Animation blocks do not cancel each other. The issue here is this line:
self.view.layoutIfNeeded() // ensure the constraint is at 0
Calling layoutIfNeeded outside an animation block will cancel your animations (because they set a new value for the values you animate) and there is not much you can do to prevent that. What you could do is making sure that no layoutIfNeeded is called outside an animation block while performing an animation. Of course this does not happen if the views are not in the same view hierarchy.

Fading Images by swipe or tap between two images

I want to create a fade between two images by swiping or taping on the image.
For example:
Two or more images are on top of the another and with swiping to left/right or clicking on the image it would change (not automatically – the picture need a interaction).
You should place 2 imageviews ontop each other, one as hidden and one as visible. Then place some empty UIButton on top of them to interact on press. The animation part should look something like that more or less:
UIView.animateWithDuration(0.3, delay: 0, options: .TransitionCrossDissolve, animations: {
firstImageView.hidden = true
secondImageView.hidden = false
}, completion: nil)

UIView Transition in table view

I want to do transition of a view inside a table view cell. More precisely, when user clicks on a button in UITableViewCell a view should slide in from the right side of the cell and this view should slide back to its original position when user clicks on a button in the slided view. I could do all of this except the animation of sending the slided view back to its original position.
To accomplish this what I have done so far is, created a xib file for the table view cell which contains two views side by side, let it be view1 and view2. When the tableview is loaded to screen, only view1 is visible to the user. And set auto layout constraint for view1.trailingedge and view2.leadingedge as 0, the constraint is connected to cell.swift and named leftConstraint. Now when a button in view1 is clicked view2 slides in from the right side of the cell using the following code :-
self.view2.transform = CGAffineTransformMakeTranslation(UIScreen.mainScreen().bounds.width, 0)
UIView.animateWithDuration(0.4, animations: { () -> Void in
self.view2.transform = CGAffineTransformIdentity
self.leftConstraint.constant -= UIScreen.mainScreen().bounds.width
})
When tried to send back view2 to its old position (this should happen on the click of a button in view2) ie. right side of the cell animation is not working, all the elements in view2 just disappears. Using the following code for sending view to the right side of the cell:-
self.view2.transform = CGAffineTransformMakeTranslation(UIScreen.mainScreen().bounds.width, 0)
UIView.animateWithDuration(0.4, animations: { () -> Void in
self.view2.transform = CGAffineTransformIdentity
self.leftConstraint.constant += UIScreen.mainScreen().bounds.width
})
Thanks in Advance for any help. And special Thanks to #MarkHim for helping.
Since you need the transformation for the rightOut position twice, first for the in-animation and later for the out animation, you could save it in a variable like rightOutTransform. Try:
CGAffineTransform rightOutTransform = CGAffineTransformMakeTranslation(self.view.fame.size.width, 0);
// assuming view2 is currently in the cell
UIView.animateWithDuration(0.4, animations: {
self.view2.transform = rightOutTransform //
})
You might want to think about removing views you don't need anymore from the view hierarchy like this when the animation completed
view2.removeFromSuperview();

How to recognize tap gesture while a view is animating

Just wondering is there way to have a view recognize tap gestures while it is being animated? I am working on a view that has a cashapelayer line tethered to it. When the user pans the view (pan gesture) the line follows accordingly until the user stops panning. At this point an animation is executed that brings the view back to its original position and the tether layer back as well. Now my only real problem is that while the view and the tether are animating the view doesnt respond to tap gestures…
Anyone know some tricks? I hope my explanation was understandable and thanks in advance!
(if the tethered view concept is not clear there is a free app called discovr apps which will give an example).
I'm assuming that you are using the [UIView animateWithDuration: delay: options: animations: completion:]; method of animating.
If so, you need to pass UIViewAnimationOptionAllowUserInteraction as an option to get the animated view to respond to touches while it is animating.
(Swift 3) Pass .allowUserInteraction option
UIView.animate(withDuration: 0.75, delay: 0.0, options: [.allowUserInteraction], animations: {
// Desired animation(s)
}, completion: { (finished: Bool) in
// Completion
})
You need to set two options - UIViewAnimationOptionAllowUserInteraction and UIViewAnimationOptionAllowAnimatedContent. First lets you interact with views during animation, second forces to redraw views on every frame of animation and not use snapshots of beginning and ending frames.

Resources