ios - 如何在 Swift IOS 中实现左右滑动以更改数据/视图?
问题描述
我想实现左右滑动手势功能以在 swift 5 中更改数据视图。我有两个 viewControllers -FeedsController
其中包含 tableview 和其中所有视频的列表。如果FeedsController
我点击评论按钮,它会移动到commentViewController
并显示带有详细评论的相同视频。实际上我想实现滑动手势动作,commentViewController
这样用户即使在详细评论页面上也可以向左滑动commentViewController
。
CommentViewController 的代码:
import UIKit
import FloatingPanel
final class CommentsViewController: UIViewController, CommentsViewInput, FloatingPanelControllerDelegate {
var post: IContentPost!
private var output: CommentsViewOutput?
private var fpc: FloatingPanelController!
private var mute: Bool {
return ASVideoPlayerController.sharedVideoPlayer.mute
}
private weak var contentVC: BottomSheetInputView?
private let router = Router()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
setup()
swipeGesture()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
fpc.removePanelFromParent(animated: true)
}
@IBAction func dismissAction(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
func swipeGesture(){
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
leftSwipe.direction = .left
rightSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
@objc func handleSwipes(_ sender:UISwipeGestureRecognizer)
{
if (sender.direction == .left)
{
print("Swipe Left")
}
if (sender.direction == .right)
{
print("Swipe Right")
}
}
private func setup() {
let presenter = CommentsPresentationModel(view: self, post: post)
self.output = presenter
setupTableView()
setupPanel()
output?.getComments() { [weak self] comments in
guard let comments = comments else { return }
self?.contentVC?.updateComments(with: comments)
}
}
private func setupPanel() {
fpc = FloatingPanelController()
fpc.delegate = self // Optional
let contentVC = router.bottomVC()
contentVC.delegate = self
self.contentVC = contentVC
fpc.set(contentViewController: contentVC)
fpc.track(scrollView: contentVC.tableView)
fpc.addPanel(toParent: self)
}
private func setupTableView() {
tableView.delegate = self
tableView.dataSource = self
tableView?.registerReusableCell(FeedContentTableViewCell.self)
}
}
extension CommentsViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: FeedContentTableViewCell = tableView.dequeueReusableCell(for: indexPath)
cell.delegate = self
cell.setup(with: post, mute: mute, needTopInset: false)
return cell
}
}
extension CommentsViewController: FeedContentCellDelegate {
func likePressed(id: String) {
output?.postLiked(postId:id)
}
func commentPressed(post: IContentPost) {
fpc.show(animated: true, completion: nil)
}
func sharePressed(id: String) {
output?.postShared(postId: id)
let text = "Hey! You gotta see that! https://lols.link/share?\(id)"
let textToShare = [text]
let activityViewController = UIActivityViewController(activityItems: textToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
func mutePressed() {
ASVideoPlayerController.sharedVideoPlayer.mute = !mute
}
}
extension CommentsViewController: BottomSheetDelegate {
func didCloseComments() {
fpc.hide(animated: true, completion: nil)
}
func didWriteComment(text: String) {
}
}
更新:
截屏:
解决方案
- 使用 2 种不同的功能来处理不同的滑动手势
- 添加以下内容
viewDidLoad()
高达 Swift 4.0
var swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedRight))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
var swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedLeft))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)
self.view.addGestureRecognizer(swipeRight)
Swift 4.2及更高版本:
“UISwipeGestureRecognizerDirection”已重命名为“UISwipeGestureRecognizer.Direction”
var swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedRight))
swipeRight.direction = UISwipeGestureRecognizer.Direction.right
var swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedLeft))
swipeLeft.direction = UISwipeGestureRecognizer.Direction.left
self.view.addGestureRecognizer(swipeLeft)
self.view.addGestureRecognizer(swipeRight)
- 创建两个 objc 函数
swipedRight
和swipedLeft
@objc func swipedRight()
{
// Add your record changing code here
}
@objc func swipedLeft()
{
// Add your record changing code here
}
推荐阅读
- date - TypeError:无法从未定义中读取属性“源”。(第 2 行,文件“代码”)
- c++ - 如何从 R 中调用已编译的 Cpp 程序?
- ios - 如何在 App 中访问 inApp 购买产品而不在代码端声明他们的标识符?
- css - 在 React 中创建平滑滚动(浏览器友好)
- python - “‘连接’层需要具有匹配形状的输入,连接轴除外。” 如何解决这个问题?
- c# - 如何在 Xamarin.iOS 中使用 #available / @available
- excel - 无需编辑 VBA 代码即可解决类型不匹配
- c - C TCP write() 和 read() 来自 main 中定义的套接字
- android - 如何在同一应用程序中使用两个用户(司机和骑手)的共享偏好来维护两个不同的会话?
- linux - 如何在多个文件上使用 awk?