带有全屏滑块的Swift图库

时间:2015-04-10 07:37:50

标签: ios arrays image swift gallery

我想建立一个类似Facebook的图片库,但不知道从哪里开始。 这就是我认为应该做的事情:

  • 显示基于数组的缩略图的CollectionView
  • 在行上单击时,我基于相同的图像数组
  • 模态打开pageViewController
  • 在向左滑动时,我转到下一张或上一张图片
  • 在向上滑动时,我关闭此视图并返回我的collectionview

这个逻辑是正确的/最佳做法吗? 也许它已经由某人完成了。

2 个答案:

答案 0 :(得分:1)

您可以使用香蕉库在Swift滑块视图中显示图像

https://github.com/gauravkatoch007/banana获取香蕉

import banana

@IBOutlet weak var imageScrollView: UIScrollView!

// Here imageArray can be a string array of Image URLs 
var imageArray = [String]()

//or imageArray can be a array of UIImages
var imageArray = [UIImage]()

var imageScroll = banana( imageScrollView :self.imageScrollView )
//Load to load images in memory and display them in User Interface
imageScroll!.load(imageArray)

//Call startScroll for autoScrolling. Default scrolling timer is 8 seconds
imageScroll!.startScroll()


//Call this function to stop autoScrolling on touch or swipe.
imageScroll!.assignTouchGesture()

答案 1 :(得分:0)

更新我解决了这个问题:

  • 图库是一个集合视图
  • 在选中的单元格上,我模态地转换到另一个视图,其中包含一个滚动视图,其中我将5个图像放在另一个(右)之后,并处理分页。
  • 我的图像将始终是中间的第三个,每次滚动后,我选择整个数组的5个图像,这些图像以索引为中心,或者包含5个图像。

这是我的类PhotoViewController:UIViewController,UIScrollViewDelegate {

var screenSize: CGRect = UIScreen.mainScreen().bounds

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var blackView: UIView!
@IBOutlet weak var doneButton: UIButton!
@IBOutlet weak var photoActions: UIImageView!

var img1:UIImageView = UIImageView(image: UIImage())
var img2:UIImageView = UIImageView(image: UIImage())
var img3:UIImageView = UIImageView(image: UIImage())
var img4:UIImageView = UIImageView(image: UIImage())
var img5:UIImageView = UIImageView(image: UIImage())

var w1: UIImage!
var w2: UIImage!
var w3: UIImage!
var w4: UIImage!
var w5: UIImage!
var pageIndex: Int!
var currentPage: Int!
var oldPage: Int!
var endFrame: CGRect!
var arrayIndex: Int!

// placeholder for if the image frame is scrolled
var scrolledPhotoFrame: CGRect!

override func viewDidLoad() {
    super.viewDidLoad()
    RightSize()
    LoadData()
}

func LoadData(){
    // data passed from feedViewController gets stored here
    img1.image = w1
    img2.image = w2
    img3.image = w3
    img4.image = w4
    img5.image = w5
    // set the right endFrame based on the selectedImage

    switch pageIndex {
    case 0:
        img1.frame = endFrame
        img1.frame.origin.x = 0
    case 1:
        img2.frame = endFrame
        img2.frame.origin.x = screenSize.width
    case 2:
        img3.frame = endFrame
        img3.frame.origin.x = (screenSize.width * 2)
    case 3:
        img4.frame = endFrame
        img4.frame.origin.x = (screenSize.width * 3)
    case 4:
        img5.frame = endFrame
        img5.frame.origin.x = (screenSize.width * 4)
    default:
        break
    }
    currentPage = pageIndex
    oldPage = pageIndex

    scrollView.contentSize = CGSize(width: screenSize.width*5, height: screenSize.height)
    scrollView.contentOffset.x = CGFloat(pageIndex * Int(screenSize.width))
    println(scrollView.contentOffset.x)
    // default value of scrolledPhotoFrame is unscrolled position of photoImageView
    scrolledPhotoFrame = endFrame

    // required for registering scroll events
    scrollView.delegate = self
   // RightSize()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func doneDidPress(sender: AnyObject) {
    dismissViewControllerAnimated(true, completion: nil)
}

// called while scrolling
func scrollViewDidScroll(scrollView: UIScrollView) {

    currentPage = Int(scrollView.contentOffset.x / screenSize.width)
    if (currentPage != oldPage){
        //Ho cambiato pagina
        if (currentPage>oldPage){
            //Sono andato di 1 foto avanti
            println("Avanti")
            arrayIndex = arrayIndex+1
            CambioPagina()

        }else{
            //Sono andato di 1 foto indietro
            arrayIndex = arrayIndex-1
            println("indietro")
        }

        oldPage=currentPage
    }
    var alpha = CGFloat(1 - abs(scrollView.contentOffset.y) / 100)
    var alpha2 = CGFloat(1 - abs(scrollView.contentOffset.y) / 20)

    blackView.alpha = alpha
    doneButton.alpha = alpha2
    photoActions.alpha = alpha2
}

// This method is called right as the user lifts their finger
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {

    var offsetY = scrollView.contentOffset.y
    var alpha = CGFloat(1 - abs(scrollView.contentOffset.y) / 240)

    if (abs(offsetY) > 100) {
        scrolledPhotoFrame.origin.y = scrolledPhotoFrame.origin.y - offsetY
        blackView.hidden = true
        scrollView.hidden = true // could be wrong
        doneButton.hidden = true
        dismissViewControllerAnimated(true, completion: nil)
    }
}

// selecting the view to zoom
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
    switch currentPage {
    case 0:
        return img1
    case 1:
        return img2
    case 2:
        return img3
    case 3:
        return img4
    case 4:
        return img5
    default:
        return nil
    }
}

func scrollViewWillBeginZooming(scrollView: UIScrollView, withView view: UIView!) {
    img2.hidden = true
    img3.hidden = true
    img4.hidden = true
    img5.hidden = true
}

func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView!, atScale scale: CGFloat) {

}

//Controllo per cambio pagina..! chissa!
func CambioPagina(){
    switch arrayIndex{
    case 0:
        w1 = UIImage(named: arrayFoto[arrayIndex])
        w2 = UIImage(named: arrayFoto[arrayIndex+1])
        w3 = UIImage(named: arrayFoto[arrayIndex+2])
        w4 = UIImage(named: arrayFoto[arrayIndex+3])
        w5 = UIImage(named: arrayFoto[arrayIndex+4])
        pageIndex = 0
    case 1:
        w1 = UIImage(named: arrayFoto[arrayIndex-1])
        w2 = UIImage(named: arrayFoto[arrayIndex])
        w3 = UIImage(named: arrayFoto[arrayIndex+1])
        w4 = UIImage(named: arrayFoto[arrayIndex+2])
        w5 = UIImage(named: arrayFoto[arrayIndex+3])
        pageIndex = 1
    case (arrayFoto.count-2):
        w1 = UIImage(named: arrayFoto[arrayIndex-3])
        w2 = UIImage(named: arrayFoto[arrayIndex-2])
        w3 = UIImage(named: arrayFoto[arrayIndex-1])
        w4 = UIImage(named: arrayFoto[arrayIndex])
        w5 = UIImage(named: arrayFoto[arrayIndex+1])
        pageIndex = 3
    case (arrayFoto.count-1):
        w1 = UIImage(named: arrayFoto[arrayIndex-4])
        w2 = UIImage(named: arrayFoto[arrayIndex-3])
        w3 = UIImage(named: arrayFoto[arrayIndex-2])
        w4 = UIImage(named: arrayFoto[arrayIndex-1])
        w5 = UIImage(named: arrayFoto[arrayIndex])
        pageIndex = 4
    default:
        w1 = UIImage(named: arrayFoto[arrayIndex-2])
        w2 = UIImage(named: arrayFoto[arrayIndex-1])
        w3 = UIImage(named: arrayFoto[arrayIndex])
        w4 = UIImage(named: arrayFoto[arrayIndex+1])
        w5 = UIImage(named: arrayFoto[arrayIndex+2])
        pageIndex = 2
    }
    LoadData()
}
func RightSize(){
    img1.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height)
    scrollView.addSubview(img1)
    img2.frame = CGRect(x: screenSize.width, y: 0, width: screenSize.width, height: screenSize.height)
    scrollView.addSubview(img2)
    img3.frame = CGRect(x: (screenSize.width*2), y: 0, width: screenSize.width, height: screenSize.height)
    scrollView.addSubview(img3)
    img4.frame = CGRect(x: (screenSize.width*3), y: 0, width: screenSize.width, height: screenSize.height)
    scrollView.addSubview(img4)
    img5.frame = CGRect(x: (screenSize.width*4), y: 0, width: screenSize.width, height: screenSize.height)
    scrollView.addSubview(img5)
    img1.contentMode = .ScaleAspectFit
    img2.contentMode = .ScaleAspectFit
    img3.contentMode = .ScaleAspectFit
    img4.contentMode = .ScaleAspectFit
    img5.contentMode = .ScaleAspectFit
}

任何更好的方法都会很棒,例如,我无法处理图像缩放