swift中UIScrollView的使用(图片缩放)

github学习地址:https://github.com/potato512/SYSwiftLearning

效果图


关键源码

注意事项:

(1)缩放时将图片视图UIImageView的父视图设置为UIScrollView(A),然后再将UIScrollView的父视图设置为UIScrollView(B);同时设置A的delegate方法,以及设置B的contentSize;

(2)缩放时将图片一直居中显示,即在代理方法"scrollViewDidZoom"中设置;

// MARK: - 视图
    
func setUI()
{
        self.mainScrollView = UIScrollView(frame: CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 280.0))
        
        // 添加到父视图
        self.view.addSubview(self.mainScrollView)
     
        // 添加子视图label
        var originX:CGFloat = 0.0
        for number in 1...3
        {
            let scrollview = UIScrollView(frame: CGRectMake(originX, 0.0, CGRectGetWidth(self.mainScrollView.bounds), CGRectGetHeight(self.mainScrollView.bounds)))
            self.mainScrollView.addSubview(scrollview)
            if 1 == number
            {
                scrollview.backgroundColor = UIColor.brownColor()
            }
            else if 2 == number
            {
                scrollview.backgroundColor = UIColor.blueColor()
            }
            else
            {
                scrollview.backgroundColor = UIColor.orangeColor()
            }
            
            let imageview = UIImageView(frame: scrollview.bounds)
            scrollview.addSubview(imageview);
            imageview.contentMode = .ScaleAspectFit
            imageview.backgroundColor = UIColor.purpleColor()
            var image = UIImage(named: "01")
            if 2 == number
            {
                image = UIImage(named: "02")
            }
            else if 3 == number
            {
                image = UIImage(named: "03")
            }
            imageview.image = image
            
            
            originX = (CGRectGetMinX(scrollview.frame) + CGRectGetWidth(scrollview.frame))
            
            
            // 两个手指拿捏缩放
            scrollview.minimumZoomScale = 0.3
            scrollview.maximumZoomScale = 2.0
            scrollview.delegate = self
            
            // 双击缩放
            let doubleTap = UITapGestureRecognizer(target: self, action: Selector("doubleClick:"))
            doubleTap.numberOfTapsRequired = 2
            imageview.userInteractionEnabled = true
            imageview.addGestureRecognizer(doubleTap)
        }
        
        // 背景颜色
        self.mainScrollView.backgroundColor = UIColor(red: CGFloat(CGFloat(random()) / CGFloat(RAND_MAX)), green: CGFloat(CGFloat(random()) / CGFloat(RAND_MAX)), blue: CGFloat(CGFloat(random()) / CGFloat(RAND_MAX)), alpha: 1.0)
        // 其他属性
        self.mainScrollView.scrollEnabled = true // 可以上下滚动
        self.mainScrollView.scrollsToTop = true // 点击状态栏时,可以滚动回顶端
        self.mainScrollView.bounces = true // 反弹效果,即在最顶端或最底端时,仍然可以滚动,且释放后有动画返回效果
        self.mainScrollView.pagingEnabled = true // 分页显示效果
        self.mainScrollView.showsHorizontalScrollIndicator = true // 显示水平滚动条
        self.mainScrollView.showsVerticalScrollIndicator = true // 显示垂直滚动条
        self.mainScrollView.indicatorStyle = UIScrollViewIndicatorStyle.White // 滑动条的样式
        // 设置内容大小
        self.mainScrollView.contentSize = CGSizeMake(originX, CGRectGetHeight(self.mainScrollView.bounds))
}
// MARK: - UIScrollViewDelegate
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?
{
        print("1 scrollViewDidZoom")
        
        // 设置被缩放的对应视图
        for imageview in scrollView.subviews
        {
            if imageview is UIImageView
            {
                return imageview
            }
        }
        
        return nil
}
    
func scrollViewWillBeginZooming(scrollView: UIScrollView, withView view: UIView?)
{
        print("2 scrollViewWillBeginZooming")
        
        print("view = \(view)")
}
    
func scrollViewDidZoom(scrollView: UIScrollView)
{
        print("3 scrollViewDidZoom")
        
        // 居中显示
        let imageview = scrollView.subviews.first as! UIImageView
        self.centerShow(scrollView, imageview: imageview)
}
    
func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat)
{
        print("4 scrollViewDidEndZooming")
        
        print("scale = \(scale),view = \(view)")
        
        // 缩放效果
        // 放大或缩小
        if scrollView.minimumZoomScale >= scale
        {
            scrollView.setZoomScale(0.3, animated: true)
        }
        if scrollView.maximumZoomScale <= scale
        {
            scrollView.setZoomScale(2.0, animated: true)
        }
}
// MARK: - 双击缩放
var isScaleBig:Bool = false
func doubleClick(gestureRecognizer:UITapGestureRecognizer)
{
        let imageview = gestureRecognizer.view as! UIImageView
        let scrollview = imageview.superview as! UIScrollView
        
        // 放大缩小
        let scale = scrollview.zoomScale
        if isScaleBig
        {
            scrollview.setZoomScale((scale / 0.3), animated: true)
            isScaleBig = false
        }
        else
        {
            scrollview.setZoomScale((scale * 0.3), animated: true)
            isScaleBig = true
        }
}
func centerShow(scrollview:UIScrollView, imageview:UIImageView)
{
        // 居中显示
        let offsetX = (scrollview.bounds.size.width > scrollview.contentSize.width) ? (scrollview.bounds.size.width - scrollview.contentSize.width) * 0.5 : 0.0;
        let offsetY = (scrollview.bounds.size.height > scrollview.contentSize.height) ?
        (scrollview.bounds.size.height - scrollview.contentSize.height) * 0.5 : 0.0;
        imageview.center = CGPointMake(scrollview.contentSize.width * 0.5 + offsetX,scrollview.contentSize.height * 0.5 + offsetY);
}







你可能感兴趣的:(swift学习)