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.

        }

        

    }

     

  • 相关阅读:
    并发编程(四)—— ThreadLocal源码分析及内存泄露预防
    并发编程(三)—— ReentrantLock的用法
    并发编程(二)—— CountDownLatch、CyclicBarrier和Semaphore
    并发编程(一)—— volatile关键字和 atomic包
    Java 多线程(四)—— 单例模式
    Java 多线程(三)—— 线程的生命周期及方法
    Java 多线程(二)—— 线程的同步
    星空雅梦
    星空雅梦
    星空雅梦
  • 原文地址:https://www.cnblogs.com/madaha/p/4145948.html
Copyright © 2011-2022 走看看