Skip to content

Commit

Permalink
potentially fix a race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Feb 13, 2017
1 parent e2155e4 commit 9b060f2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class AppleProductViewController: UIViewController, HeroViewControllerDelegate {
case normal, slidingLeft, slidingRight
}
var state: TransitionState = .normal
weak var nextVC: AppleProductViewController?

func pan() {
let translateX = panGR.translation(in: nil).x
Expand All @@ -74,22 +75,22 @@ class AppleProductViewController: UIViewController, HeroViewControllerDelegate {
Hero.shared.cancel(animate: false)
let currentIndex = viewControllerIDs.index(of: self.title!)!
let nextIndex = (currentIndex + (nextState == .slidingLeft ? 1 : viewControllerIDs.count - 1)) % viewControllerIDs.count
let nextVC = self.storyboard!.instantiateViewController(withIdentifier: viewControllerIDs[nextIndex]) as! AppleProductViewController
nextVC = self.storyboard!.instantiateViewController(withIdentifier: viewControllerIDs[nextIndex]) as? AppleProductViewController

if nextState == .slidingLeft {
applyShrinkModifiers()
nextVC.applySlideModifiers()
nextVC!.applySlideModifiers()
} else {
applySlideModifiers()
nextVC.applyShrinkModifiers()
nextVC!.applyShrinkModifiers()
}
state = nextState
hero_replaceViewController(with: nextVC)
hero_replaceViewController(with: nextVC!)
} else {
let progress = abs(Double(translateX / view.bounds.width))
Hero.shared.update(progress: progress)
if state == .slidingLeft {
Hero.shared.apply(modifiers: [.translate(x: view.bounds.width + translateX)], to: Hero.shared.toViewController!.view)
if state == .slidingLeft, let nextVC = nextVC {
Hero.shared.apply(modifiers: [.translate(x: view.bounds.width + translateX)], to: nextVC.view)
} else {
Hero.shared.apply(modifiers: [.translate(x: translateX)], to: view)
}
Expand Down
60 changes: 26 additions & 34 deletions Sources/HeroBaseController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,8 @@ public extension HeroBaseController {
*/
public func update(progress: Double) {
guard transitioning else { return }
DispatchQueue.main.async {
self.beginTime = nil
self.progress = max(0, min(1, progress))
}
self.beginTime = nil
self.progress = max(0, min(1, progress))
}

/**
Expand All @@ -149,18 +147,16 @@ public extension HeroBaseController {
*/
public func end(animate: Bool = true) {
guard transitioning && interactive else { return }
DispatchQueue.main.async {
if !animate {
self.complete(finished:true)
return
}
var maxTime: TimeInterval = 0
for animator in self.animators {
maxTime = max(maxTime, animator.resume(timePassed:self.progress * self.totalDuration,
reverse: false))
}
self.complete(after: maxTime, finishing: true)
if !animate {
self.complete(finished:true)
return
}
var maxTime: TimeInterval = 0
for animator in self.animators {
maxTime = max(maxTime, animator.resume(timePassed:self.progress * self.totalDuration,
reverse: false))
}
self.complete(after: maxTime, finishing: true)
}

/**
Expand All @@ -170,18 +166,16 @@ public extension HeroBaseController {
*/
public func cancel(animate: Bool = true) {
guard transitioning && interactive else { return }
DispatchQueue.main.async {
if !animate {
self.complete(finished:false)
return
}
var maxTime: TimeInterval = 0
for animator in self.animators {
maxTime = max(maxTime, animator.resume(timePassed:self.progress * self.totalDuration,
reverse: true))
}
self.complete(after: maxTime, finishing: false)
if !animate {
self.complete(finished:false)
return
}
var maxTime: TimeInterval = 0
for animator in self.animators {
maxTime = max(maxTime, animator.resume(timePassed:self.progress * self.totalDuration,
reverse: true))
}
self.complete(after: maxTime, finishing: false)
}

/**
Expand All @@ -198,17 +192,15 @@ public extension HeroBaseController {
*/
public func apply(modifiers: [HeroModifier], to view: UIView) {
guard transitioning && interactive else { return }
DispatchQueue.main.async {
let targetState = HeroTargetState(modifiers: modifiers)
if let otherView = self.context.pairedView(for: view) {
for animator in self.animators {
animator.apply(state: targetState, to: otherView)
}
}
let targetState = HeroTargetState(modifiers: modifiers)
if let otherView = self.context.pairedView(for: view) {
for animator in self.animators {
animator.apply(state: targetState, to: view)
animator.apply(state: targetState, to: otherView)
}
}
for animator in self.animators {
animator.apply(state: targetState, to: view)
}
}
}

Expand Down

0 comments on commit 9b060f2

Please sign in to comment.