简体   繁体   English

如何在Swift / iOS中创建侧面刷图库?

[英]How to create a side swiping photo gallery in Swift/iOS?

I am looking to create a side swiping photo gallery. 我希望创建一个侧滑图片库。 There are many tutorials that show how to make one with a tableview, but I'd like to make a gallery that starts off with an image and allows you to swipe left or right to browse. 有许多教程展示如何制作一个带有tableview的教程,但我想创建一个以图像开头的图库,并允许您向左或向右滑动以进行浏览。 Does anyone know of a github extension or tutorial, or have any knowledge to help point me in the right direction? 有没有人知道github扩展或教程,或有任何知识帮助指出我正确的方向? Thanks. 谢谢。

Something like this: 像这样的东西: 一些

You could do this on a regular viewController... You could probably find some animations for this, but I guess this is a step in the right direction: 您可以在常规viewController上执行此操作...您可能会为此找到一些动画,但我想这是朝着正确方向迈出的一步:

First, define an imageView and the images to go inside: 首先,定义一个imageView和图像进入内部:

// Connect a UIImageView to the outlet below
@IBOutlet weak var swipeImageView: UIImageView!
// Type in the names of your images below
let imageNames = ["","","","",""]

Then, in the viewDidLoad, set up gestureRecognizers for the directions: 然后,在viewDidLoad中,为方向设置gestureRecognizers:

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    var swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    self.view.addGestureRecognizer(swipeLeft)

After that, you can set up the function that was called to manage the image switches. 之后,您可以设置调用以管理图像开关的功能。

 var currentImage = 0
 func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {


        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.Left:
            if currentImage == imageNames.count - 1 {
                currentImage = 0

            }else{
                currentImage += 1
            }
            swipeImageView.image = UIImage(named: imageNames[currentImage])

        case UISwipeGestureRecognizerDirection.Right:
            if currentImage == 0 {
                currentImage = imageNames.count - 1
            }else{
                currentImage -= 1
            }
            swipeImageView.image = UIImage(named: imageNames[currentImage])
        default:
            break
        }
    }
}

Full viewController code: 全视图控制器代码:

import UIKit

class ViewController: UIViewController {



    // Connect a UIImageView to the outlet below
    @IBOutlet weak var swipeImageView: UIImageView!
    // Type in the names of your images below
    let imageNames = ["","","","",""]



    var currentImage = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
        swipeRight.direction = UISwipeGestureRecognizerDirection.Right
        self.view.addGestureRecognizer(swipeRight)

        var swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
        swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
        self.view.addGestureRecognizer(swipeLeft)
        // Do any additional setup after loading the view, typically from a nib.
    }
    func respondToSwipeGesture(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {


            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.Left:
                if currentImage == imageNames.count - 1 {
                    currentImage = 0

                }else{
                    currentImage += 1
                }
                swipeImageView.image = UIImage(named: imageNames[currentImage])

            case UISwipeGestureRecognizerDirection.Right:
                if currentImage == 0 {
                    currentImage = imageNames.count - 1
                }else{
                    currentImage -= 1
                }
                swipeImageView.image = UIImage(named: imageNames[currentImage])
            default:
                break
            }
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM