0

I want to implement swipe gesture left right feature to change data views in swift 5. I have two viewControllers - FeedsController which contains tableview and list of all videos in it. In FeedsController if I click on comment button it moves to commentViewController and showing same video with with comments in details. Actually I want to implement Swipe gesture action on commentViewController so user can swipe left right even on detail comment page which is commentViewController.

Code for 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) {

    }


}

Update:

Screenshot:

FeedViewController

CommentsViewController

4
  • Do you want to implement swipe to delete or something like that? Commented Nov 30, 2019 at 12:43
  • @MojtabaHosseini no not to delete. Swipe to change records. Commented Nov 30, 2019 at 12:44
  • For your case, implement UIPageViewController to swipe left and right for viewing prev/next video comments. Commented Nov 30, 2019 at 12:55
  • @Mani But can you guide me how to achieve this? As I mentioned I have tableview which contains list of videos and below comment button just like insta type, once clicked it goes to comment - Detail page which has video on top and below all comments but I want change on swipe thing so user will not go back in same CommentViewController user can use swipe gesture. Commented Nov 30, 2019 at 13:04

1 Answer 1

2
  • 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)

Swift 4.2 onwards:
'UISwipeGestureRecognizerDirection' has been renamed to '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)
  • create two objc functions swipedRight and swipedLeft
@objc func swipedRight()
{
    // Add your record changing code here
}

@objc func swipedLeft()
{
    // Add your record changing code here
}
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you but I am already doing this. I just need to know about record changing code :)
to change the records you have to load all items in collection view and then enable paging using flow layout .
@NayanDave If possible I can upload my project to github.
@ShivamParmar I am already using tableview even in commentViewController. Actually I am not getting an idea how to achieve.
can you send me screenshot of your screen and then explain ...!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.