首页 > 解决方案 > 如何在 Swift 中向 UIView 发送滑动动作

问题描述

我正在尝试克隆 Tinder,完成了 Yalantis/Koloda(https://github.com/Yalantis/Koloda)向左(不喜欢的人)和向右(喜欢的人)滑动的操作。我还有两个按钮喜欢/不喜欢的图像。我希望当我触摸喜欢/不喜欢的图像时,我的 mainView 会向左/向右滑动。

let likeImageTap = UITapGestureRecognizer(target: self, action: #selector(HomeViewController.sendSwipeRightAction))
        likeImage.isUserInteractionEnabled = true
        likeImage.addGestureRecognizer(likeImageTap)

我应该如何输入这个函数?

    @objc func sendSwipeRightAction() {
        
    }

这是我的协议

extension HomeViewController: KolodaViewDataSource, KolodaViewDelegate {
   //...
       func koloda(_ koloda: KolodaView, didSwipeCardAt index: Int, in direction: SwipeResultDirection)
    {
        if direction == .left {
          //...
          print ("swipe left")
        }
        if direction == .right {
          //...
          print("swipe right")
        }
    }
}

这是我的看法

@IBOutlet var mainView: KolodaView!

我尝试使用这个函数,然后在点击图像时调用它,这样我就可以像滑动一样做我想做的事情,但视图不会更改为下一张图像。

public func swipe(_ direction: SwipeResultDirection, force: Bool = false)
 { 
//..
}

所以我想向视图发送滑动动作,例如:

mainView.sendActions(for: .swipeLeft)

我应该怎么做?谢谢你的建议。

标签: iosswiftuiswipegesturerecognizerswipeviewkoloda

解决方案


示例中有代码

kolodaView.swipe(.left)
kolodaView.swipe(.right)

你的代码将是这样的

@objc func sendSwipeRightAction() {
        mainView.swipe(.right)
    }

推荐阅读