专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Swift

[iOS]容易的APP引导页的实现 (Swift)

发布时间:2011-06-30 07:20:43 文章来源:www.iduyao.cn 采编人员:星星草
[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
            }
        }
    }
}
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: