From a32dae6c29f7b04fea174c01882d6fd59f272fbb Mon Sep 17 00:00:00 2001 From: Paolo Date: Sun, 2 Dec 2018 17:10:35 +0100 Subject: [PATCH] Animation Glitches fix Animation glitches ios 12 Added simple logging (card.isDebug=true) --- Cards.podspec | 2 +- Cards/Sources/Animator.swift | 52 ++- Cards/Sources/Card.swift | 27 +- Cards/Sources/CardHighlight.swift | 2 +- Cards/Sources/DetailViewController.swift | 4 + Cards/Sources/LayoutHelper.swift | 8 +- .../xcschemes/xcschememanagement.plist | 5 + .../xcdebugger/Breakpoints_v2.xcbkptlist | 300 ++++++++++++++---- 8 files changed, 295 insertions(+), 105 deletions(-) diff --git a/Cards.podspec b/Cards.podspec index d5ae011..5247bdb 100644 --- a/Cards.podspec +++ b/Cards.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'Cards' - s.version = '1.3.4' + s.version = '1.3.5' s.summary = 'Awesome iOS 11 appstore cards in swift 4.' s.homepage = 'https://github.com/PaoloCuscela/Cards' s.screenshots = 'https://raw.githubusercontent.com/PaoloCuscela/Cards/master/Images/Header.png', 'https://raw.githubusercontent.com/PaoloCuscela/Cards/master/Images/DetailView.gif' diff --git a/Cards/Sources/Animator.swift b/Cards/Sources/Animator.swift index 72c6a09..07b2c7a 100644 --- a/Cards/Sources/Animator.swift +++ b/Cards/Sources/Animator.swift @@ -30,10 +30,9 @@ class Animator: NSObject, UIViewControllerAnimatedTransitioning { container.addSubview(to.view) container.addSubview(from.view) - guard presenting else { - // Detail View Controller Dismiss Animations + card.log("ANIMATOR>> Begin dismiss transition.") card.isPresenting = false let detailVC = from as! DetailViewController @@ -72,42 +71,37 @@ class Animator: NSObject, UIViewControllerAnimatedTransitioning { } // Detail View Controller Present Animations + card.log("ANIMATOR>> Begin present transition.") card.isPresenting = true let detailVC = to as! DetailViewController - let bounce = self.bounceTransform(card.originalFrame, to: card.backgroundIV.frame) - - container.bringSubviewToFront(detailVC.view) + let bounceOffset = self.bounceTransform(card.originalFrame, to: card.backgroundIV.frame) + container.bringSubview(toFront: detailVC.view) detailVC.card = card - detailVC.layout(card.originalFrame, isPresenting: false) - // Blur and fade with completion - UIView.animate(withDuration: velocity, delay: 0, options: .curveEaseOut, animations: { - - self.card.transform = CGAffineTransform.identity // Reset card identity after push back on tap - detailVC.blurView.alpha = 1 - detailVC.snap.alpha = 1 - self.card.backgroundIV.layer.cornerRadius = 0 - - }, completion: { _ in - - detailVC.layout(self.card.originalFrame, isPresenting: true, isAnimating: false, transform: .identity) - transitionContext.completeTransition(true) - }) + self.card.delegate?.cardIsShowingDetail?(card: self.card) - // Layout with bounce effect - UIView.animate(withDuration: velocity/2, delay: 0, options: .curveEaseOut, animations: { - - detailVC.layout(detailVC.view.frame, isPresenting: true, transform: bounce) - self.card.delegate?.cardIsShowingDetail?(card: self.card) + // This code should actually be executed outside the animation but idk why this first animation is executed and skipped ( even with 'duration' setted up ) + UIView.animate(withDuration: 0, delay: 0, options: .curveEaseIn, animations: { + // Setting detail VC in dismissed mode (VC frame = card frame) + detailVC.layout(self.card.originalFrame, isPresenting: false) - }) { _ in UIView.animate(withDuration: self.velocity/2, delay: 0, options: .curveEaseOut, animations: { - - detailVC.layout(detailVC.view.frame, isPresenting: true) - self.card.delegate?.cardIsShowingDetail?(card: self.card) + }, completion: { finished in UIView.animate(withDuration: self.velocity/2, delay: 0, options: .curveEaseIn, animations: { + detailVC.blurView.alpha = 1 + detailVC.snap.alpha = 1 + detailVC.layout(detailVC.view.frame, isPresenting: true, transform: bounceOffset) + }, completion: { (_) in UIView.animate(withDuration: self.velocity/2, delay: 0, options: .curveEaseIn, animations: { + detailVC.layout(detailVC.view.frame, isPresenting: true) + + }, completion: { (_) in + detailVC.layout(detailVC.view.frame, isPresenting: true) + transitionContext.completeTransition(true) + + }) }) - } + }) + } diff --git a/Cards/Sources/Card.swift b/Cards/Sources/Card.swift index 30284f8..3a10513 100644 --- a/Cards/Sources/Card.swift +++ b/Cards/Sources/Card.swift @@ -79,7 +79,7 @@ import UIKit */ @IBInspectable public var contentInset: CGFloat = 6 { didSet { - insets = LayoutHelper(rect: originalFrame).X(contentInset) + insets = LayoutHelper(rect: frame).X(contentInset) } } /** @@ -114,6 +114,10 @@ import UIKit else if !hasParallax && !motionEffects.isEmpty { motionEffects.removeAll() } } } + /** + If the card should print debug logs. + */ + public var isDebug: Bool = false /** Delegate for the card. Should extend your VC with CardDelegate. */ @@ -141,6 +145,7 @@ import UIKit } open func initialize() { + log("CARD: Initializing Card") // Tap gesture init self.addGestureRecognizer(tap) @@ -162,7 +167,6 @@ import UIKit override open func draw(_ rect: CGRect) { super.draw(rect) - originalFrame = rect self.layer.shadowOpacity = shadowOpacity self.layer.shadowColor = shadowColor.cgColor @@ -190,19 +194,18 @@ import UIKit @objc func cardTapped() { self.delegate?.cardDidTapInside?(card: self) + resetAnimated() if let vc = superVC { + log("CARD: Card tapped, Presenting DetailViewController") vc.present(self.detailVC, animated: true, completion: nil) - } else { - - resetAnimated() } } //MARK: - Animations - private func pushBackAnimated() { + private func shrinkAnimated() { UIView.animate(withDuration: 0.2, animations: { self.transform = CGAffineTransform(scaleX: 0.95, y: 0.95) }) } @@ -261,15 +264,23 @@ extension Card: UIGestureRecognizerDelegate { if let superview = self.superview { originalFrame = superview.convert(self.frame, to: nil) + log("CARD: Card's touch began, setting original frame to ---> \(originalFrame)" ) } - - pushBackAnimated() + shrinkAnimated() } } //MARK: - Helpers +extension Card { + + public func log(_ message: String){ + if self.isDebug { print(message) } + } + +} + extension UILabel { func lineHeight(_ height: CGFloat) { diff --git a/Cards/Sources/CardHighlight.swift b/Cards/Sources/CardHighlight.swift index e0c38f7..8104da7 100644 --- a/Cards/Sources/CardHighlight.swift +++ b/Cards/Sources/CardHighlight.swift @@ -173,7 +173,7 @@ import UIKit height: gimme.Y(25)) titleLbl.frame.origin = CGPoint(x: insets, y: gimme.Y(5, from: iconIV)) - titleLbl.frame.size.width = (originalFrame.width * 0.65) + ((backgroundIV.bounds.width - originalFrame.width)/3) + titleLbl.frame.size.width = (frame.width * 0.65) + ((backgroundIV.bounds.width - frame.width)/3) titleLbl.frame.size.height = gimme.Y(35) itemSubtitleLbl.sizeToFit() diff --git a/Cards/Sources/DetailViewController.swift b/Cards/Sources/DetailViewController.swift index dfa0bf0..77a6367 100644 --- a/Cards/Sources/DetailViewController.swift +++ b/Cards/Sources/DetailViewController.swift @@ -32,6 +32,7 @@ internal class DetailViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() + card.log("DetailVC: Loaded") if #available(iOS 11.0, *) { scrollView.contentInsetAdjustmentBehavior = .never @@ -117,6 +118,7 @@ internal class DetailViewController: UIViewController { //MARK: - Layout & Animations for the content ( rect = Scrollview + card + detail ) func layout(_ rect: CGRect, isPresenting: Bool, isAnimating: Bool = true, transform: CGAffineTransform = CGAffineTransform.identity){ + self.card.log("DetailVC>> Will layout to: ---> \(rect)") // Layout for dismiss guard isPresenting else { @@ -130,9 +132,11 @@ internal class DetailViewController: UIViewController { // Layout for present in fullscreen if isFullscreen { + scrollView.layer.cornerRadius = 0 scrollView.frame = view.bounds scrollView.frame.origin.y = 0 + self.card.backgroundIV.layer.cornerRadius = 0 // Layout for present in non-fullscreen } else { diff --git a/Cards/Sources/LayoutHelper.swift b/Cards/Sources/LayoutHelper.swift index c824b31..26b048b 100644 --- a/Cards/Sources/LayoutHelper.swift +++ b/Cards/Sources/LayoutHelper.swift @@ -45,22 +45,22 @@ open class LayoutHelper { return from.frame.minY - Y(percentage) - height } - static open func Width(_ percentage: CGFloat, of view: UIView) -> CGFloat { + static public func Width(_ percentage: CGFloat, of view: UIView) -> CGFloat { return view.frame.width * (percentage / 100) } - static open func Height(_ percentage: CGFloat, of view: UIView) -> CGFloat { + static public func Height(_ percentage: CGFloat, of view: UIView) -> CGFloat { return view.frame.height * (percentage / 100) } - static open func XScreen(_ percentage: CGFloat) -> CGFloat { + static public func XScreen(_ percentage: CGFloat) -> CGFloat { return percentage * UIScreen.main.bounds.height / 100 } - static open func YScreen(_ percentage: CGFloat) -> CGFloat { + static public func YScreen(_ percentage: CGFloat) -> CGFloat { return percentage * UIScreen.main.bounds.width / 100 diff --git a/Demo/Demo.xcodeproj/xcuserdata/paolocuscela.xcuserdatad/xcschemes/xcschememanagement.plist b/Demo/Demo.xcodeproj/xcuserdata/paolocuscela.xcuserdatad/xcschemes/xcschememanagement.plist index 83780ff..555ccd4 100644 --- a/Demo/Demo.xcodeproj/xcuserdata/paolocuscela.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Demo/Demo.xcodeproj/xcuserdata/paolocuscela.xcuserdatad/xcschemes/xcschememanagement.plist @@ -9,6 +9,11 @@ orderHint 3 + Demo.xcscheme_^#shared#^_ + + orderHint + 3 + diff --git a/Demo/Demo.xcworkspace/xcuserdata/paolocuscela.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Demo/Demo.xcworkspace/xcuserdata/paolocuscela.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 2f1d1d1..d89ba65 100644 --- a/Demo/Demo.xcworkspace/xcuserdata/paolocuscela.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Demo/Demo.xcworkspace/xcuserdata/paolocuscela.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -90,11 +90,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "../Cards/Sources/Card.swift" - timestampString = "555086835.8593301" + timestampString = "565458651.055328" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "149" - endingLineNumber = "149" + startingLineNumber = "155" + endingLineNumber = "155" landmarkName = "initialize()" landmarkType = "7"> @@ -186,11 +186,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "../Cards/Sources/Card.swift" - timestampString = "555086835.85947" + timestampString = "565458651.055928" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "187" - endingLineNumber = "187" + startingLineNumber = "193" + endingLineNumber = "193" landmarkName = "Card" landmarkType = "3"> @@ -234,11 +234,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "../Cards/Sources/Card.swift" - timestampString = "555086835.859544" + timestampString = "565458651.056268" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "186" - endingLineNumber = "186" + startingLineNumber = "192" + endingLineNumber = "192" landmarkName = "Card" landmarkType = "3"> @@ -378,11 +378,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "../Cards/Sources/Card.swift" - timestampString = "555086835.8596489" + timestampString = "565458651.05664" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "192" - endingLineNumber = "192" + startingLineNumber = "198" + endingLineNumber = "198" landmarkName = "cardTapped()" landmarkType = "7"> @@ -490,13 +490,13 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "../Cards/Sources/Card.swift" - timestampString = "555086835.859755" + timestampString = "565458651.057012" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "245" - endingLineNumber = "245" - landmarkName = "Card" - landmarkType = "21"> + startingLineNumber = "252" + endingLineNumber = "252" + landmarkName = "unknown" + landmarkType = "0"> @@ -744,12 +744,12 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "../Cards/Sources/DetailViewController.swift" - timestampString = "556634741.7506551" + timestampString = "565458651.057533" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "133" - endingLineNumber = "133" - landmarkName = "layout(_:isPresenting:isAnimating:transform:)" + startingLineNumber = "87" + endingLineNumber = "87" + landmarkName = "viewDidAppear(_:)" landmarkType = "7"> @@ -760,12 +760,28 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "../Cards/Sources/DetailViewController.swift" - timestampString = "556634741.750713" + timestampString = "565458651.0575809" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "86" - endingLineNumber = "86" - landmarkName = "viewDidAppear(_:)" + startingLineNumber = "218" + endingLineNumber = "218" + landmarkName = "XButton" + landmarkType = "3"> + + + + @@ -776,13 +792,13 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "../Cards/Sources/DetailViewController.swift" - timestampString = "556634741.750765" + timestampString = "565458651.057676" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "211" - endingLineNumber = "211" - landmarkName = "unknown" - landmarkType = "0"> + startingLineNumber = "127" + endingLineNumber = "127" + landmarkName = "layout(_:isPresenting:isAnimating:transform:)" + landmarkType = "7"> + + + + + + + + + + + + @@ -807,13 +871,13 @@ shouldBeEnabled = "No" ignoreCount = "0" continueAfterRunningActions = "No" - filePath = "../Cards/Sources/DetailViewController.swift" - timestampString = "556634741.750869" + filePath = "../Cards/Sources/Card.swift" + timestampString = "565458651.058744" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "126" - endingLineNumber = "126" - landmarkName = "layout(_:isPresenting:isAnimating:transform:)" + startingLineNumber = "270" + endingLineNumber = "270" + landmarkName = "touchesBegan(_:with:)" landmarkType = "7"> @@ -823,13 +887,13 @@ shouldBeEnabled = "No" ignoreCount = "0" continueAfterRunningActions = "No" - filePath = "../Cards/Sources/DetailViewController.swift" - timestampString = "556634741.750922" + filePath = "../Cards/Sources/CardHighlight.swift" + timestampString = "565457643.529278" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "146" - endingLineNumber = "146" - landmarkName = "layout(_:isPresenting:isAnimating:transform:)" + startingLineNumber = "170" + endingLineNumber = "170" + landmarkName = "layout(animating:)" landmarkType = "7"> @@ -839,16 +903,112 @@ shouldBeEnabled = "No" ignoreCount = "0" continueAfterRunningActions = "No" - filePath = "../Cards/Sources/Animator.swift" - timestampString = "556634741.7509741" + filePath = "../Cards/Sources/Card.swift" + timestampString = "565458651.0590219" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "100" - endingLineNumber = "100" - landmarkName = "animateTransition(using:)" + startingLineNumber = "246" + endingLineNumber = "246" + landmarkName = "animationController(forDismissed:)" landmarkType = "7"> + + + + + + + + + + + + + + + + + + + + + + + +