【问题标题】:Scroll View with images in a Horizontal (like the Youtube App) in Swift在 Swift 中水平滚动查看图像(如 Youtube 应用程序)
【发布时间】:2016-01-12 16:02:33
【问题描述】:

我想学习 Swift,我想制作一个带有 ScrollView 的应用,就像在 YouTube 应用中一样。

我在情节提要中制作的滚动视图。 我用这段代码试试:

let myImages = ["ImageGelb.png","ImageGrün.png","ImageRot.png"]
    let imageWidth:CGFloat = 66
    let imageHeight:CGFloat = 66
    var xPosition:CGFloat = 0
    var scrollViewSize:CGFloat=0

    var index=0
    index<=myImages.count
    index++

    let myImage:UIImage = UIImage(named: myImages[index])!
    let myImageView:UIImageView = UIImageView()
    myImageView.image = myImage

    myImageView.frame.size.width = imageWidth
    myImageView.frame.size.height = imageHeight
    myImageView.frame.origin.x = xPosition
    myImageView.frame.origin.y = 10

    ScrollViewNeuesFreunde.addSubview(myImageView)
    xPosition += imageWidth
    scrollViewSize += imageWidth

    ScrollViewNeuesFreunde.contentSize = CGSize(width: scrollViewSize, height: imageHeight)

但它不起作用。屏幕上只有“ImageGrün.png”,而 ScrollView 不起作用。

有人可以帮我吗?

【问题讨论】:

    标签: ios swift image youtube uiscrollview


    【解决方案1】:

    除了一次之外,您不会增加索引。 index = 0,然后你用 index++ 增加它,在你的数组的索引 1 处为你提供图片。您想使用 for 循环。

    let myImages = ["ImageGelb.png","ImageGrün.png","ImageRot.png"]
        let imageWidth:CGFloat = 66
        let imageHeight:CGFloat = 66
        var xPosition:CGFloat = 0
        var scrollViewSize:CGFloat=0
    
    
        for image in myImages {
           let myImage:UIImage = UIImage(named: image)!
           let myImageView:UIImageView = UIImageView()
           myImageView.image = myImage
    
           myImageView.frame.size.width = imageWidth
           myImageView.frame.size.height = imageHeight
           myImageView.frame.origin.x = xPosition
           myImageView.frame.origin.y = 10
    
           ScrollViewNeuesFreunde.addSubview(myImageView)
           xPosition += imageWidth
           scrollViewSize += imageWidth
        }
        ScrollViewNeuesFreunde.contentSize = CGSize(width: scrollViewSize, height: imageHeight)
    

    这样您就不需要索引,因为 Swift 的快速枚举会为您处理好它。欲了解更多信息,请查看此链接。https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html#//apple_ref/doc/uid/TP40014097-CH9-ID121

    【讨论】:

      猜你喜欢
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 2013-07-03
      • 1970-01-01
      相关资源
      最近更新 更多