Fading Images by swipe or tap between two images - ios

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)

Related

Animation on UIButton's Image stops when another UIButton Background color is changed

Context:
Swift 4
There is a stackView of UIButtons, that exists in a scrollView. If a button has an image, it animates infinitely. Once another button is tapped in the stackView, the animation stops. I've tried running on the main thread, and commitAnimations (which isn't recommended) - neither have worked.
The code is proprietary, so I can't paste it, but here's the idea:
This is the code that animates the button's image:
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [.repeat, .autoreverse], animations: {
view.alpha = 0
}, completion: nil)
In the function that selects another button in the scrollView, a few things are happening:
scrollToItem is being called on a collectionView
scrollRectToVisible is being called
Some properties like backgroundColor are being called on views
Edit
After trying to narrow the cause of the problem, I've found that the issue is updating another button's background color, not when a scrollView scrolls.
If I can provide more context, please let me know!

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: { /... })

UIView is not showing except for a button action in swift

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

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