如何在所有四个方向上在按钮操作上滑动 UIView

问题描述

我在左右两侧有两个 UIButton,按钮中间有一个 UIView..

我必须在 UIButton 单击时使用一些动画滑动 ContainerView..

如果用户点击右侧按钮 ContainerView 向 .right 方向滑动....如果用户点击左侧按钮 ContainerView 向 .left 方向滑动..

我没有在任何地方找到它....需要完整编码部分的帮助

我已经这样做了

override func viewDidAppear(_ animated: Bool) {
    leftSwipe()
    rightSwipe()
}

 //MARK:Left Swipe Function
func leftSwipe()
{
    let swipeLeft = UISwipeGestureRecognizer()
    swipeLeft.direction = .left   
    self.insideContainerViewSecond.addGestureRecognizer(swipeLeft)
    swipeLeft.addTarget(self,action: #selector(swipe(sender:)))
}

//MARK:Right Swipe Function
func rightSwipe()
{
    let swipeRight = UISwipeGestureRecognizer()
    swipeRight.direction = .right  
    self.insideContainerViewSecond.addGestureRecognizer(swipeRight)
    swipeRight.addTarget(self,action: #selector(swipe(sender:)))
}

//MARK: Swipe Function
@objc func swipe(sender: UISwipeGestureRecognizer)
{
    switch sender.direction
    {
    case .left:
        print("swiped left")
    case .right:
        print("swiped right")
    default:
        print("no action")
    }
}

在这图片中,我有一个 UIView 和两个左右按钮..当我在 UIView 上滑动时,它在没有动画的情况下左右滑动.....翻转动画

enter image description here

解决方法

我们有 iOS 的 CATransition 类。你应该使用这个。或者你应该约束动画。也许这个 repo 可以帮助你获得第一个技巧。

https://github.com/barankaraoguzzz/FastOnBoarding/blob/master/FOView/Classes/FOAnimation.swift

如果这个问题的答案不正确。再来:)

,

你有几个不同的问题...

如何在点击按钮时“模拟”滑动手势?

您可以通过不同的方式执行此操作,例如创建一个 showNextImage() 函数,然后通过向左滑动或点击右键来调用它。

或者,您可以执行以下操作,创建一个临时的滑动手势识别器并将其传递给您的识别器函数:

@objc func leftButtonTap(_ sender: UIButton)
{
    // create a right-swipe-gesture
    let sgr = UISwipeGestureRecognizer()
    sgr.direction = .right
    self.swipe(sender: sgr)
}

@objc func rightButtonTap(_ sender: UIButton)
{
    // create a left-swipe-gesture
    let sgr = UISwipeGestureRecognizer()
    sgr.direction = .left
    self.swipe(sender: sgr)
}

您在轻扫(或点击按钮)时说,要使用翻转动画来显示下一张(或上一张)图像?

您可以通过向“容器”视图添加两个图像视图来实现此目的。设置一个视图隐藏。当你想“显示下一张图片”时,设置隐藏imageView的.image属性,然后使用UIView.transition(...)从可见imageView翻转到隐藏imageView:

private func flipMe(_ direction: UIView.AnimationOptions) -> Void {
    // fromView is the one that is NOT hidden
    let fromView = imgViewB.isHidden ? imgViewA : imgViewB
    
    // toView is the one that IS hidden
    let toView = imgViewB.isHidden ? imgViewB : imgViewA
    
    UIView.transition(from: fromView,to: toView,duration: 0.5,options: [direction,.showHideTransitionViews],completion: { b in
                        // if we want to do something on completion
                      })
}

这是一个简单的示例,您可以运行它看看会发生什么。没有 @IBOutlet@IBAction 连接,因此您不需要任何 Storyboard 设置:

FlipView 自定义视图子类:

class FlipView: UIView {
    
    // set image names from controller
    var imageNames: [String] = [] {
        didSet {
            if let img = UIImage(named: imageNames[0]) {
                imgViewB.image = img
            } else {
                imgViewB.image = testImage()
            }
        }
    }
    
    // index counter
    private var idx: Int = 0
    
    // two image views
    private let imgViewA: UIImageView = UIImageView()
    private let imgViewB: UIImageView = UIImageView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }
    func commonInit() -> Void {
        
        // add both image views
        // with light-gray backgrounds
        // constraining all 4 sides to self
        [imgViewA,imgViewB].forEach { v in
            v.backgroundColor = UIColor(white: 0.9,alpha: 1.0)
            addSubview(v)
            v.translatesAutoresizingMaskIntoConstraints = false
            NSLayoutConstraint.activate([
                v.topAnchor.constraint(equalTo: topAnchor),v.leadingAnchor.constraint(equalTo: leadingAnchor),v.trailingAnchor.constraint(equalTo: trailingAnchor),v.bottomAnchor.constraint(equalTo: bottomAnchor),])
        }
        
        // start with one of the image views hidden
        imgViewA.isHidden = true
        
    }
    
    private func flipMe(_ direction: UIView.AnimationOptions) -> Void {
        // fromView is the one that is NOT hidden
        let fromView = imgViewB.isHidden ? imgViewA : imgViewB
        
        // toView is the one that IS hidden
        let toView = imgViewB.isHidden ? imgViewB : imgViewA
        
        UIView.transition(from: fromView,completion: { b in
                            // if we want to do something on completion
                          })
    }
    
    func flipToPrevious() -> Void {
        // don't try to flip past the first image
        if idx == 0 {
            return
        }
        
        // decrement the index
        idx -= 1
        
        let hiddenImageView = imgViewA.isHidden ? imgViewA : imgViewB
        // get the previous image
        if let img = UIImage(named: imageNames[idx]) {
            hiddenImageView.image = img
        } else {
            hiddenImageView.image = testImage()
        }
        
        // flip it from left
        flipMe(.transitionFlipFromLeft)
    }

    func flipToNext() -> Void {
        // don't try to flip past the last image
        if idx == imageNames.count - 1 {
            return
        }
        
        // increment the index
        idx += 1
        
        let hiddenImageView = imgViewA.isHidden ? imgViewA : imgViewB
        // get the next image
        if let img = UIImage(named: imageNames[idx]) {
            hiddenImageView.image = img
        } else {
            hiddenImageView.image = testImage()
        }
        
        // flip it from right
        flipMe(.transitionFlipFromRight)
    }
    
    // get a numbered system image if the named image
    //  cannot be loaded from assets
    private func testImage() -> UIImage {
        let colors: [UIColor] = [
            .systemRed,.systemGreen,.systemBlue,.systemYellow,.systemTeal,.systemPurple,]
        guard let img = UIImage(systemName: "\(idx+1).circle") else {
            // that should not fail,but in case it does
            return UIImage()
        }
        return img.withTintColor(colors[idx % colors.count],renderingMode: .alwaysOriginal)
    }
}

ViewController 示例 ViewController 类:

class ViewController: UIViewController {
    
    let insideContainerViewSecond: FlipView = FlipView()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // respect safe area
        let g = view.safeAreaLayoutGuide
    
        // create left and right buttons
        let leftButton = UIButton()
        leftButton.translatesAutoresizingMaskIntoConstraints = false
        let leftImg = UIImage(systemName: "chevron.left.circle.fill")
        leftButton.setImage(leftImg,for: [])
        leftButton.addTarget(self,action: #selector(leftButtonTap(_:)),for: .touchUpInside)
        
        let rightButton = UIButton()
        rightButton.translatesAutoresizingMaskIntoConstraints = false
        let rightImg = UIImage(systemName: "chevron.right.circle.fill")
        rightButton.setImage(rightImg,for: [])
        rightButton.addTarget(self,action: #selector(rightButtonTap(_:)),for: .touchUpInside)
        
        insideContainerViewSecond.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubview(insideContainerViewSecond)
        view.addSubview(leftButton)
        view.addSubview(rightButton)
        
        NSLayoutConstraint.activate([
            
            insideContainerViewSecond.topAnchor.constraint(equalTo: g.topAnchor,constant: 80.0),insideContainerViewSecond.leadingAnchor.constraint(equalTo: g.leadingAnchor,insideContainerViewSecond.trailingAnchor.constraint(equalTo: g.trailingAnchor,constant: -80.0),insideContainerViewSecond.heightAnchor.constraint(equalTo: insideContainerViewSecond.widthAnchor,multiplier: 0.5),leftButton.centerYAnchor.constraint(equalTo: insideContainerViewSecond.topAnchor),leftButton.centerXAnchor.constraint(equalTo: insideContainerViewSecond.leadingAnchor),rightButton.centerYAnchor.constraint(equalTo: insideContainerViewSecond.topAnchor),rightButton.centerXAnchor.constraint(equalTo: insideContainerViewSecond.trailingAnchor),])

        // your array of image names
        let imageNames: [String] = [
            "pic1","pic2","pic3","pic4","pic5","pic6",]

        insideContainerViewSecond.imageNames = imageNames

        leftSwipe()
        rightSwipe()
    }

    //MARK:Left Swipe Function
    func leftSwipe()
    {
        let swipeLeft = UISwipeGestureRecognizer()
        swipeLeft.direction = .left
        self.insideContainerViewSecond.addGestureRecognizer(swipeLeft)
        swipeLeft.addTarget(self,action: #selector(swipe(sender:)))
    }
    
    //MARK:Right Swipe Function
    func rightSwipe()
    {
        let swipeRight = UISwipeGestureRecognizer()
        swipeRight.direction = .right
        self.insideContainerViewSecond.addGestureRecognizer(swipeRight)
        swipeRight.addTarget(self,action: #selector(swipe(sender:)))
    }
    
    //MARK: Swipe Function
    @objc func swipe(sender: UISwipeGestureRecognizer)
    {
        switch sender.direction
        {
        case .left:
            print("swiped left")
            self.insideContainerViewSecond.flipToNext()
        case .right:
            print("swiped right")
            self.insideContainerViewSecond.flipToPrevious()
        default:
            print("no action")
        }
    }

    @objc func leftButtonTap(_ sender: UIButton)
    {
        // create a right-swipe-gesture
        let sgr = UISwipeGestureRecognizer()
        sgr.direction = .right
        self.swipe(sender: sgr)
    }

    @objc func rightButtonTap(_ sender: UIButton)
    {
        // create a left-swipe-gesture
        let sgr = UISwipeGestureRecognizer()
        sgr.direction = .left
        self.swipe(sender: sgr)
    }

}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...