zoukankan      html  css  js  c++  java
  • Swift 图片浏览器

    class ViewController: UIViewController, UIScrollViewDelegate{

        var scrollView: UIScrollView?

        

        var images: NSMutableArray = NSMutableArray(capacity: 5)

        

        override func viewDidLoad() {

            super.viewDidLoad()

            // 

            self.title = "图片浏览"

            

            self.automaticallyAdjustsScrollViewInsets = false

            

            

            scrollView = UIScrollView(frame: self.view.bounds)

            self.view.addSubview(scrollView!)

            

            //不显示滚动条

            scrollView?.showsHorizontalScrollIndicator = false

            scrollView?.showsVerticalScrollIndicator = false

            //设置分页显示

            scrollView?.pagingEnabled = true

            //设置背景颜色

            scrollView?.backgroundColor = UIColor.redColor()

            

            //设置内容视图大小

            scrollView?.contentSize = CGSize( self.view.frame.size.width * 5, height: self.view.frame.size.height)

            scrollView?.delegate = self

            

            //加载图片

            for index in 0...5 {

                

                let image: UIImage! = UIImage(named: "(index).JPG")

                let photoScrollView:PhotoScrollView! = PhotoScrollView(frame: CGRect(x: self.view.frame.size.width * CGFloat(index), y: 0, self.view.frame.size.width, height: self.view.frame.size.height))

                photoScrollView.tag = 200+index

                photoScrollView.backgroundColor = UIColor(red:0.4, green: 1, blue: 1, alpha: 1)

                photoScrollView.image = image

                self.scrollView?.addSubview(photoScrollView)

            }

            

            

        }

        

        func scrollViewDidEndDecelerating(scrollView: UIScrollView)

        {

            let currentIndex = Int(scrollView.contentOffset.x/self.view.bounds.width)

            if currentIndex != index {

                let photoScrollView = scrollView.viewWithTag(200+index) as! PhotoScrollView;

                //还原比例大小

                photoScrollView.setZoomScale(1, animated: false)

            }

            index = currentIndex

        }

        

        var index:NSInteger = 0

    }

     2.设置img

    class PhotoScrollView: UIScrollView, UIScrollViewDelegate {

        var _image:UIImage!

        var image:UIImage! {

            get {

                return self._image

            }

            set {

                _image = newValue

                let imgView:UIImageView = self.viewWithTag(100) as! UIImageView

                imgView.backgroundColor = UIColor.greenColor()

                imgView.image = image

            }

        }

        override init(frame: CGRect) {

            super.init(frame: frame)

            

            let imgView:UIImageView! = UIImageView(frame: self.bounds)

            imgView.tag = 100

            self.addSubview(imgView)

            

            //取消滑动条

            self.showsHorizontalScrollIndicator = false

            self.showsVerticalScrollIndicator = false

            self.delegate = self

            

            //设置最大、最小缩放比例

            self.maximumZoomScale = 3

            self.minimumZoomScale = 1

            

        }

        required init?(coder aDecoder: NSCoder) {

            super.init(coder: aDecoder)

        }

        

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

            return self.viewWithTag(100)

        }

    }

  • 相关阅读:
    解决方法:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
    解决装系统选中的磁盘采用的是GPT分区形式
    破解windows server 2008 的登录密码。有效的
    Arcgis Engine(ae)接口详解(8):临时元素(element)
    Arcgis Engine(ae)接口详解(7):地图(map)操作
    Arcgis Engine(ae)接口详解(5):IGeometry几何高级操作
    Arcgis Engine(ae)接口详解(6):workspace操作
    Arcgis Engine(ae)接口详解(5):IGeometry几何基础操作
    Arcgis Engine(ae)接口详解(3):featureClass的feature编辑和删除
    Arcgis Engine(ae)接口详解(4):featureClass的feature插入
  • 原文地址:https://www.cnblogs.com/ZGSmile/p/5726507.html
Copyright © 2011-2022 走看看