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

如何在swift IOS中实现左右滑动以更改数据/视图?,ios,swift,uitableview,swipe,Ios,Swift,Uitableview,Swipe,我想实现左右滑动手势功能,以更改swift 5中的数据视图。 我有两个ViewController-FeedsController,其中包含tableview和其中所有视频的列表。在FeedsController中,如果我单击comment按钮,它将移动到commentViewController,并显示带有详细注释的相同视频。 实际上,我想在commentViewController上实现滑动手势动作,这样用户甚至可以在细节评论页面(即commentViewController)上左右滑动 C

我想实现左右滑动手势功能,以更改swift 5中的数据视图。 我有两个ViewController-
FeedsController
,其中包含tableview和其中所有视频的列表。在
FeedsController
中,如果我单击comment按钮,它将移动到
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! 
  • Use 2 different functions to handle different swipe gestures
  • Add following in
    viewDidLoad()
up to 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)
导入UIKit
导入浮动面板
最后一个类CommentsViewController:UIViewController、CommentsViewInput、FloatingPanelControllerDelegate{
var post:IContentPost!
私有变量输出:CommentsViewOutput?
私有var fpc:FloatingPanelController!
私家车:布尔{
返回为VideoPlayerController.sharedVideoPlayer.mute
}
私有弱var内容VC:BottomSheetInputView?
私有let路由器=路由器()
@IBVAR表格视图:UITableView!
重写func viewDidLoad(){
super.viewDidLoad()
设置()
swipeGesture()
}
覆盖函数视图将消失(u动画:Bool){
超级。视图将消失(动画)
fpc.removePanelFromParent(动画:true)
}
@IBAction func dismissAction(\发送方:任意){
驳回(动画:真,完成:无)
}
func swipeGesture(){
让leftSwipe=UISwipegestureRecognitor(目标:self,操作:#选择器(handleSwipes(:))
让rightSwipe=UISwipegestureRecognitor(目标:self,操作:#选择器(handleSwipes(:))
leftSwipe.direction=.left
rightSwipe.direction=.right
view.AddGestureRecognitor(左扫)
view.AddGestureRecognitor(右击)
}
@objc func handleSwipes(\发送方:UISwipegestureRecognitor)
{
如果(发送方方向==.左)
{
打印(“向左滑动”)
}
if(sender.direction==.right)
{
打印(“向右滑动”)
}
}
专用函数设置(){
让演示者=评论演示模型(视图:self,post:post)
self.output=presenter
setupTableView()
设置面板()
输出?.getComments(){[weak self]中的注释
guard let comments=comments else{return}
self?.contentVC?.updateComments(带:注释)
}
}
专用功能设置面板(){
fpc=浮动面板控制器()
fpc.delegate=自//可选
让contentVC=router.bottomVC()
contentVC.delegate=self
self.contentVC=contentVC
fpc.set(contentViewController:contentVC)
fpc.track(滚动视图:contentVC.tableView)
fpc.addPanel(主客户:自身)
}
专用函数setupTableView(){
tableView.delegate=self
tableView.dataSource=self
tableView?.registerReusableCell(FeedContentTableViewCell.self)
}
}
扩展注释ViewController:UITableViewDelegate,UITableViewDataSource{
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回1
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell:FeedContentTableViewCell=tableView.dequeueReusableCell(for:indexPath)
cell.delegate=self
单元格设置(带:post、静音:静音、需要插入:false)
返回单元
}
}
扩展注释viewcontroller:FeedContentCellDelegate{
func-likePressed(id:String){
输出?.postId(postId:id)
}
func CommentPost(发布:IContentPost){
fpc.show(动画:真,完成:无)
}
func sharePressed(id:字符串){
输出?.postShared(postId:id)
让text=“嘿!你得看看!
  • 使用两种不同的功能处理不同的刷卡手势
  • viewDidLoad()中添加以下内容
最高达Swift 4.0

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)
Swift 4.2及以后版本
“UISweepGestureRecognitizerDirection”已重命名为“UISweepGestureRecognitizer.Direction”

  • 创建两个objc函数
    swipedRight
    swipedLeft
@objc func swipedRight()
{
//在此处添加您的记录更改代码
}
@objc func swipedLeft()
{
//在此处添加您的记录更改代码
}
  • 使用两种不同的功能处理不同的刷卡手势
  • viewDidLoad()中添加以下内容
最高达Swift 4.0

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)
Swift 4.2及以后版本
“UISweepGestureRecognitizerDirection”已重命名为“UISweepGestureRecognitizer.Direction”

  • 创建两个objc函数
    swipedRight
    swipedLeft
@objc func swipedRight()
{
//在此处添加您的记录更改代码
}
@objc func swipedLeft()
{
//在此处添加您的记录更改代码
}

是否要执行滑动删除或类似操作?@MojtabaHosseini no不删除。滑动更改记录。对于您的情况,请执行UIPageViewController向左和向右滑动以查看上一个/下一个视频评论。@Mani但您能指导我如何实现吗?正如我提到的,我有包含视频列表的tableviewd下面的评论按钮就像insta类型一样,点击后会进入评论-详细信息页面,上面和下面都有视频,但我想更改滑动内容,这样用户就不会在同一个CommentViewController中返回。用户可以使用滑动手势。您想实现滑动删除或类似操作吗?@MojtabaHosseini no不删除.滑动以更改记录。对于您的情况,请使用UIPageViewController向左和向右滑动以查看上一个/下一个视频评论。@Mani但您能指导我如何实现这一点吗?正如我所提到的