如何创建斯威夫特/ iOS的一个侧面刷卡照相馆?

问题描述 投票:5回答:1

我期待创造一个侧面刷卡照相馆。有很多的教程介绍如何制作一个带的tableview,但我想,使与图像开始起飞,并允许您向左或向右滑动来浏览一个画廊。有谁知道一个GitHub的延伸或教程,或有任何知识,帮助点我在正确的方向?谢谢。

事情是这样的:Some

ios swift
1个回答
11
投票

你可以这样做定期的viewController ...你也许可以找到一些这方面的动画,但我想这是正确的方向迈出的一步:

首先,定义一个ImageView的和去里面的图片:

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


}