如何实现左右滑动以在快速的IOS中更改数据/视图?

问题描述 投票:0回答:1

我想实现左右滑动手势功能,以在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) {

    }


}

ios swift uitableview swipe
1个回答
0
投票
使用2种不同的功能来处理不同的滑动手势
  • viewDidLoad()中添加以下内容>
  • 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)

    创建两个objc函数swipedRightswipedLeft

  • @objc func swipedRight()

    {在此处添加更改记录的代码}

    @objc func swipedLeft()
    

    {在此处添加更改记录的代码}

© www.soinside.com 2019 - 2024. All rights reserved.