I'm presenting a view from another using a UIViewControllerTransitioningDelegate instance as transitioning delegate and modalPresentationStyle = Custom.
I'm using a TableViewController using static table view cells with UITextfields inside. Now when tapping over a text field whose borders are close to the the keyboard's frame, part of the tableView beneath the keyboard shows up. I also removed a UITapGestureRecognizer that I added to the semi-transparent background to make sure it's not part of the problem but the issue it's still there. Any ideas? Below is the animateTransition() method
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
dimmingView.backgroundColor = UIColor(white: 0.0, alpha: 0.5)
let containerView = transitionContext.containerView()
if let presentedViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) {
let presentedView = presentedViewController.view
let fromView = transitionContext.viewForKey(UITransitionContextFromViewControllerKey)
let centre = presentedView.center
if isPresenting {
presentedView.center = containerView.center
presentedView.frame = presentedView.bounds.rectByInsetting(dx: 30, dy: 150)
transitionContext.containerView().addSubview(presentedView)
dimmingView.frame = containerView.bounds
dimmingView.alpha = 0.0
containerView.insertSubview(dimmingView, atIndex: 0)
presentedViewController.transitionCoordinator()?.animateAlongsideTransition({
context in
self.dimmingView.alpha = 1.0
}, completion: nil)
}
else {
presentedViewController.transitionCoordinator()?.animateAlongsideTransition({
context in
self.dimmingView.alpha = 0.0
}, completion: {
context in
self.dimmingView.removeFromSuperview()
})
}
UIView.animateWithDuration(self.transitionDuration(transitionContext),
delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10.0, options: nil,
animations: {
presentedView.center = centre
}, completion: {
_ in
transitionContext.completeTransition(true)
})
}
}
Related
To provide the interaction pop gesture in full view, i have a UIPanGestureRecognizer in my controller with which we can swipe from left to right any where in the view controller to pop the controller, instead of using the default NavigationController pop gesture.
When i use the gesture with keyboard open in the view the keyboard also dismissing(not occurring with default NavigationController pop gesture) with the interaction, which looks weird.
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if operation == .pop {
return PopControllerTransition()//My transition
}
return nil
}
How can i prevent the keyboard dismiss while pop viewController with my custom pop transition.
//transition code
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
let fromController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
let toController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
toController.view.transform = CGAffineTransform(translationX: -(toController.view.bounds.width/3), y: 0)
containerView.insertSubview(toController.view, belowSubview: fromController.view)
let view = GradientView(frame: containerView.frame)
view.horizontal = true
view.backgroundColor = UIColor.clear
view.transform = CGAffineTransform(translationX: -toController.view.bounds.width, y: 0)
view.gradientLayer.colors = [UIColor(white: 0.0, alpha: 0.2).cgColor, UIColor(white: 0.0, alpha: 0.5).cgColor]
containerView.insertSubview(view, belowSubview: fromController.view)
let duration = transitionDuration(using: transitionContext)
UIView.animate(withDuration: duration, animations: {
toController.view.transform = .identity
fromController.view.transform = CGAffineTransform(translationX: fromController.view.bounds.width, y: 0)
view.transform = .identity
view.alpha = 0.0
}) { (finish) in
if !transitionContext.transitionWasCancelled {
fromController.view.removeFromSuperview()
}
view.removeFromSuperview()
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
So I noticed that keyboard transition is different on iOS 13 compared to iOS 12 so I had to do it manually like that:
let animations: () -> Void = {
fromView.frame.origin.x += fromView.bounds.width
toView.frame.origin.x += fromView.bounds.width
// Keyboard behavior changed on iOS 13. It dismisses ignoring transition
// so need to fix that manually.
if #available(iOS 13.0, *) {
let keyboardWindow = UIApplication.shared.windows
.first { String(describing: type(of: $0)) == "UIRemoteKeyboardWindow" }
if let keyboardWindow = keyboardWindow, let keyboardImage = keyboardWindow.getImage() {
let imageView = UIImageView(image: keyboardImage)
keyboardWindow.addSubview(imageView)
keyboardWindow.frame.origin.x += keyboardWindow.frame.width
}
}
}
You can check implementation I did - https://github.com/APUtils/Animators/blob/master/Animators/Classes/Right%20Slide%20Animation/RightSlideOutAnimator.swift
I am creating a custom transition for my app but when I try to create a snapshot of the destination view it always appears as a blank white rectangle. It is important I note that this is a custom push transition and not a modal presentation of the view. When presenting modally the snapshot appears to work. Is this the normal behaviour for custom push/pop transitions?
The code I've written is as follows:
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let toViewController = transitionContext.viewController(forKey: .to) as? CultureViewController else {
return
}
let containerView = transitionContext.containerView
let finalFrame = transitionContext.finalFrame(for: toViewController)
let snapshot = toViewController.view.snapshotView(afterScreenUpdates: true)
snapshot?.frame = UIScreen.main.bounds
containerView.addSubview(snapshot!)
toViewController.view.transform = CGAffineTransform(translationX: 0, y: toViewController.view.bounds.height)
//containerView.addSubview(toViewController.view)
let duration = transitionDuration(using: transitionContext)
UIView.animateKeyframes(withDuration: duration, delay: 0, options: .calculationModeCubic, animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1, animations: {
toViewController.view.frame = finalFrame
})
}, completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
Taking a snapshot of a view before it is rendered, such as is the case with a transition before you call containerView.addSubview(toViewController.view), returns a blank view, per UIView's documentation.
If the current view is not yet rendered, perhaps because it is not yet onscreen, the snapshot view has no visible content.
I saw a tutorial on Appcoda Transition ViewControllers transition a menu from up to bottom and I implemented it. Then, I tried to transition from bottom up using UIViewControllerContextTransitioning. But, doing it wrong cause I was setting the wrong values I think. Below is the code
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
//Get reference to our fromView, toView and the container view
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
//Setup the transform for sliding
let container = transitionContext.containerView()
let height = container?.frame.height
let moveDown = CGAffineTransformMakeTranslation(0, height! - 150)
let moveUp = CGAffineTransformMakeTranslation(0, -50)
//Add both views to the container view
if isPresenting {
toView?.transform = moveUp
snapShot = fromView?.snapshotViewAfterScreenUpdates(true)
container?.addSubview(toView!)
container?.addSubview(snapShot!)
}
//Perform the animation
UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.3, options: UIViewAnimationOptions(rawValue: 0), animations: {
if self.isPresenting {
self.snapShot?.transform = moveDown
toView?.transform = CGAffineTransformIdentity
} else {
self.snapShot?.transform = CGAffineTransformIdentity
fromView?.transform = moveUp
}
}, completion: {finished in
transitionContext.completeTransition(true)
if !self.isPresenting {
self.snapShot?.removeFromSuperview()
}
})
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return duration
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
// Get reference to our fromView, toView and the container view
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
// Set up the transform we'll use in the animation
guard let container = transitionContext.containerView() else {
return
}
let moveUp = CGAffineTransformMakeTranslation(0, container.frame.height + 50)
let moveDown = CGAffineTransformMakeTranslation(0, -250)
// Add both views to the container view
if isPresenting {
toView.transform = moveUp
snapshot = fromView.snapshotViewAfterScreenUpdates(true)
container.addSubview(toView)
container.addSubview(snapshot!)
}
// Perform the animation
UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.8, options: [], animations: {
if self.isPresenting {
self.snapshot?.transform = moveDown
toView.transform = CGAffineTransformIdentity
} else {
self.snapshot?.transform = CGAffineTransformIdentity
fromView.transform = moveUp
}
}, completion: { finished in
transitionContext.completeTransition(true)
if !self.isPresenting {
self.snapshot?.removeFromSuperview()
}
})
}
This Should work. I checked out the tutorial you shared and you probably can't see the menu on the bottom because the way the MenuTableViewController.swift is set up on storyboard it is made so that the menu is always started from the top, so change that up and it should work perfectly fine. Let me know if you have any questions.
I tried to follow this question: Presenting a UINavigationController in a custom modal, but my navigation bar is shrinking from 64p to 44p after the animation. I'm using a custom UIPresentationController and UIViewControllerAnimatedTransitioning.
I think the problem is with the following code. When I comment out the animation it the navigation bar starts out and remains at 44p
func animatePresentationWithTransitionContext(transitionContext: UIViewControllerContextTransitioning) {
let presentedController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)! as DrawerNavigationController
let presentedControllerView = transitionContext.viewForKey(UITransitionContextToViewKey)!
let containerView = transitionContext.containerView()
var finalFrame = transitionContext.finalFrameForViewController(presentedController)
presentedControllerView.frame = CGRectOffset(finalFrame, 0, containerView.bounds.size.height)
containerView.addSubview(presentedControllerView)
UIView.animateWithDuration(self.duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: .AllowUserInteraction, animations: {
var height = presentedController.height()
presentedControllerView.frame.origin.y = containerView.bounds.size.height - height
}, completion: {(completed: Bool) -> Void in
transitionContext.completeTransition(true)
})
}
My custom segue animation works fine, but sometimes I see white-flashes/flickering during the animation. Any tips or suggestions on how to avoid this?
This is my code :
import UIKit
#objc(InsetBlurModalSeque) class InsetBlurModalSeque: UIStoryboardSegue {
override func perform() {
let sourceViewController = self.sourceViewController as UIViewController
let destinationViewController = self.destinationViewController as UIViewController
// Make sure the background is ransparent
destinationViewController.view.backgroundColor = UIColor.clearColor()
// Take screenshot from source VC
UIGraphicsBeginImageContext(sourceViewController.view.bounds.size)
sourceViewController.view.drawViewHierarchyInRect(sourceViewController.view.frame, afterScreenUpdates:true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// Blur screenshot
var blurredImage:UIImage = image.applyBlurWithRadius(5, tintColor: UIColor(white: 1.0, alpha: 0.0), saturationDeltaFactor: 1.3, maskImage: nil)
// Crop screenshot, add to view and send to back
let blurredBackgroundImageView : UIImageView = UIImageView(image:blurredImage)
blurredBackgroundImageView.clipsToBounds = true;
blurredBackgroundImageView.contentMode = UIViewContentMode.Center
let insets:UIEdgeInsets = UIEdgeInsetsMake(20, 20, 20, 20);
blurredBackgroundImageView.frame = UIEdgeInsetsInsetRect(blurredBackgroundImageView.frame, insets)
destinationViewController.view.addSubview(blurredBackgroundImageView)
destinationViewController.view.sendSubviewToBack(blurredBackgroundImageView)
// Add original screenshot behind blurred image
let backgroundImageView : UIImageView = UIImageView(image:image)
destinationViewController.view.addSubview(backgroundImageView)
destinationViewController.view.sendSubviewToBack(backgroundImageView)
// Add the destination view as a subview, temporarily – we need this do to the animation
sourceViewController.view.addSubview(destinationViewController.view)
// Set initial state of animation
destinationViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
blurredBackgroundImageView.alpha = 0.0;
backgroundImageView.alpha = 0.0;
// Animate
UIView.animateWithDuration(0.5,
delay: 0.0,
usingSpringWithDamping: 0.6,
initialSpringVelocity: 0.0,
options: UIViewAnimationOptions.CurveLinear,
animations: {
destinationViewController.view.transform = CGAffineTransformIdentity
blurredBackgroundImageView.alpha = 1.0
backgroundImageView.alpha = 1.0;
},
completion: { (fininshed: Bool) -> () in
// Remove from temp super view
destinationViewController.view.removeFromSuperview()
sourceViewController.presentViewController(destinationViewController, animated: false, completion: nil)
}
)
}
}
Thanks.