美文网首页
Swift - 滚动视图(UIScrollView)的用法

Swift - 滚动视图(UIScrollView)的用法

作者: MangoJ | 来源:发表于2016-07-29 13:48 被阅读291次
    import UIKit
    
    class ViewController: UIViewController ,UIScrollViewDelegate{
    override func viewDidLoad() {
        super.viewDidLoad()
        var SCREEN_WIDTH  = self.view.frame.width
        var SCREEN_HIGHT = self.view.frame.height
    
     let scrollView = UIScrollView()
        scrollView.frame = CGRectMake(0, 260, SCREEN_WIDTH, 100)
        let imageView = UIImageView(image: UIImage(named: "hello"))
        scrollView.contentSize = imageView.bounds.size
        scrollView.addSubview(imageView)
        scrollView.delegate = self
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
       //设置最大最小缩放比
        scrollView.minimumZoomScale = 0.1
        scrollView.maximumZoomScale = 3
        self.view.addSubview(scrollView)
    
    }
    
      func scrollViewDidScroll(scrollView: UIScrollView) {
        print("x:\(scrollView.contentOffset.x) y:\(scrollView.contentOffset.y)")
    }
    
    //手势放大缩小
    //要实现放大缩小功能,需要指定UIScrollView的允许缩放最大比例和最小比例(默认都是是1.0)。
    //  同时delegate属性指定一个委托类,委托类要继承UIScrollViewDelegate协议,并在委托类中实现viewForZoomingInScrollView方法。
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        for subview : AnyObject in scrollView.subviews {
            if subview.isKindOfClass(UIImageView) {
                return subview as? UIView
            }
        }
        return nil
     }
    
    
    }

    相关文章

      网友评论

          本文标题:Swift - 滚动视图(UIScrollView)的用法

          本文链接:https://www.haomeiwen.com/subject/yzdzjttx.html