1.在AppDelegate中判断是否第一次进入,更换根视图
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. self.window = UIWindow(frame: UIScreen.mainScreen().bounds) if(!NSUserDefaults.standardUserDefaults().boolForKey("firstLaunch")) { NSUserDefaults().setBool(true,forKey: "firstLaunch") NSLog("first lanuch") var vc : WelcomeController = WelcomeController() self.window?.rootViewController = vc } else{ NSLog("not first lanuch") var navC = ViewController() //获取名为Main.storyboard的文件 var storyboard = UIStoryboard(name: "Main",bundle:nil) self.window?.rootViewController = storyboard.instantiateViewControllerWithIdentifier("MainController") as? UINavigationController } self.window?.backgroundColor = UIColor.whiteColor() self.window?.makeKeyAndVisible() return true }
2.在WelcomeController页面中设置相对应的布局
//页面布局 func setUI() { var scrollView = UIScrollView(frame: CGRectMake(0,320,640)) scrollView.contentSize = CGSizeMake(960,0) scrollView.pagingEnabled = true var imageView1 = UIImageView(frame: CGRectMake(0,640)) imageView1.image = UIImage(named: "top_image1") scrollView.addSubview(imageView1) var imageView2 = UIImageView(frame: CGRectMake(320,640)) imageView2.image = UIImage(named: "top_image2") scrollView.addSubview(imageView2) var imageView3 = UIImageView(frame: CGRectMake(640,640)) imageView3.image = UIImage(named: "top_image3") imageView3.userInteractionEnabled = true scrollView.addSubview(imageView3) var button :UIButton = UIButton() button.setTitle("跳转",forState: UIControlState.Normal) button.frame = CGRectMake(46,371,230,37) //为button添加点击事件 button.addTarget(self,action: "firstpressed",forControlEvents: UIControlEvents.TouchUpInside) imageView3.addSubview(button) var page = UIPageControl(frame: CGRectMake(135,600,50,20)) page.numberOfPages = 3 scrollView.addSubview(page) self.view.addSubview(scrollView) }
3.为按钮添加点击事件
func firstpressed() { //若在AppDelegate中self.windows.rootviewcontroller = WelcomeController()则 // var nav = UIStoryboard(name: "Main",bundle: nil).instantiateViewControllerWithIdentifier("MainController") as? UINavigationController //若var vc = storyboard.instantiateViewControllerWithIdentifier("welcomeController") as? UIViewController // self.window?.rootViewController = vc,则 var nav = self.storyboard?.instantiateViewControllerWithIdentifier("MainController") as? UINavigationController self.presentViewController(nav!,animated: false,completion: nil) }原文链接:https://www.f2er.com/swift/326832.html