Skip to content

Commit 19ac960

Browse files
committed
Merge pull request #29 from MailOnline/ka/ProgramaticClose
Ka/Programatic close support
2 parents 32457b8 + c41313f commit 19ac960

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

ImageViewer/Source/GalleryViewController.swift

+19-9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ final public class GalleryViewController : UIPageViewController, UIViewControlle
6262
public var landedPageAtIndexCompletion: ((Int) -> Void)?
6363
/// If set, launched after all animations finish when the close button is pressed.
6464
public var closedCompletion: (() -> Void)?
65+
/// If set, launched after all animations finish when the close() method is invoked via public API.
66+
public var programaticallyClosedCompletion: (() -> Void)?
6567
/// If set, launched after all animations finish when the swipe-to-dismiss (applies to all directions and cases) gesture is used.
6668
public var swipedToDismissCompletion: (() -> Void)?
6769

@@ -190,7 +192,7 @@ final public class GalleryViewController : UIPageViewController, UIViewControlle
190192

191193
private func configureCloseButton() {
192194

193-
closeButton?.addTarget(self, action: #selector(GalleryViewController.close), forControlEvents: .TouchUpInside)
195+
closeButton?.addTarget(self, action: #selector(GalleryViewController.interactiveClose), forControlEvents: .TouchUpInside)
194196
}
195197

196198
func createViewHierarchy() {
@@ -337,7 +339,17 @@ final public class GalleryViewController : UIPageViewController, UIViewControlle
337339

338340
// MARK: - Actions
339341

340-
func close() {
342+
public func close() {
343+
344+
closeWithAnimation(programaticallyClosedCompletion)
345+
}
346+
347+
func interactiveClose() {
348+
349+
closeWithAnimation(closedCompletion)
350+
}
351+
352+
func closeWithAnimation(completion: (() -> Void)?) {
341353

342354
UIView.animateWithDuration(0.1, animations: { [weak self] in
343355

@@ -355,26 +367,24 @@ final public class GalleryViewController : UIPageViewController, UIViewControlle
355367

356368
imageController.closeAnimation(self?.closeAnimationDuration ?? 0.2, completion: { [weak self] finished in
357369

358-
self?.innerClose()
370+
self?.postAnimationClose(completion)
359371
})
360372
}
361373
}
362374
else {
363-
self?.innerClose()
375+
self?.postAnimationClose(completion)
364376
}
365-
366377
}
367378
}
368379

369-
func innerClose() {
380+
func postAnimationClose(completion: (() -> Void)?) {
370381

371382
self.modalTransitionStyle = .CrossDissolve
372-
self.dismissViewControllerAnimated(true) {
383+
self.dismissViewControllerAnimated(false) {
373384

374385
self.applicationWindow!.windowLevel = UIWindowLevelNormal
386+
completion?()
375387
}
376-
377-
closedCompletion?()
378388
}
379389

380390
// MARK: - Image Controller Delegate

0 commit comments

Comments
 (0)