本文主要是介绍[IOS]ScrollView PageControl 制作引导页,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ScrollView & PageControl 制作引导页
此空间用于制作app的开始画面。
在实际做app的时候,可能用纯代码实现会更加方便,但需要的能力也更大,特别是对布局的掌控!此能力需要看更多的开源代码来学习!不能太依赖StoryBoard。
(Demo)代码实现:
先布局好ScrollView,PageControl,以及ScrollView的Delegate。
//
// ViewController.swift
// demo
//
// Created by 颜泽鑫 on 4/10/16.
// Copyright © 2016 颜泽鑫. All rights reserved.
//import UIKitclass ViewController: UIViewController, UIScrollViewDelegate {// 已连线。@IBOutlet weak var ScrollView: UIScrollView!@IBOutlet weak var PageControl: UIPageControl!// 不连线。var btn = UIButton()override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.ScrollView.frame = self.view.frame// 确定内容的宽度,也就是页数。!ScrollView.contentSize = CGSizeMake(4 * self.view.frame.width, 0)ScrollView.pagingEnabled = trueScrollView.showsHorizontalScrollIndicator = false// 此处不能使用storyborad来添加image。// 因为,此处是每张图都要有一个imageView对应,而不是一个ImageView对应各种图。for var i=0;i<4;i++ {let image = UIImage(named: "\(i+1)")let imageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))imageView.image = imagevar frame = imageView.frameframe.origin.x = CGFloat(i)*frame.size.widthimageView.frame = frameScrollView.addSubview(imageView)// 这里一定要写这条代码!// 因为每新增一个image都会把原来的PageControl盖住self.view.addSubview(PageControl)}}func buttonClick(button:UIButton){let controller = UIAlertController(title: "Wellcome to C++!", message: nil, preferredStyle: UIAlertControllerStyle.Alert)let CancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)controller.addAction(CancelAction)self.presentViewController(controller, animated: true, completion: nil)}func scrollViewDidEndDecelerating(scrollView: UIScrollView) {// 通过x的偏移量来计算页数。let index = Int(scrollView.contentOffset.x / self.view.frame.size.width) //获取当前页数PageControl.currentPage = indexPageControl.alpha = 1//在这里添加按钮的渐入效果,当页面滑到第4页时出现if(index == 3) {self.PageControl.alpha = 0// 此处3*self.view.frame.width的3,// 指的是对于ScrollView上的x位置。self.btn.frame = CGRectMake(3*self.view.frame.width, self.view.frame.height, self.view.frame.width, 50)self.btn.setTitle("开启C++之旅", forState: UIControlState.Normal)self.btn.titleLabel?.font = UIFont.systemFontOfSize(20)self.btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)// 此处的RGB都是小数值,需要把RGB值除以255self.btn.backgroundColor = UIColor(red: CGFloat(0), green: CGFloat(0.3), blue: CGFloat(1), alpha: CGFloat(1))self.btn.alpha = 0self.btn.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)UIView.animateWithDuration(1.5, delay: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void inself.btn.frame = CGRectMake(3*self.view.frame.width, self.view.frame.height-100, self.view.frame.width, 50)self.btn.alpha = 1//注意把按钮添加到scrollView上,不要添加到imageView上,会无法点击self.ScrollView.addSubview(self.btn)}, completion: nil)}}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}
}
反思:此Demo遇到了大量不能使用StoryBoard来增加控件的情况,我个人理解是,因为某些控件只有在特定的情况下才能显现和使用,如果把它直接放在StoryBoard的话,就可能会出现无法预计的效果。
ScollView的空间实际长度(contentsize)是界面的3倍,也就是说,实际上他是一个控件在不同区域的显示,所以不能在storyboard中添加三个imageView来显示不同的图片。
这也提醒我,除了学会如何用StoryBoard以外,还会学会如何手写纯代码实现相应的功能。
如何只显示一次引导页:
简单地说,就是在AppDelegate里面判断是否是第一次打开此app。
func buttonClick(button:UIButton){let sb = UIStoryboard(name: "Main", bundle: nil)let secondVC = sb.instantiateViewControllerWithIdentifier("second")self.presentViewController(secondVC, animated: true, completion: nil)}
class AppDelegate: UIResponder, UIApplicationDelegate {var window: UIWindow?func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {// Override point for customization after application launch.//判断App是否第一次启动if(!NSUserDefaults.standardUserDefaults().boolForKey("firstLaunch")){
// self.window?.rootViewController = ViewController()// self.window?.makeKeyAndVisible()NSUserDefaults.standardUserDefaults().setBool(true, forKey: "firstLaunch")}else{let sb = UIStoryboard(name: "Main", bundle: nil)let secondVC = sb.instantiateViewControllerWithIdentifier("second")self.window?.rootViewController = secondVC// self.window?.makeKeyAndVisible()NSUserDefaults.standardUserDefaults().setBool(true, forKey: "firstLaunch")}return true}
}
这篇关于[IOS]ScrollView PageControl 制作引导页的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!