• swift UIScrollView


    //

    //  ScrollViewController.swift

    //  UIControlDemo

    //

    //  Created by   on 14/12/1.

    //  Copyright (c) 2014 马大哈. All rights reserved.

    //

     

    import UIKit

     

    class ScrollViewController: BaseViewController ,UIScrollViewDelegate{

        

        var scroll: UIScrollView?

        var pageControl:UIPageControl?

     

        override func viewDidLoad() {

            super.viewDidLoad()

            

            self.title = "当前索引: 1 / 5"

            

            scroll = UIScrollView(frame: CGRectMake(0, 100, self.view.frame.size.width, 200))

            scroll?.backgroundColor = UIColor.redColor()

            scroll?.delegate = self

            scroll?.showsHorizontalScrollIndicator = true

            scroll?.showsVerticalScrollIndicator = true

            scroll?.pagingEnabled = true

            self.view.addSubview(scroll!)

            

            for var indexInt = 0; indexInt < 5; ++indexInt{

                

                var xLoca = CGFloat(indexInt) * CGFloat(self.view.frame.size.width)

                

                var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton

                button.backgroundColor = .grayColor()

                button.frame = CGRectMake(xLoca, 0, self.view.frame.size.width, 200)

                button.setTitleColor(UIColor.whiteColor(), forState:.Normal)

                button.setTitle("点击按钮", forState: UIControlState.Normal)

                button.titleLabel!.font = UIFont.boldSystemFontOfSize(CGFloat(25))

                button.setImage(UIImage(named:""), forState: UIControlState.Normal)

                button.contentMode = UIViewContentMode.ScaleAspectFit

                scroll!.addSubview(button)

                            

            }

            

            scroll?.contentSize = CGSizeMake(5*self.view.frame.size.width, 200)

            scroll?.setContentOffset(CGPointMake(0, 0), animated: true)

      

            pageControl = UIPageControl(frame: CGRectMake(0, 320, self.view.frame.size.width,40))

            pageControl?.backgroundColor = UIColor.clearColor()

            pageControl?.numberOfPages = 5

            pageControl?.currentPage = 0

            pageControl?.pageIndicatorTintColor = UIColor.blackColor()

            pageControl?.currentPageIndicatorTintColor = UIColor.redColor()

            pageControl?.userInteractionEnabled = false

            self.view.addSubview(pageControl!)

            

        }

        

        func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

            var index = Int(scrollView.contentOffset.x/scrollView.frame.size.width)

            pageControl?.currentPage = index

            self.title = "当前索引: (index+1) / 5"

        }

        

        override func didReceiveMemoryWarning() {

            super.didReceiveMemoryWarning()

            // Dispose of any resources that can be recreated.

        }

        

    }

     

  • 相关阅读:
    如何心无旁鹜的编程
    [转]虚拟现实和现实增强技术带来的威胁
    Mac上好用软件集锦
    无论如何都要来报到
    Unity3D脚本语言UnityScript初探
    X3DOM新增剪裁平面节点ClipPlane支持
    用Perl编写Apache模块续二
    如何浪费自己青春
    macbook 我们需要买吗
    看了一本Unity3D的教程
  • 原文地址:https://www.cnblogs.com/madaha/p/4145948.html
Copyright © 2020-2023  润新知