How to create a side gallery in Swift / iOS?

I am looking to create a side gallery. There are many tutorials that show how to do this using tableview, but I would like to create a gallery that starts with an image and allows you to scroll left or right to view it. Does anyone know about a github extension or tutorial, or have any knowledge to help me point in the right direction? Thank you

Something like that: Some

+4
source share
1 answer

You can do this on a regular viewController ... Perhaps you can find some animations for this, but I think this is a step in the right direction:

First define imageView and images to go inside:

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

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)

, .

 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
        }
    }
}

viewController :

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.
    }


}
+4

Source: https://habr.com/ru/post/1648802/


All Articles