forked from SwipeCellKit/SwipeCellKit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSwipeTransitionLayout.swift
88 lines (71 loc) · 3.74 KB
/
SwipeTransitionLayout.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// SwipeTransitionLayout.swift
//
// Created by Jeremy Koch
// Copyright © 2017 Jeremy Koch. All rights reserved.
//
import UIKit
// MARK: - Layout Protocol
protocol SwipeTransitionLayout {
func container(view: UIView, didChangeVisibleWidthWithContext context: ActionsViewLayoutContext)
func layout(view: UIView, atIndex index: Int, with context: ActionsViewLayoutContext)
func visibleWidthsForViews(with context: ActionsViewLayoutContext) -> [CGFloat]
}
// MARK: - Layout Context
struct ActionsViewLayoutContext {
let numberOfActions: Int
let orientation: SwipeActionsOrientation
let contentSize: CGSize
let visibleWidth: CGFloat
let minimumButtonWidth: CGFloat
init(numberOfActions: Int, orientation: SwipeActionsOrientation, contentSize: CGSize = .zero, visibleWidth: CGFloat = 0, minimumButtonWidth: CGFloat = 0) {
self.numberOfActions = numberOfActions
self.orientation = orientation
self.contentSize = contentSize
self.visibleWidth = visibleWidth
self.minimumButtonWidth = minimumButtonWidth
}
static func newContext(for actionsView: SwipeActionsView) -> ActionsViewLayoutContext {
return ActionsViewLayoutContext(numberOfActions: actionsView.actions.count,
orientation: actionsView.orientation,
contentSize: actionsView.contentSize,
visibleWidth: actionsView.visibleWidth,
minimumButtonWidth: actionsView.minimumButtonWidth)
}
}
// MARK: - Supported Layout Implementations
class BorderTransitionLayout: SwipeTransitionLayout {
func container(view: UIView, didChangeVisibleWidthWithContext context: ActionsViewLayoutContext) {
}
func layout(view: UIView, atIndex index: Int, with context: ActionsViewLayoutContext) {
let diff = context.visibleWidth - context.contentSize.width
view.frame.origin.x = (CGFloat(index) * context.contentSize.width / CGFloat(context.numberOfActions) + diff) * context.orientation.scale
}
func visibleWidthsForViews(with context: ActionsViewLayoutContext) -> [CGFloat] {
let diff = context.visibleWidth - context.contentSize.width
let visibleWidth = context.contentSize.width / CGFloat(context.numberOfActions) + diff
// visible widths are all the same regardless of the action view position
return (0..<context.numberOfActions).map({ _ in visibleWidth })
}
}
class DragTransitionLayout: SwipeTransitionLayout {
func container(view: UIView, didChangeVisibleWidthWithContext context: ActionsViewLayoutContext) {
view.bounds.origin.x = (context.contentSize.width - context.visibleWidth) * context.orientation.scale
}
func layout(view: UIView, atIndex index: Int, with context: ActionsViewLayoutContext) {
view.frame.origin.x = (CGFloat(index) * context.minimumButtonWidth) * context.orientation.scale
}
func visibleWidthsForViews(with context: ActionsViewLayoutContext) -> [CGFloat] {
return (0..<context.numberOfActions)
.map({ max(0, min(context.minimumButtonWidth, context.visibleWidth - (CGFloat($0) * context.minimumButtonWidth))) })
}
}
class RevealTransitionLayout: DragTransitionLayout {
override func container(view: UIView, didChangeVisibleWidthWithContext context: ActionsViewLayoutContext) {
let width = context.minimumButtonWidth * CGFloat(context.numberOfActions)
view.bounds.origin.x = (width - context.visibleWidth) * context.orientation.scale
}
override func visibleWidthsForViews(with context: ActionsViewLayoutContext) -> [CGFloat] {
return super.visibleWidthsForViews(with: context).reversed()
}
}