-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from mateuszszklarek/swipe-support
Add support UISwipeGestureRecognizer in view
- Loading branch information
Showing
15 changed files
with
392 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
4.0 | ||
4.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import SnapKit | ||
import UIKit | ||
|
||
class LabeledView: UIView { | ||
|
||
let label: UILabel = Subviews.label | ||
|
||
init() { | ||
super.init(frame: .zero) | ||
|
||
addSubviews() | ||
setUpLayout() | ||
} | ||
|
||
// MARK: - Subviews | ||
|
||
private func addSubviews() { | ||
addSubview(label) | ||
} | ||
|
||
// MARK: - Layout | ||
|
||
private func setUpLayout() { | ||
label.snp.makeConstraints { | ||
$0.top.trailing.lessThanOrEqualToSuperview() | ||
$0.bottom.leading.lessThanOrEqualToSuperview() | ||
$0.center.equalToSuperview() | ||
} | ||
} | ||
|
||
// MARK: - Required initializer | ||
|
||
required init?(coder _: NSCoder) { return nil } | ||
|
||
} | ||
|
||
private extension LabeledView { | ||
|
||
enum Subviews { | ||
static var label: UILabel { | ||
let label = UILabel(frame: .zero) | ||
label.textColor = .white | ||
label.font = UIFont.systemFont(ofSize: 15.0, weight: .bold) | ||
label.textAlignment = .center | ||
|
||
return label | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import UIKit | ||
import SnapKit | ||
|
||
class SwipeView: UIView { | ||
|
||
let upView: UIView = Subviews.view(color: .green, text: "UP") | ||
let leftView: UIView = Subviews.view(color: .red, text: "LEFT") | ||
let rightView: UIView = Subviews.view(color: .blue, text: "RIGHT") | ||
let downView: UIView = Subviews.view(color: .gray, text: "DOWN") | ||
|
||
init() { | ||
super.init(frame: .zero) | ||
|
||
setUpSelf() | ||
addSubviews() | ||
setupLayout() | ||
} | ||
|
||
private func setUpSelf() { | ||
backgroundColor = .white | ||
} | ||
|
||
// MARK: Subviews | ||
|
||
private let stackView: UIStackView = Subviews.stackView | ||
|
||
private func addSubviews() { | ||
[upView, leftView, rightView, downView].forEach { | ||
stackView.addArrangedSubview($0) | ||
} | ||
|
||
addSubview(stackView) | ||
} | ||
|
||
// MARK: Layout | ||
|
||
private func setupLayout() { | ||
stackView.snp.makeConstraints { | ||
$0.edges.equalToSuperview() | ||
} | ||
|
||
[upView, leftView, rightView, downView].forEach { view in | ||
view.snp.makeConstraints { | ||
$0.width.equalToSuperview() | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Required initializer | ||
|
||
required init?(coder _: NSCoder) { return nil } | ||
|
||
} | ||
|
||
private extension SwipeView { | ||
|
||
enum Subviews { | ||
static func view(color: UIColor, text: String) -> LabeledView { | ||
let view = LabeledView() | ||
view.backgroundColor = color | ||
view.label.text = text | ||
|
||
return view | ||
} | ||
|
||
static var stackView: UIStackView { | ||
let stackView = UIStackView(frame: .zero) | ||
stackView.axis = .vertical | ||
stackView.alignment = .center | ||
stackView.distribution = .fillEqually | ||
stackView.spacing = 5 | ||
|
||
return stackView | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import UIKit | ||
|
||
class SwipeViewController: UIViewController { | ||
|
||
var didSwipeUp: (() -> Void)? | ||
var didSwipeLeft: (() -> Void)? | ||
var didSwipeRight: (() -> Void)? | ||
var didSwipeDown: (() -> Void)? | ||
|
||
var swipeView: SwipeView { return view as! SwipeView } | ||
|
||
// MARK: - Lifecycle | ||
|
||
override func loadView() { | ||
view = SwipeView() | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
setUpSwipeGestures() | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private func setUpSwipeGestures() { | ||
setUpSwipeGesture(direction: .up, action: #selector(swipedUp), in: swipeView.upView) | ||
setUpSwipeGesture(direction: .left, action: #selector(swipedLeft), in: swipeView.leftView) | ||
setUpSwipeGesture(direction: .right, action: #selector(swipedRight), in: swipeView.rightView) | ||
setUpSwipeGesture(direction: .down, action: #selector(swipedDown), in: swipeView.downView) | ||
} | ||
|
||
// MARK: Actions | ||
|
||
private func setUpSwipeGesture(direction: UISwipeGestureRecognizer.Direction, action: Selector?, in view: UIView) { | ||
let swipeGesture = UISwipeGestureRecognizer(target: self, action: action) | ||
swipeGesture.direction = direction | ||
view.addGestureRecognizer(swipeGesture) | ||
} | ||
|
||
@objc | ||
private func swipedUp() { | ||
print("SWIPED UP") | ||
didSwipeUp?() | ||
} | ||
|
||
@objc | ||
private func swipedLeft() { | ||
print("SWIPED LEFT") | ||
didSwipeLeft?() | ||
} | ||
|
||
@objc | ||
private func swipedRight() { | ||
print("SWIPED RIGHT") | ||
didSwipeRight?() | ||
} | ||
|
||
@objc | ||
private func swipedDown() { | ||
print("SWIPED DOWN") | ||
didSwipeDown?() | ||
} | ||
|
||
} |
Oops, something went wrong.