iOS开发——UI篇Swift篇&UIScrollView

时间:2023-03-08 16:46:55
iOS开发——UI篇Swift篇&UIScrollView

UIScrollView

     //返回按钮事件
     @IBAction func backButtonClick()
     {
         self.navigationController?.popViewControllerAnimated(true)
     }

     //创建滚动视图
     var mainScrollView:UIScrollView!

     var view1:UIView!
     var view2:UIView!
     var view3:UIView!

     //创建UIPageControl实例
     var pageCtrl:UIPageControl!

     override func viewDidLoad() {
         super.viewDidLoad()

         titleLabel.text = titleString

         // Do any additional setup after loading the view.

         //创建滚动视图
         mainScrollView = UIScrollView(frame: CGRectMake(, , , ))

         //设置背景色
         mainScrollView.backgroundColor = UIColor.grayColor()

         //添加到视图上
         self.view.addSubview(mainScrollView)

         //创建3个视图,添加到ScrollView上
         view1 = UIView(frame: CGRectMake(, , , ))
         view1.backgroundColor = UIColor.redColor()
         mainScrollView.addSubview(view1)

         view2 = UIView(frame: CGRectMake(, , , ))
         view2.backgroundColor = UIColor.greenColor()
         mainScrollView.addSubview(view2)

         view3 = UIView(frame: CGRectMake(, , , ))
         view3.backgroundColor = UIColor.blueColor()
         mainScrollView.addSubview(view3)

         //设置scrollView的容器大小
         mainScrollView.contentSize = CGSizeMake(*, )

         //设置滚动条风格
         mainScrollView.indicatorStyle = UIScrollViewIndicatorStyle.White

         //设置水平方向滚动条是否可见
         mainScrollView.showsHorizontalScrollIndicator = false

         //设置垂直方向滚动条是否可见
         mainScrollView.showsVerticalScrollIndicator = false

         //设置成翻页滚动
         mainScrollView.pagingEnabled = true

         mainScrollView.delegate = self

         mainScrollView.maximumZoomScale = 2.0;//允许放大2倍
         mainScrollView.minimumZoomScale = 0.5;//允许放大到0.5倍

         //页面控制器

         //创建UIPageControl实例
         pageCtrl = UIPageControl(frame: CGRectMake(, , , ))

         //添加到视图
         self.view.addSubview(pageCtrl)

         //设置总页数
         pageCtrl.numberOfPages = 

         //当前显示到第几页面
         pageCtrl.currentPage = 

         //添加事件
         pageCtrl.addTarget(self, action: "pageTurn:", forControlEvents: UIControlEvents.ValueChanged)

     }

     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }

     /*
     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
         // Get the new view controller using segue.destinationViewController.
         // Pass the selected object to the new view controller.
     }
     */

     //MARK: - UIScrollViewDelegate

     func scrollViewDidScroll(scrollView: UIScrollView)
     {
         //视图滚动中一直触发
     }

     func scrollViewWillBeginDragging(scrollView: UIScrollView)
     {
         //刚开始拖动视图时触发一次
     }

     func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
     {
         //将要结束拖动触发一次
     }

     func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool)
     {
         //拖动结束,触发一次
     }

     func scrollViewWillBeginDecelerating(scrollView: UIScrollView)
     {
         //拖动接触后,开始减速
     }

     func scrollViewDidEndDecelerating(scrollView: UIScrollView)
     {
         //减速完毕,恢复到静止状态

        //获取scrollView视图滚动的x坐标
         var offX:CGFloat = scrollView.contentOffset.x

         //计算当前是第几屏
         var index:Int = (Int)(offX / )

         //设置分页指示器currentPage值
         pageCtrl.currentPage = index;
     }

     //缩放功能-返回缩放的视图
     func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?
     {
         //获取当前滚动视图移动的X坐标
         var offsetX = scrollView.contentOffset.x
         println(offsetX)

          //如果没有滚动,返回view1
         {
             return view1
         }
          //滚动300,返回view2
         {
             return view2
         }
          //滚动600,返回view3
         {
             return view3
         }

         return nil
     }

     func scrollViewDidZoom(scrollView: UIScrollView)
     {
         //缩放过程中一直调用
     }

     func scrollViewWillBeginZooming(scrollView: UIScrollView, withView view: UIView!)
     {
         //开始缩放,调用一次
     }

     func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView!, atScale scale: CGFloat)
     {
         //缩放结束,调用一次
     }

     func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView)
     {
         println("scrollViewDidEndScrollingAnimation")
     }

     func scrollViewShouldScrollToTop(scrollView: UIScrollView) -> Bool
     {
         return false
     }

     func scrollViewDidScrollToTop(scrollView: UIScrollView)
     {
         println("scrollViewDidScrollToTop")
     }

     //MARK: - 

     //分页控制器value改变事件
     func pageTurn(sender:AnyObject?)
     {
         //获取分页控制器
 //        var newPageCtl = sender as UIPageControl

         //2015年5月2后修改
         var newPageCtl = sender as! UIPageControl

         //获取分页控制器当前页
         var index = newPageCtl.currentPage

         //设置ScrollView的x移动距离
         mainScrollView.contentOffset = CGPointMake((CGFloat)( * index), )
     }