This commit is contained in:
Ali
2020-02-19 18:22:42 +04:00
parent 7c7b91194d
commit a5899ef4e7
7 changed files with 133 additions and 69 deletions

View File

@@ -76,8 +76,9 @@ public final class GalleryPagerNode: ASDisplayNode, UIScrollViewDelegate, UIGest
private let leftFadeNode: ASImageNode
private let rightFadeNode: ASImageNode
private var highlightedSide: Bool?
private var pressGestureRecognizer: UILongPressGestureRecognizer?
private var tapRecognizer: TapLongTapOrDoubleTapGestureRecognizer?
public private(set) var items: [GalleryItem] = []
private var itemNodes: [GalleryItemNode] = []
@@ -139,46 +140,82 @@ public final class GalleryPagerNode: ASDisplayNode, UIScrollViewDelegate, UIGest
public override func didLoad() {
super.didLoad()
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.pressGesture(_:)))
gestureRecognizer.delegate = self
gestureRecognizer.minimumPressDuration = 0.01
self.view.addGestureRecognizer(gestureRecognizer)
self.pressGestureRecognizer = gestureRecognizer
let recognizer = TapLongTapOrDoubleTapGestureRecognizer(target: self, action: #selector(self.tapLongTapOrDoubleTapGesture(_:)))
recognizer.delegate = self
self.tapRecognizer = recognizer
recognizer.tapActionAtPoint = { _ in
return .keepWithSingleTap
}
recognizer.highlight = { [weak self] point in
guard let strongSelf = self else {
return
}
let size = strongSelf.bounds
var highlightedSide: Bool?
if let point = point {
if point.x < size.width * 1.0 / 5.0 {
if strongSelf.items.count > 1 {
highlightedSide = false
}
} else {
if strongSelf.items.count > 1 {
highlightedSide = true
}
}
}
if strongSelf.highlightedSide != highlightedSide {
strongSelf.highlightedSide = highlightedSide
let leftAlpha: CGFloat
let rightAlpha: CGFloat
if let highlightedSide = highlightedSide {
leftAlpha = highlightedSide ? 0.0 : 1.0
rightAlpha = highlightedSide ? 1.0 : 0.0
} else {
leftAlpha = 0.0
rightAlpha = 0.0
}
if strongSelf.leftFadeNode.alpha != leftAlpha {
strongSelf.leftFadeNode.alpha = leftAlpha
if leftAlpha.isZero {
strongSelf.leftFadeNode.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.16, timingFunction: kCAMediaTimingFunctionSpring)
} else {
strongSelf.leftFadeNode.layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.08)
}
}
if strongSelf.rightFadeNode.alpha != rightAlpha {
strongSelf.rightFadeNode.alpha = rightAlpha
if rightAlpha.isZero {
strongSelf.rightFadeNode.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.16, timingFunction: kCAMediaTimingFunctionSpring)
} else {
strongSelf.rightFadeNode.layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.08)
}
}
}
}
self.view.addGestureRecognizer(recognizer)
}
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
@objc private func pressGesture(_ gestureRecognizer: UILongPressGestureRecognizer) {
let edgeWidth: CGFloat = 44.0
let location = gestureRecognizer.location(in: gestureRecognizer.view)
switch gestureRecognizer.state {
case .began:
let transition: ContainedViewLayoutTransition = .animated(duration: 0.07, curve: .easeInOut)
if location.x < edgeWidth && self.canGoToPreviousItem() {
transition.updateAlpha(node: self.leftFadeNode, alpha: 1.0)
} else if location.x > self.frame.width - edgeWidth && self.canGoToNextItem() {
transition.updateAlpha(node: self.rightFadeNode, alpha: 1.0)
@objc private func tapLongTapOrDoubleTapGesture(_ recognizer: TapLongTapOrDoubleTapGestureRecognizer) {
switch recognizer.state {
case .ended:
if let (gesture, location) = recognizer.lastRecognizedGestureAndLocation {
if case .tap = gesture {
let size = self.bounds.size
if location.x < size.width * 1.0 / 5.0 && self.canGoToPreviousItem() {
self.goToPreviousItem()
} else if self.canGoToNextItem() {
self.goToNextItem()
}
}
case .ended:
let transition: ContainedViewLayoutTransition = .animated(duration: 0.1, curve: .easeInOut)
if location.x < edgeWidth && self.canGoToPreviousItem() {
transition.updateAlpha(node: self.leftFadeNode, alpha: 0.0)
self.goToPreviousItem()
} else if location.x > self.frame.width - edgeWidth && self.canGoToNextItem() {
transition.updateAlpha(node: self.rightFadeNode, alpha: 0.0)
self.goToNextItem()
}
case .cancelled:
let transition: ContainedViewLayoutTransition = .animated(duration: 0.1, curve: .easeInOut)
if location.x < edgeWidth {
transition.updateAlpha(node: self.leftFadeNode, alpha: 0.0)
} else if location.x > self.frame.width - edgeWidth {
transition.updateAlpha(node: self.rightFadeNode, alpha: 0.0)
}
default:
break
}
default:
break
}
}