`
iaiai
  • 浏览: 2145052 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[iOS]简单的APP引导页的实现 (Swift)

 
阅读更多
在第一次打开APP或者APP更新后通常用引导页来展示产品特性

下面是swift简单的实现,图片自己从网上找吧
AppDelegate.swift
//
//  AppDelegate.swift
//  palmICT_swift
//
//  Created by iaiai on 15/12/17.
//  Copyright (c) 2015年 iaiai. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        NSUserDefaults.standardUserDefaults().objectForKey("FirstLanght") == nil ? enterGuide() : enterMain()
        
        return true
    }
    
    func enterGuide(){
        var guideController = GuideViewController()
        self.window!.rootViewController = guideController
        self.window!.backgroundColor = UIColor.whiteColor()
        self.window!.makeKeyAndVisible()
        
        //下面的方式是读取sb中定义的界面
//        var storyboard = UIStoryboard(name: "Main", bundle: nil)
//        var guidanceViewController = storyboard.instantiateViewControllerWithIdentifier("GuidanceVC") as! GuideViewController
//        self.window!.rootViewController = guidanceViewController
    }
    
    func enterMain(){
        self.window!.rootViewController = ViewController()
        self.window!.backgroundColor = UIColor.whiteColor()
        self.window!.makeKeyAndVisible()
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}


GuideViewController.swift
//
//  GuideViewController.swift
//  palmICT_swift
//
//  Created by iaiai on 15/12/17.
//  Copyright (c) 2015年 iaiai. All rights reserved.
//

import UIKit

class GuideViewController: UIViewController {
    
    var scrollView:UIScrollView!
    
    var pageControl:UIPageControl!
    var startButton:UIButton!
    
    var numOfPages = 4
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //滚动
        scrollView = UIScrollView()
        scrollView.frame = self.view.bounds
        scrollView.delegate = self
        scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * CGFloat(numOfPages), self.view.bounds.size.height)
        scrollView.pagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.scrollsToTop = false
        
        for i in 0..<numOfPages {
            var image = UIImage(named: "guide_0\(i + 1).png")
            var imageView = UIImageView(image: image)
            
            imageView.frame = CGRectMake(self.view.bounds.size.width * CGFloat(i), 0, self.view.bounds.size.width, self.view.bounds.size.height)
            
            scrollView.addSubview(imageView)
        }
        
        scrollView.contentOffset = CGPointZero
        self.view.addSubview(scrollView)
        
        //分页圆点
        pageControl = UIPageControl()
        pageControl.frame = CGRect(x: self.view.frame.origin.x, y: self.view.frame.size.height-60, width: self.view.frame.size.width, height: 30)
        pageControl.numberOfPages = numOfPages
        pageControl.currentPage = 0
        self.view.addSubview(pageControl)
        
        startButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
        startButton.frame = CGRect(x: self.view.frame.size.width/2-50, y: self.view.frame.size.height-120, width: 100, height: 30)
        startButton.backgroundColor = UIColor.blueColor()
        startButton.alpha = 0
        startButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        startButton.setTitle("立即体验", forState: UIControlState.Normal)
        startButton.layer.cornerRadius = 5  //设置圆角
        startButton.addTarget(self,action:"jump:",forControlEvents:UIControlEvents.TouchUpInside)
        self.view.addSubview(startButton)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    func jump(sender: UIButton!) {
        var viewController = ViewController()
        viewController.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
        presentViewController(viewController, animated: true, completion: nil)
    }
}

extension GuideViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(scrollView: UIScrollView) {
        var offset = scrollView.contentOffset
        // 随着滑动改变pageControl的状态
        pageControl.currentPage = Int(offset.x / view.bounds.width)
        // 因为currentPage是从0开始,所以numOfPages减1
        if pageControl.currentPage == numOfPages - 1 {
            UIView.animateWithDuration(0.5) {
                self.startButton.alpha = 0.8
            }
        } else {
            UIView.animateWithDuration(0.5) {
                self.startButton.alpha = 0.0
            }
        }
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics