在Xcode中建立好工程空间(Work Space)之后,并且建立Cocoa Touch Framework工程WkyLib和应用工程WkgJys工程,下面我们就可以开始进行正式开发了。

首先我们进入Cocoa Touch Framework工程WkyLib,先在下面创建如下组用于组织代码:common, model, view, controller,如下图所示:

最老程序员创业开发实训7---Cocoa Touch Framework实现Splash屏幕功能_ide

同时,在WkyLib的目录下,创建相同的目录common, model, view, controller, images:

最老程序员创业开发实训7---Cocoa Touch Framework实现Splash屏幕功能_MVC_02

在view目录下建立新文件,WKYAppSplashView.swift,这个文件是MVC中的视图类,采用Auto Layout技术,将指定的背景图作为UIImageView的图像源,代码如下所示:

import UIKit

public class WKYAppSplashView
{
public init(rootView: UIView, splashImageName: String? = nil) {
auxInt = 0
if let realName = splashImageName {
self.splashImageName = realName
} else {
self.splashImageName = "app_splash.jpg"
}
let splashImage = UIImage(named: self.splashImageName!)
splashImageView = UIImageView(image: splashImage)
splashImageView!.contentMode = UIViewContentMode.ScaleToFill
splashImageView!.setTranslatesAutoresizingMaskIntoConstraints(false)
rootView.addSubview(splashImageView!)
// set the image view width
var widthConstraint = NSLayoutConstraint(
item: splashImageView!,
attribute: NSLayoutAttribute.Width,
relatedBy: NSLayoutRelation.Equal,
toItem: rootView,
attribute: NSLayoutAttribute.Width,
multiplier: 1.0,
constant: 0
)
rootView.addConstraint(widthConstraint)
// set the height
var heightConstraint = NSLayoutConstraint(
item: splashImageView!,
attribute: NSLayoutAttribute.Height,
relatedBy: NSLayoutRelation.Equal,
toItem: rootView,
attribute: NSLayoutAttribute.Height,
multiplier: 1.0,
constant: 0
)
rootView.addConstraint(heightConstraint);
// center the image view
var centerXConstraint = NSLayoutConstraint(
item: splashImageView!,
attribute: NSLayoutAttribute.CenterX,
relatedBy: NSLayoutRelation.Equal,
toItem: rootView,
attribute: NSLayoutAttribute.CenterX,
multiplier: 1.0,
constant: 0
)
rootView.addConstraint(centerXConstraint);
// center the image view
var centerYConstraint = NSLayoutConstraint(
item: splashImageView!,
attribute: NSLayoutAttribute.CenterY,
relatedBy: NSLayoutRelation.Equal,
toItem: rootView,
attribute: NSLayoutAttribute.CenterY,
multiplier: 1.0,
constant: 0
)
rootView.addConstraint(centerYConstraint)
}

public func getOutlets() -> (UIImageView?, Int?){
return (splashImageView, auxInt);
}

public let splashImageView: UIImageView?
public var splashImageName: String?
public let auxInt: Int?
}

上面的代码虽然有点长,但是还是比较容易理解的。代码首先为splashImageView设置图像源,注意splashImageName是Optional String,这是Swift引入的新特性,可以较好的避免空指针异常,程序首先通过常量realName来获取方法参数splashImageName里所含的字符串,如果不为空则赋给splashImageName属性,如果为空,则执行else语名,将splashImageName设置为缺省值,大家可以看一下Swift语言手册,或上节内容,熟悉一下Optional的正规定义,结合本处的代码,也许更容易理解Swift引入的Optional类型的处理方式。

再向下是利用Auto Layout技术画出界面,分为四个步骤,首先设置splashImageView的长、宽和屏幕相同,再设置其中心点的X、Y与屏幕中心重合,这样就实现了将指定图片作为背景图显示的效果。

下面在controller组下创建WKYAppSplashViewController类,代码如下所示:

public class WKYAppSplashViewControler: UIViewController
{
override public func viewDidLoad() {
super.viewDidLoad()
let rootView = self.view
//appSplashView = WKYAppSplashView(rootView: self.view, splashImageName: "app_splash.jpg")
}

override public func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

public var appSplashView: WKYAppSplashView?
}

上面代码比较简单,唯一需要说明的就是viewDidLoad方法中最后注释掉的appSplashView初始化语句,因为对appSplashView进行初始化,会放在具体应用工程中WKYAppSplashViewController子类的viewDidLoad方法中进行。

好了,Cocoa Touch Framework的功能开发就完成了,可以点击运行按钮,如果显示Build Suceed就大功告成了。

下面选择具体应用工程WkgJys,首先建立common, model, view, controller组以利于代码组织。其次在WkgJys目录下,建立comm, model, view, controller, images目录,方法和WkyLib中一样,这里就不再重复了。

我们在WkgJys项目信息页面中,确保已经加入对WkyLib的引用。

接着我们定义新的类JYSAppSplashView类,该类继承于WKYAppSplashView,代码如下所示:

import UIKit
import WkyLib

class JYSAppSplashView: WKYAppSplashView
{
override init(rootView: UIView, splashImageName: String? = nil) {
super.init(rootView: rootView, splashImageName: splashImageName)
}
}

在上面的代码中首先引入了WkyLib这个Framework,然后重载了父类的构造函数,在本类中,只是简单地直接调用父类的相应方法而已。

下面在controller组下加入JYSAppSplashViewController类,代码如下所示:

import UIKit
import WkyLib

class JYSAppSplashViewController: WKYAppSplashViewControler
{
override func viewDidLoad() {
super.viewDidLoad()
appSplashView = JYSAppSplashView(rootView: self.view, splashImageName: "app_splash.jpg")
}
}

这段代码很简单,会调用JYSAppSplashView的构造函数,绘制整个界面。

现在我们选择WkgJys为活跃工程,点击运行按钮,这时我们就可以看到Splash页面了。

这里还想多说几句,其实IOS应用中,启动画面是由LaunchScreen.xib来定义的(plist.info文件中定义),我们的Splash页面,实际上是在这个页面之后的一个页面。引入这个页面主要是完成自定义动态启动页面的需求,另外也是为了与Android平台保持一致。
在下一节中,我们将讲一下怎么实现Splash页面一闪而过,进入主界面的实现方式,下节再见。


华丽的分隔线