How to add an Image to an ScrollView in Swift

Viewed 6753

I develop an App in Xcoode and I add an ScrollView. Now I added some little Images in a Row.

let myImages = ["BallBeispielBlau.png","BallBeispielRot.png","BallBeispielGelb.png","BallBeispielBlau.png","BallBeispielRot.png","BallBeispielGelb.png","BallBeispielBlau.png","BallBeispielRot.png","BallBeispielGelb.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

        let LongTapGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "InfosZuEinemEvent:")
        LongTapGestureRecognizer.minimumPressDuration = 1.0;
        myImageView.addGestureRecognizer(LongTapGestureRecognizer)
        myImageView.userInteractionEnabled = true

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

        ScrollViewFreunde.addSubview(myImageView)
        ScrollViewFreunde.showsHorizontalScrollIndicator = false
        xPosition += imageWidth
        scrollViewSize += imageWidth
    }
    ScrollViewFreunde.contentSize = CGSize(width: scrollViewSize, height: imageHeight)

That works fine but I want it that the six image so the image which come after the five image where show under the first image so in the second row. I try it with an if statement but it doesn´t work. I hope you could help me.

enter image description here

Thanks for help.

3 Answers
Related