Search code examples
iosswiftuiscrollviewuiimageviewuiimage

Can't zoom UIImage in a Scroll View with Parse


who can help with zoom on scrollview. Using Parse to get the pictures, but can't do zoom. Through all of the possible features does not work - aImg this is (UIImage) to setupPhotosInScrollView function, can anyone help ? Tried Frameworks but nothing helped


Code

class AdPhotos: UIViewController, UIScrollViewDelegate {

    /*--- VIEWS ---*/
    @IBOutlet weak var containerScrollView: UIScrollView!


    /*--- VARIABLES ---*/
    var adObj = PFObject(className: ADS_CLASS_NAME)
    var photosArray = [UIImage]()



    // ------------------------------------------------
    // MARK: - VIEW DID LOAD
    // ------------------------------------------------
    override func viewDidLoad() {
        super.viewDidLoad()

        // Ad Title
        adTitleLabel.text = "\(adObj[ADS_TITLE]!)"

        // Get photos
        let imageFile = adObj[ADS_IMAGE1] as? PFFile
        imageFile?.getDataInBackground(block: { (data, error) in
            if error == nil { if let imageData = data {
                self.photosArray.append(UIImage(data: imageData)!)
                self.setupPhotosInScrollView()
                print("PHOTO 1")
                }}})

        DispatchQueue.main.async {

            if self.adObj[ADS_IMAGE2] != nil {
                self.pageControl.numberOfPages = 2
                let imageFile = self.adObj[ADS_IMAGE2] as? PFFile
                imageFile?.getDataInBackground(block: { (data, error) in
                    if error == nil { if let imageData = data {
                        self.photosArray.append(UIImage(data: imageData)!)
                        self.setupPhotosInScrollView()
                        print("PHOTO 2")
                        }}})
            }
            if self.adObj[ADS_IMAGE3] != nil {
                self.pageControl.numberOfPages = 3
                let imageFile = self.adObj[ADS_IMAGE3] as? PFFile
                imageFile?.getDataInBackground(block: { (data, error) in
                    if error == nil { if let imageData = data {
                        self.photosArray.append(UIImage(data: imageData)!)
                        self.setupPhotosInScrollView()
                        print("PHOTO 3")
                        }}})
            }
            if self.adObj[ADS_IMAGE4] != nil {
                self.pageControl.numberOfPages = 4
                let imageFile = self.adObj[ADS_IMAGE4] as? PFFile
                imageFile?.getDataInBackground(block: { (data, error) in
                    if error == nil { if let imageData = data {
                        self.photosArray.append(UIImage(data: imageData)!)
                        self.setupPhotosInScrollView()
                        print("PHOTO 4")
                        }}})
            }
            if self.adObj[ADS_IMAGE5] != nil {
                self.pageControl.numberOfPages = 5
                let imageFile = self.adObj[ADS_IMAGE5] as? PFFile
                imageFile?.getDataInBackground(block: { (data, error) in
                    if error == nil { if let imageData = data {
                        self.photosArray.append(UIImage(data: imageData)!)
                        self.setupPhotosInScrollView()
                        print("PHOTO 5")
                        }}})
            }

        }
    }
    // ------------------------------------------------
    // MARK: - SETUP PHOTOS IN SCROLLVIEW
    // ------------------------------------------------
    @objc func setupPhotosInScrollView() {
        var X:CGFloat = 0
        let Y:CGFloat = 0
        let W:CGFloat = view.frame.size.width
        let H:CGFloat = view.frame.size.height
        let G:CGFloat = 0
        var counter = 0

        // Loop to create ImageViews
        for i in 0..<photosArray.count {
            counter = i

            // Create a ImageView
            let aImg = UIImageView(frame: CGRect(x: X, y: Y, width: W, height: H))
            aImg.tag = i
            aImg.contentMode = .scaleAspectFit
            aImg.image = photosArray[i]


            // Add ImageViews based on X
            X += W + G
            containerScrollView.addSubview(aImg)

        } // ./ FOR loop

        // Place Buttons into a ScrollView
        containerScrollView.contentSize = CGSize(width: W * CGFloat(counter+2), height: H)
    }


    // ------------------------------------------------
    // MARK: - CHANGE PAGE CONTROL PAGES ON SCROLL
    // ------------------------------------------------
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let pageWidth = containerScrollView.frame.size.width
        let page = Int(floor((containerScrollView.contentOffset.x * 2 + pageWidth) / (pageWidth * 2)))
        pageControl.currentPage = page
    }
}

Solution

  • I think you don't need ScrollView and manage Pagingnation. you need CollectionView with just enable Paging option From story-bored.

    1. Drag Collection View in your ViewController. Enable paging option from property Selector.

    2. Assign CollectionView(leading, trailing, top, bottom) to View(leading, trailing, top, bottom )

    3. Drag CollectionView cell into CollectionView. Resize cell to fill entire CollectionView

    4. add Scrollview into CollectionView cell and add ScrollView(leading, trailing, top, bottom) == CollectionView cell(leading, trailing, top, bottom)

    5. add imageView in Scrollview and add imageView(leading, trailing, top, bottom) == Scrollview (leading, trailing, top, bottom) and equal width and height to CollectionView cell.

    6. implement DataSource and Delegate method For CollectionView.

    and For Zooming Add Following Code to Your CollectionView Cell

    import UIKit
    
    class CustomeCellCollectionViewCell: UICollectionViewCell , UIScrollViewDelegate {
    
        @IBOutlet var imageView : UIImageView!
        @IBOutlet var scrollView : UIScrollView!
    
        override func awakeFromNib() {
            super.awakeFromNib()
    
            scrollView.delegate = self
            scrollView.minimumZoomScale = 1.0
            scrollView.maximumZoomScale = 6.0
        }
    
        func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    
            return feedsImageView
        }
    }
    

    enter image description here