I am successfully able to flip a view using the following code:
UIView.transition(from: firstView, to: secondView, duration: 1, options: UIViewAnimationOptions.transitionFlipFromRight, completion: nil)
However, I would like to increase this views height while the animation is occurring. Is this possible?
I tried animating the height before transitioning, but it does nothing:
//original height is 300.0
firstViewHeightConstraint.constant = 600.0
UIView.animate(withDuration: 0.3, animations: {
self.view!.layoutIfNeeded()
})
UIView.transition(from: firstView, to: secondView, duration: 1, options: UIViewAnimationOptions.transitionFlipFromRight, completion: nil)
You will need to call the other overload of UIView.transistion on the superview of the two views in question and manually animate out and in the views. For the new view you have to composite the flip rotation and the scale into a single matrix, set it as the initial transform and then animate to identity and for the old view do you do the inverse.
Related
I want to have my tableview increase in size and moves up when scrolled down while also keeping the constraint to the bottom layout. I thought I would try CGAffineTransform.
func MoveUP() {
// pop up login screen
let bottom = CGAffineTransform(translationX: 0, y: -30)
UIView.animate(withDuration: 0.7, delay: 0.2, options: [], animations: {
// Add the transformation in this block
// self.container is your view that you want to animate
self.numberOfProperties.transform = bottom
self.tableview.transform = bottom
}, completion: nil)
}
The problem with this is that it moves up the tableview but does not keep the proper constraints to the bottom. Many apps seems to have this behavior, what tool am I missing in order to achieve this result?
I want to animate a view like using the following code:
UIView.animate(withDuration: 2.7, delay: 0.1, options: .allowAnimatedContent, animations: {
self.AVCenterY.constant = 0.8
}, completion: nil)
But it happens so fast it seems like it is not animated. On the other hand, when I animate the property alpha it is animated (it takes the 2.7 seconds to change). I used 2.7 sec to make sure the problem was that I was using a small duration time.
Constraints cannot be animated at all. It is the act of layout that can be animated:
UIView.animate(withDuration: 2.7, delay: 0.1, options: .allowAnimatedContent, animations: {
self.AVCenterY.constant = 0.8
theView.superview?.layoutIfNeeded() // *
}, completion: nil)
When we animate the act of layout (or when the runtime does so), then any constraint changes are also automatically animated.
Note that what I animate is the layout of the superview of the view that is to move. I called it theView but that is just something I made up. You will need an outlet to that view so that you can get its superview, and use the name of that outlet.
I've got a collection view setup to run with a custom collection view layout. I can animate cells appearing and disappearing by changing their transformations and alpha on the UICollectionViewLayoutAttributes, however, I would like to change the animation curve to use springs + damping in order to make the cells "bounce" when they appear.
Does anybody have an idea of how I can achieve this?
Thanks,
Wrap your UICollectionView's deleteItems/insertItems/reloadItems calls inside UIView.animateWithDuration:delay:options:animations:completion: call.
Example:
let duration = 0.25 // Hardcoded keyboard animation duration and curve
let curve = UIViewAnimationOptions(rawValue: 7 << 16) // ↑
UIView.animate(withDuration: duration, delay: 0, options: curve, animations: ({ [weak self] in
self?.collectionView?.performBatchUpdates({
self?.collectionView?.deleteItems(at: deletedIPs)
self?.collectionView?.insertItems(at: insertedIPs)
self?.collectionView?.reloadItems(at: updatedIPs)
}, completion: nil)
}), completion: nil)
How can I animate a stack view to slide up starting from x=0 up to y=500, I have the following method in the viewDidLoad() which does a growing effect.
StackView.transform = CGAffineTransformMakeScale(0.0, 0.0)
And then I added a growing effect in the viewDidAppear() method
UIView.animateWithDuration(0.4, delay: 0.0, options: [], animations: {
self.StackView.transform = CGAffineTransformIdentity
}, completion: nil)
After the viewDidLoad method executes, the stack view is minimized. When the viewDidLoad method completes, the viewDidAppear method is invoked, and the animation begins and the stack view begins to grow. The animation stops when the stack view reaches it's original size.
Although is a nice effect that's not what I want to accomplish, I want the animation to slide up from x = 0 and stops at y = 500 I tried to add the following code in the viewDidLoad to accomplish this effect, but I still get the same growing effect. Any suggestions on how to accomplish this?
StackView.transform = CGAffineTransformMakeTranslation(0, 500)
You´re almost there just make a few changes
// These values depends on the positioning of your element
let left = CGAffineTransformMakeTranslation(-300, 0)
let right = CGAffineTransformMakeTranslation(300, 0)
let top = CGAffineTransformMakeTranslation(0, -300)
UIView.animateWithDuration(0.4, delay: 0.0, options: [], animations: {
// Add the transformation in this block
// self.container is your view that you want to animate
self.container.transform = top
}, completion: nil)
I need to switch between two subviews, for that I am using flip animation, but it flips whole screen not subview. Here is the code I used to flip:
UIView.transitionFromView(frontView, toView: backView, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromRight | UIViewAnimationOptions.ShowHideTransitionViews, completion: nil)
I have created frontView and backView in StoryBoard, and backView hidden initially.
Please help me too flip only the subviews.
finally i got fix for this, we need to add frontView and backView to another View(container) not for self.view, then container will flip.
This question is old, but it can help someone. If you want to flip your self.view use it:
Swift 2
UIView.transitionWithView(self.view, duration: 0.8, options: UIViewAnimationOptions.TransitionFlipFromRight | UIViewAnimationOptions.ShowHideTransitionViews, animations: {
self.view.addSubview(newView)
}, completion: { finished in
// HERE you can remove your old view
oldView.removeFromSuperview()
})
Swift 3, 4, 5
UIView.transition(with: self.view, duration: 0.8, options: [UIView.AnimationOptions.transitionFlipFromRight, UIView.AnimationOptions.showHideTransitionViews], animations: {
self.view.addSubview(newView)
}, completion: { finished in
// HERE you can remove your old view
oldView.removeFromSuperview()
})