如何实现左右滑动以更改 swift IOS 中的数据/视图?
Posted
技术标签:
【中文标题】如何实现左右滑动以更改 swift IOS 中的数据/视图?【英文标题】:How can I implement Swipe left and right to change data/views in swift IOS? 【发布时间】:2019-11-30 12:36:27 【问题描述】:我想实现左右滑动手势功能以在 swift 5 中更改数据视图。
我有两个 viewControllers - FeedsController
其中包含 tableview 和其中所有视频的列表。在FeedsController
中,如果我点击评论按钮,它会移动到commentViewController
并显示与cmets 相同的视频的详细信息。
实际上我想在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)
更新:
截图:
【问题讨论】:
你想实现滑动删除之类的吗? @MojtabaHosseini 不删除。滑动以更改记录。 根据您的情况,实现 UIPageViewController 左右滑动查看上一个/下一个视频 cmets。 @Mani 但是你能指导我如何实现这一目标吗?正如我所提到的,我有一个包含视频列表和下方评论按钮的表格视图,就像 insta 类型一样,一旦点击它就会进入评论 - 详细信息页面在所有 cmets 的顶部和下方都有视频,但我想改变滑动的东西,所以用户不会去回到同一个 CommentViewController 用户可以使用滑动手势。 【参考方案1】: 使用 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
【讨论】:
谢谢,但我已经这样做了。我只需要了解记录更改代码:) 要更改记录,您必须在集合视图中加载所有项目,然后使用流布局启用分页。 @NayanDave 如果可能的话,我可以将我的项目上传到 github。 @ShivamParmar 我已经在使用 tableview,即使在 commentViewController 中。实际上我不知道如何实现。 你能把你的屏幕截图发给我,然后解释一下……!以上是关于如何实现左右滑动以更改 swift IOS 中的数据/视图?的主要内容,如果未能解决你的问题,请参考以下文章