Skip to content

Commit

Permalink
fix a race condition with interactive transition
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Feb 12, 2017
1 parent f57b79e commit f71d664
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 27 deletions.
6 changes: 3 additions & 3 deletions Sources/Hero.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ internal extension Hero {
animate()
#else
if inNavigationController {
// When animating within navigationController, we have to delay a frame.
// otherwise snapshots will not be taken. Possibly a bug with UIKit
execute(after: 0.01666666667) {
// When animating within navigationController, we have to dispatch later into the main queue.
// otherwise snapshots will be pure white. Possibly a bug with UIKit
DispatchQueue.main.async {
self.animate()
}
} else {
Expand Down
28 changes: 4 additions & 24 deletions Sources/HeroBaseController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@

import UIKit

internal var heroOperationQueue: OperationQueue = {
let oq = OperationQueue()
oq.maxConcurrentOperationCount = 1
oq.name = "heroOperationQueue"
return oq
}()

/// Base class for managing a Hero transition
public class HeroBaseController: NSObject {
// MARK: Properties
Expand Down Expand Up @@ -143,7 +136,7 @@ public extension HeroBaseController {
*/
public func update(progress: Double) {
guard transitioning else { return }
execute {
DispatchQueue.main.async {
self.beginTime = nil
self.progress = max(0, min(1, progress))
}
Expand All @@ -156,7 +149,7 @@ public extension HeroBaseController {
*/
public func end(animate: Bool = true) {
guard transitioning && interactive else { return }
execute {
DispatchQueue.main.async {
if !animate {
self.complete(finished:true)
return
Expand All @@ -177,7 +170,7 @@ public extension HeroBaseController {
*/
public func cancel(animate: Bool = true) {
guard transitioning && interactive else { return }
execute {
DispatchQueue.main.async {
if !animate {
self.complete(finished:false)
return
Expand Down Expand Up @@ -205,7 +198,7 @@ public extension HeroBaseController {
*/
public func apply(modifiers: [HeroModifier], to view: UIView) {
guard transitioning && interactive else { return }
execute {
DispatchQueue.main.async {
let targetState = HeroTargetState(modifiers: modifiers)
if let otherView = self.context.pairedView(for: view) {
for animator in self.animators {
Expand Down Expand Up @@ -372,19 +365,6 @@ internal extension HeroBaseController {

completion?(finished)
}

func execute(after: TimeInterval = 0, block: @escaping () -> Void) {
if heroOperationQueue.operationCount > 0 || after > 0 {
heroOperationQueue.addOperation {
Thread.sleep(forTimeInterval: after)
DispatchQueue.main.async {
block()
}
}
} else {
block()
}
}
}

// MARK: Plugin Support
Expand Down

0 comments on commit f71d664

Please sign in to comment.