Swiftgram/submodules/Display/Source/ContextControllerSourceNode.swift
Ali 9948945f7b Merge commit '8341247b5df17485928a325cd314c18ad1b9f6f1' into bazel
# Conflicts:
#	.gitignore
#	BUCK
#	Makefile
#	Telegram/Telegram-iOS/Telegram-iOS-Hockeyapp.entitlements
#	submodules/AsyncDisplayKit/Source/ASDisplayNode.h
#	submodules/AsyncDisplayKit/Source/Private/ASDisplayNode+FrameworkPrivate.h
#	submodules/Display/Display/ListView.swift
#	submodules/Display/Display/Navigation/NavigationController.swift
#	submodules/Display/Display/NavigationBar.swift
#	submodules/Display/Display/NavigationButtonNode.swift
#	submodules/Display/Display/TabBarNode.swift
#	submodules/Display/Display/ViewController.swift
#	submodules/Display/Source/ContextContentContainerNode.swift
#	submodules/Display/Source/ContextContentSourceNode.swift
#	submodules/Display/Source/ContextControllerSourceNode.swift
#	submodules/Display/Source/ContextGesture.swift
#	submodules/TelegramUI/Sources/Resources/Animations/ChatListEmpty.tgs
#	submodules/TelegramUI/Sources/Resources/Animations/ChatListFilterEmpty.tgs
#	submodules/TelegramUI/TelegramUI/ChatController.swift
#	submodules/TelegramUI/TelegramUI/FetchVideoMediaResource.swift
#	submodules/ffmpeg/FFMpeg/FFMpegRemuxer.m
2020-02-26 20:35:26 +04:00

71 lines
3.0 KiB
Swift

import Foundation
import AsyncDisplayKit
import Display
public final class ContextControllerSourceNode: ASDisplayNode {
private var contextGesture: ContextGesture?
public var isGestureEnabled: Bool = true {
didSet {
self.contextGesture?.isEnabled = self.isGestureEnabled
}
}
public var activated: ((ContextGesture) -> Void)?
public var shouldBegin: ((CGPoint) -> Bool)?
public var customActivationProgress: ((CGFloat, ContextGestureTransition) -> Void)?
public var targetNodeForActivationProgress: ASDisplayNode?
public func cancelGesture() {
self.contextGesture?.cancel()
self.contextGesture?.isEnabled = false
self.contextGesture?.isEnabled = self.isGestureEnabled
}
override public func didLoad() {
super.didLoad()
let contextGesture = ContextGesture(target: self, action: nil)
self.contextGesture = contextGesture
self.view.addGestureRecognizer(contextGesture)
contextGesture.shouldBegin = { [weak self] point in
guard let strongSelf = self, !strongSelf.bounds.width.isZero else {
return false
}
return strongSelf.shouldBegin?(point) ?? true
}
contextGesture.activationProgress = { [weak self] progress, update in
guard let strongSelf = self, !strongSelf.bounds.width.isZero else {
return
}
if let customActivationProgress = strongSelf.customActivationProgress {
customActivationProgress(progress, update)
} else {
let targetNode = strongSelf.targetNodeForActivationProgress ?? strongSelf
let minScale: CGFloat = (strongSelf.bounds.width - 10.0) / strongSelf.bounds.width
let currentScale = 1.0 * (1.0 - progress) + minScale * progress
switch update {
case .update:
targetNode.layer.sublayerTransform = CATransform3DMakeScale(currentScale, currentScale, 1.0)
case .begin:
targetNode.layer.sublayerTransform = CATransform3DMakeScale(currentScale, currentScale, 1.0)
case let .ended(previousProgress):
let previousScale = 1.0 * (1.0 - previousProgress) + minScale * previousProgress
targetNode.layer.sublayerTransform = CATransform3DMakeScale(currentScale, currentScale, 1.0)
targetNode.layer.animateSpring(from: previousScale as NSNumber, to: currentScale as NSNumber, keyPath: "sublayerTransform.scale", duration: 0.5, delay: 0.0, initialVelocity: 0.0, damping: 90.0)
}
}
}
contextGesture.activated = { [weak self] gesture in
if let activated = self?.activated {
activated(gesture)
} else {
gesture.cancel()
}
}
contextGesture.isEnabled = self.isGestureEnabled
}
}