63 lines
1.8 KiB
Swift
63 lines
1.8 KiB
Swift
//
|
|
// LargeImageInteractionController.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 9/1/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class LargeImageInteractionController: UIPercentDrivenInteractiveTransition {
|
|
|
|
var inProgress = false
|
|
var direction: CGFloat?
|
|
|
|
var shouldCompleteTransition = false
|
|
private weak var viewController: UIViewController!
|
|
|
|
init(viewController: UIViewController) {
|
|
super.init()
|
|
self.viewController = viewController
|
|
let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handleGesture(_:)))
|
|
panRecognizer.allowedScrollTypesMask = .all
|
|
viewController.view.addGestureRecognizer(panRecognizer)
|
|
}
|
|
|
|
@objc func handleGesture(_ recognizer: UIPanGestureRecognizer) {
|
|
let translation = recognizer.translation(in: recognizer.view!.superview!)
|
|
var progress = translation.y / 200
|
|
if let direction = direction {
|
|
progress *= direction
|
|
} else {
|
|
direction = progress > 0 ? 1 : -1
|
|
progress = abs(progress)
|
|
}
|
|
progress = min(max(progress, 0), 1)
|
|
let velocity = abs(recognizer.velocity(in: recognizer.view!.superview!).y)
|
|
|
|
switch recognizer.state {
|
|
case .began:
|
|
inProgress = true
|
|
viewController.dismiss(animated: true)
|
|
case .changed:
|
|
shouldCompleteTransition = progress > 0.5 || velocity > 1000
|
|
update(progress)
|
|
case .cancelled:
|
|
inProgress = false
|
|
cancel()
|
|
case .ended:
|
|
inProgress = false
|
|
direction = nil
|
|
if shouldCompleteTransition {
|
|
finish()
|
|
} else {
|
|
cancel()
|
|
}
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
}
|