0

I have created and initialized a scrollview that pulls from an array of images. So a user swipes past a photo, then another, and I need to delete the image based on the location of the current image I have created a for loop that deletes the image i-- in the array, but this crashes because I can't delete the i. Is there a way I can accomplish this? Thank you: here is my code

class ViewController: UIViewController {
var imageArray = [UIImage]()

@IBOutlet weak var mainScrollView: UIScrollView!

  func scrollViewDidScroll(_ scrollView: UIScrollView) {
        for i in 0..<imageArray.count {
            if i >= 3 {
                imageArray.remove(at: i - 2)
            }
    }
}


override func viewDidLoad() {
super.viewDidLoad()
mainScrollView.frame = view.frame
imageArray = [#imageLiteral(resourceName: "dubai6"), #imageLiteral(resourceName: "dubai2"), #imageLiteral(resourceName: "dubai7"), #imageLiteral(resourceName: "dubai4"), #imageLiteral(resourceName: "dubai3"), #imageLiteral(resourceName: "dubai5"), #imageLiteral(resourceName: "DigitalDrawingPreview"), #imageLiteral(resourceName: "denarus"), #imageLiteral(resourceName: "dubai1")]
for i in 0..<imageArray.count  {
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFit
    imageView.image = imageArray[i]
    let xPosition = self.view.frame.width * CGFloat(i)
    imageView.frame = CGRect(x: xPosition, y: 0, width: self.view.frame.width, height: self.mainScrollView.frame.height)

    mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
    mainScrollView.addSubview(imageView)
}
5
  • This code example isn't really complete, can you add the full example. Commented Mar 21, 2017 at 17:58
  • I believe this is all my code I can check but I'm pretty sure this what I got, just a scroll view with images john_ryan 36 Commented Mar 21, 2017 at 18:35
  • Ok i assumed there would be some code that handled the scroll, otherwise how would you remove the last image once the user scrolled to the next Commented Mar 21, 2017 at 18:36
  • Good point let me get that part make take a few minutes Commented Mar 21, 2017 at 18:38
  • john_ryan i added the scrollviewdidscroll method check it out, still not working Commented Mar 22, 2017 at 2:26

1 Answer 1

1

Rather than using a UIScrollView, I would recommend using a UITableView or UICollectionView. It is very easy to manage the data model and then call table.reloadData().

You would track the scroll position with the ScrollViewDelegate method scrollViewDidScroll(_ scrollView) and force a reload once you've scrolled far enough.

Sign up to request clarification or add additional context in comments.

4 Comments

dmorrow I cannot use a table view, so ill try using the reload idea in the did scroll
@RandyWindin Why can't you use a table view? It's just a scroll view with external logic about how to place repeating objects.
well it is a horozontial scroll not verticle, i created this to see images that you can scroll through like on your photo album on your phone.
The you should use a UICollectionView, which can scroll in either direction. This use case is exactly the type of thing it is designed for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.