Skip to content

Commit

Permalink
Animation Glitches fix
Browse files Browse the repository at this point in the history
Animation glitches ios 12
Added simple logging (card.isDebug=true)
  • Loading branch information
PaoloCuscela committed Dec 2, 2018
1 parent 1a9955c commit a32dae6
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 105 deletions.
2 changes: 1 addition & 1 deletion Cards.podspec
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
52 changes: 23 additions & 29 deletions Cards/Sources/Animator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

})
})
}
})


}

Expand Down
27 changes: 19 additions & 8 deletions Cards/Sources/Card.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
/**
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -141,6 +145,7 @@ import UIKit
}

open func initialize() {
log("CARD: Initializing Card")

// Tap gesture init
self.addGestureRecognizer(tap)
Expand All @@ -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
Expand Down Expand Up @@ -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) })
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Cards/Sources/CardHighlight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions Cards/Sources/DetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions Cards/Sources/LayoutHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<key>orderHint</key>
<integer>3</integer>
</dict>
<key>Demo.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>3</integer>
</dict>
</dict>
</dict>
</plist>
Loading

0 comments on commit a32dae6

Please sign in to comment.