Skip to content

Commit

Permalink
Fix safeArea not being respected when generating snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
ppamorim committed May 2, 2020
1 parent a437e8b commit 4807ac9
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 86 deletions.
2 changes: 1 addition & 1 deletion CircularRevealKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'CircularRevealKit'
s.version = '0.9.2'
s.version = '0.9.3'
s.summary = 'Circular reveal animations made easy'
s.homepage = 'https://github.com/T-Pro/CircularRevealKit'
s.description = 'This library was created to allow developers to implement the material design reveal effect.'
Expand Down
168 changes: 86 additions & 82 deletions CircularRevealKit/Classes/UICircularViewControllerExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,115 +115,119 @@ public extension UIViewController {
let fromViewController: UIViewController? = transactionContext.viewController(
forKey: UITransitionContextViewControllerKey.from)

if let toView: UIView = toViewController?.view,
let fromView: UIView = fromViewController?.view,
let toViewSnapshot: UIView = toView.snapshotView(afterScreenUpdates: true),
let fromViewSnapshot: UIView = fromView.snapshotView(afterScreenUpdates: true) {
guard let toView: UIView = toViewController?.view,
let fromView: UIView = fromViewController?.view else {
return
}

let fadeView: UIView? = self.buildFadeView(fadeColor, fromView.frame)

switch revealType {

case RevealType.reveal:
toView.isHidden = false
transactionContext.containerView.insertSubview(
toView,
aboveSubview: fromView)

fromViewSnapshot.isOpaque = true
fromViewSnapshot.isHidden = true
transactionContext.containerView.addSubview(fromViewSnapshot)
guard let toViewSnapshot: UIView = toView.snapshotView(afterScreenUpdates: true),
let fromViewSnapshot: UIView = fromView.snapshotView(afterScreenUpdates: true) else {
return
}

DispatchQueue.main.asyncAfter(deadline: .now()) {
let fadeView: UIView? = self.buildFadeView(fadeColor, fromView.frame)

if let fadeView: UIView = fadeView {
fadeView.alpha = 0.01
transactionContext.containerView.addSubview(fadeView)
}
switch revealType {

toViewSnapshot.isHidden = true
transactionContext.containerView.addSubview(toViewSnapshot)
case RevealType.reveal:

transactionContext.containerView.insertSubview(
toView,
aboveSubview: fromView)
fromViewSnapshot.isOpaque = true
fromViewSnapshot.isHidden = true
transactionContext.containerView.addSubview(fromViewSnapshot)

toViewSnapshot.layoutIfNeeded()
fromViewSnapshot.layoutIfNeeded()
DispatchQueue.main.asyncAfter(deadline: .now()) {

UIView.animate(withDuration: animationTime) {
fadeView?.alpha = 1.0
}
if let fadeView: UIView = fadeView {
fadeView.alpha = 0.01
transactionContext.containerView.addSubview(fadeView)
}

toViewSnapshot.drawAnimatedCircularMask(
startFrame: rect,
duration: animationTime,
revealType: revealType) { () -> Void in
toViewSnapshot.isHidden = true
transactionContext.containerView.addSubview(toViewSnapshot)

DispatchQueue.main.asyncAfter(deadline: .now()) {
completion(true)
transitionCompletion?()
fromViewSnapshot.removeFromSuperview()
fadeView?.removeFromSuperview()
toViewSnapshot.removeFromSuperview()
}
toViewSnapshot.layoutIfNeeded()
fromViewSnapshot.layoutIfNeeded()

}
UIView.animate(withDuration: animationTime) {
fadeView?.alpha = 1.0
}

fromViewSnapshot.isHidden = false
toViewSnapshot.isHidden = false
fadeView?.isHidden = false
toViewSnapshot.drawAnimatedCircularMask(
startFrame: rect,
duration: animationTime,
revealType: revealType) { () -> Void in

}
DispatchQueue.main.asyncAfter(deadline: .now()) {
completion(true)
transitionCompletion?()
fromViewSnapshot.removeFromSuperview()
fadeView?.removeFromSuperview()
toViewSnapshot.removeFromSuperview()
toView.isHidden = false
}

case RevealType.unreveal:
}

toViewSnapshot.isOpaque = true
toViewSnapshot.isHidden = true
transactionContext.containerView.addSubview(toViewSnapshot)
fromViewSnapshot.isHidden = false
toViewSnapshot.isHidden = false
fadeView?.isHidden = false

DispatchQueue.main.asyncAfter(deadline: .now()) {
}

if let fadeView: UIView = fadeView {
fadeView.alpha = 1.0
fadeView.isHidden = true
transactionContext.containerView.addSubview(fadeView)
}
case RevealType.unreveal:

fromViewSnapshot.isHidden = true
transactionContext.containerView.addSubview(fromViewSnapshot)
toViewSnapshot.isOpaque = true
toViewSnapshot.isHidden = true
transactionContext.containerView.addSubview(toViewSnapshot)

// toView.isHidden = true
transactionContext.containerView.insertSubview(
toView,
belowSubview: fromView)
DispatchQueue.main.asyncAfter(deadline: .now()) {

toViewSnapshot.layoutIfNeeded()
fromViewSnapshot.layoutIfNeeded()
if let fadeView: UIView = fadeView {
fadeView.alpha = 1.0
fadeView.isHidden = true
transactionContext.containerView.addSubview(fadeView)
}

UIView.animate(withDuration: animationTime) {
fadeView?.alpha = 0.01
}
fromViewSnapshot.isHidden = true
transactionContext.containerView.addSubview(fromViewSnapshot)

fromViewSnapshot.drawAnimatedCircularMask(
startFrame: rect,
duration: animationTime,
revealType: revealType) { () -> Void in
transactionContext.containerView.insertSubview(
toView,
belowSubview: fromView)

DispatchQueue.main.asyncAfter(deadline: .now()) {
completion(true)
transitionCompletion?()
fromViewSnapshot.removeFromSuperview()
fadeView?.removeFromSuperview()
toViewSnapshot.removeFromSuperview()
fromViewController?.view.removeFromSuperview()
toView.isHidden = false
}
toViewSnapshot.layoutIfNeeded()
fromViewSnapshot.layoutIfNeeded()

}
UIView.animate(withDuration: animationTime) {
fadeView?.alpha = 0.01
}

fromViewSnapshot.isHidden = false
toViewSnapshot.isHidden = false
fadeView?.isHidden = false
fromViewSnapshot.drawAnimatedCircularMask(
startFrame: rect,
duration: animationTime,
revealType: revealType) { () -> Void in

DispatchQueue.main.asyncAfter(deadline: .now()) {
completion(true)
transitionCompletion?()
fromViewSnapshot.removeFromSuperview()
fadeView?.removeFromSuperview()
toViewSnapshot.removeFromSuperview()
fromViewController?.view.removeFromSuperview()
toView.isHidden = false
}

}


fromViewSnapshot.isHidden = false
toViewSnapshot.isHidden = false
fadeView?.isHidden = false

}

}
Expand Down
94 changes: 94 additions & 0 deletions Example/CircularRevealKit/SecondViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ class SecondViewController: UIViewController {
return view
}()

internal lazy var navigationBar: UINavigationBar = {
let view = UINavigationBar()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.blue
let navigationItem: UINavigationItem = UINavigationItem()
navigationItem.title = "SecondViewController"
view.items = [navigationItem]
return view
}()

internal lazy var bottomBar: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.green
return view
}()

deinit {
print("Deinit SecondViewController")
}
Expand All @@ -44,6 +61,8 @@ class SecondViewController: UIViewController {
title = "SecondViewController"
view.backgroundColor = UIColor.white
view.addSubview(randomButton)
view.addSubview(navigationBar)
view.addSubview(bottomBar)
view.updateConstraintsIfNeeded()
}

Expand Down Expand Up @@ -80,6 +99,81 @@ class SecondViewController: UIViewController {
multiplier: 1,
constant: 0)]
view.addConstraints(constraints)

if #available(iOS 11, *) {
let guide = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
navigationBar.topAnchor.constraint(equalToSystemSpacingBelow: guide.topAnchor, multiplier: 1.0),
])
} else {
let standardSpacing: CGFloat = 8.0
NSLayoutConstraint.activate([
navigationBar.topAnchor.constraint(equalTo: topLayoutGuide.topAnchor, constant: standardSpacing),
])
}

let navigationBarConstraints = [
NSLayoutConstraint(
item: view!,
attribute: .left,
relatedBy: .equal,
toItem: navigationBar,
attribute: .left,
multiplier: 1,
constant: 0),
NSLayoutConstraint(
item: view!,
attribute: .right,
relatedBy: .equal,
toItem: navigationBar,
attribute: .right,
multiplier: 1,
constant: 0)
]

view.addConstraints(navigationBarConstraints)

if #available(iOS 11, *) {
let guide = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
bottomBar.bottomAnchor.constraint(equalToSystemSpacingBelow: guide.bottomAnchor, multiplier: 1.0),
])
} else {
let standardSpacing: CGFloat = 8.0
NSLayoutConstraint.activate([
bottomBar.bottomAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: standardSpacing),
])
}

let bottomBarConstraints = [
NSLayoutConstraint(
item: view!,
attribute: .left,
relatedBy: .equal,
toItem: bottomBar,
attribute: .left,
multiplier: 1,
constant: 0),
NSLayoutConstraint(
item: view!,
attribute: .right,
relatedBy: .equal,
toItem: bottomBar,
attribute: .right,
multiplier: 1,
constant: 0),
NSLayoutConstraint(
item: bottomBar,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: 44)
]

view.addConstraints(bottomBarConstraints)

}
super.updateViewConstraints()
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ platform :ios, '9.0'
use_frameworks!

target '<Your Target Name>' do
pod 'CircularRevealKit', '~> 0.9.2'
pod 'CircularRevealKit', '~> 0.9.3'
end
```

Expand All @@ -59,7 +59,7 @@ $ brew install carthage
To integrate CircularRevealKit into your Xcode project using Carthage, specify it in your `Cartfile`:

```ogdl
github "T-Pro/CircularRevealKit" ~> 0.9.2
github "T-Pro/CircularRevealKit" ~> 0.9.3
```

Run `carthage update` to build the framework and drag the built `CircularRevealKit.framework` into your Xcode project.
Expand Down

0 comments on commit 4807ac9

Please sign in to comment.