微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我希望创建一个侧滑图片库.有许多教程展示如何制作一个带有tableview的教程,但我想创建一个以图像开头的图库,并允许您向左或向右滑动以进行浏览.有没有人知道 github扩展或教程,或有任何知识帮助指出我正确的方向?谢谢.

像这样的东西:

您可以在常规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
        }
    }
}

全视图控制器代码

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


}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐