簡單導航頁製作--代碼

/

//  ViewController.swift

//  welcomePage2

//

//  Created by 曾志良 on 15/9/30.

//  Copyright (c) 2015 曾志良. All rights reserved.

//


import UIKit


class ViewController: UIViewController,UIScrollViewDelegate {

    var scrollview=UIScrollView()

    var pageController=UIPageControl()

    var but=UIButton()


    override func viewDidLoad() {

        super.viewDidLoad()

        

        pageController.center=CGPointMake(self.view.frame.width/2, self.view.frame.height-30)

        pageController.currentPageIndicatorTintColor=UIColor.redColor()

        pageController.pageIndicatorTintColor=UIColor.whiteColor()

        pageController.numberOfPages=4

        pageController.addTarget(self, action: "scrollViewDidEndDecelerating", forControlEvents: UIControlEvents.ValueChanged)

        

        

        

        

        

        scrollview.frame=self.view.bounds

        scrollview.contentSize=CGSizeMake(4*self.view.frame.width, 0)

        scrollview.pagingEnabled=true

        scrollview.bounces=false

        scrollview.showsHorizontalScrollIndicator=false

        scrollview.delegate=self

        self.view.addSubview(scrollview)

       

        


        

        for(var i=0;i<4;i++){

           let image=UIImage(named: "1.JPG")

            let imageview=UIImageView(frame: CGRectMake(0, 0, self.view.frame.widthself.view.frame.height))

            imageview.image=image

            var frame=imageview.frame

            frame.origin.x=CGFloat(i)*frame.size.width

            imageview.frame=frame

            scrollview.addSubview(imageview)

             self.view.addSubview(pageController)

            

        

        

        }

        

        

        

        

    }

    

    func scrollViewDidEndDecelerating(scrollView: UIScrollView){

        var index=Int(scrollview.contentOffset.x/self.view.frame.size.width)

        pageController.currentPage=index

        

        if (index==3){

            self.but.frame=CGRectMake(3*self.view.frame.width, self.view.frame.height, self.view.frame.width, 30)

        

            self.but.setTitle("welcome!", forState: UIControlState.Normal)

            self.but.titleLabel?.font=UIFont.systemFontOfSize(20.0)

            self.but.setTitleColor(UIColor.redColor(), forState: UIControlState.Highlighted)

            self.but.backgroundColor=UIColor.brownColor()

            self.but.alpha=0

            self.but.addTarget(self, action: "butClick:", forControlEvents: UIControlEvents.TouchUpInside)

            UIView.animateWithDuration(1, delay: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

                self.but.frame=CGRectMake(3*self.view.frame.width, self.view.frame.height-100, self.view.frame.width, 50)

                self.but.alpha=1

                self.scrollview.addSubview(self.but)

     

                }, completion: nil)

            

            

        }

        

    }

    func butClick(but:UIButton){

        let sb=UIStoryboard(name: "Main", bundle: nil)

        let second=sb.instantiateViewControllerWithIdentifier("second")as!UIViewController

        self.presentViewController(second, animated: true, completion: nil)

    

    }

    

    

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章