jeudi 23 juin 2016

UIscrollView Paging and Zooming images in swift


Having a image array(Download from server) which i want to show in scrollview with paging and zooming enable but i didn't find any solution. I am searching a lot but i didn't get any solution.

Any help is really appreciating. Thanks!

In MainController

 var mainScrollView: UIScrollView!


override func viewDidLoad() {
    super.viewDidLoad()
    var x:CGFloat = 0
    mainScrollView = UIScrollView(frame: view.bounds)
    mainScrollView.pagingEnabled = true
    let images=["image.png","photo.jpg","3.png"]
    for var i = 0; i < images.count; i++
    {

      let  imageView = UIImageView(image: UIImage(named: images[i]))

        let scrollView = SlideShow(image: imageView, scrollFrame: CGRectMake(x, view.bounds.origin.y, view.bounds.width, view.bounds.height))

        x = x + UIScreen.mainScreen().bounds.size.width
       mainScrollView.addSubview(scrollView)
    }
    mainScrollView.contentSize=CGSizeMake(x, self.mainScrollView.frame.size.height);
    mainScrollView.contentOffset=CGPointMake(0, 0);
    view.addSubview(mainScrollView)
    // Do any additional setup after loading the view.


}

In SubClass

public class SlideShow: UIScrollView, UIScrollViewDelegate {

public var imageView = UIImageView()


// MARK: - Life cycle

init(image: UIImageView, scrollFrame: CGRect) {

    super.init(frame: scrollFrame)

    //image.setToImageView(imageView)
    imageView = image
    imageView.clipsToBounds = true
    imageView.userInteractionEnabled = true

    // UIScrollView(frame: bounds)
    delegate = self
    showsVerticalScrollIndicator = false
    showsHorizontalScrollIndicator = false
    backgroundColor = UIColor.blackColor()
   // contentSize = imageView.bounds.size
   // autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
    contentOffset = CGPoint(x: 1000, y: 450)

    addSubview(imageView)

   // setZoomScale()

    setupGestureRecognizer()

}


func setupGestureRecognizer() {
    let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
    doubleTap.numberOfTapsRequired = 2
    addGestureRecognizer(doubleTap)
}

func handleDoubleTap(recognizer: UITapGestureRecognizer) {

    if (zoomScale > minimumZoomScale) {
        setZoomScale(minimumZoomScale, animated: true)
    } else {
        setZoomScale(maximumZoomScale, animated: true)
    }
}


required public init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
override public func layoutSubviews() {
    setZoomScale()
    //setZoomScale(minimumZoomScale, animated: false)
}


public func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {

    return imageView

}


func setZoomScale() {
    let imageViewSize = imageView.bounds.size
    let scrollViewSize = bounds.size
    let widthScale = scrollViewSize.width / imageViewSize.width
    let heightScale = scrollViewSize.height / imageViewSize.height

    minimumZoomScale = min(widthScale, heightScale)
    zoomScale = 1.0
}

public func scrollViewDidZoom(scrollView: UIScrollView) {
    let imageViewSize = imageView.frame.size
    let scrollViewSize = bounds.size

    let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
    let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0

    scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
} 

}


Aucun commentaire:

Enregistrer un commentaire