zoukankan      html  css  js  c++  java
  • 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.

        }

        

    }

     

  • 相关阅读:
    (转载)构建public APIs与CORS
    SpringMVC 参数注入
    java删除文件夹
    idea 自动提示生成 serialVersionUID
    JSP自定义tag
    gradle中使用嵌入式(embedded) tomcat, debug 启动
    spring in action 4th --- quick start
    Date, TimeZone, MongoDB, java中date的时区问题
    spring boot 添加拦截器
    HTTP status code
  • 原文地址:https://www.cnblogs.com/madaha/p/4145948.html
Copyright © 2011-2022 走看看