三天上手swift之项目篇一(方法稍许极端)

来源:转载



经过三天看swift的语法(不清楚看回之前文章),我们直接上项目,以下内容有点极端变态,无基础或者没耐心者请绕道!


本人觉得看太多语法没什么意思,只有在用的时候才能记下来,另外,我教大家的是短时间从oc到swift的速成,所以不会讲太多关于API和方法的解释,你能看懂更好,看到不懂的,要么看回之前资料,要么强行记下来,用中文写好用法注释,到自己独立写的时候回来抄即可。



注意点:

以下代码都是从oc原样改写过来swift的,保证逻辑是没有问题的,你只要会点oc,肯定会知道我在做什么,而且方法名称几乎是一样,看到 "?", "!" 千万不要慌,你可以不用写,因为系统会自动帮你纠正,至于想了解语法要看回我之前写的文章


使用方法:

本代码使用方法非常简单,对着抄一遍,不要管报错什么的,不要管太多细节,先用抄来感受里面奥妙和精髓


我们先从零开始搭建项目的框架,在oc中我们都会把main干掉,然后把vc和storyboard也删除,从AppDelegate开始,创建窗口,我们看看swift是怎么做的


import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// 设置全局nav与tab色值
UINavigationBar.appearance().tintColor = UIColor(red: 255.0/255.0, green: 2.0/255.0, blue: 51.0/255, alpha: 1.0)
UITabBar.appearance().tintColor = UIColor(red: 255.0/255.0, green: 2.0/255.0, blue: 51.0/255, alpha: 1.0)
// 创建窗口
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = TabBarController()
window?.makeKeyAndVisible()

return true
}
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:.
}

}

现在app主流框架会嵌套一个tabBarController, 自定义一个NavigationController,就是tabBarController成为窗口的根控制器,NavigationController成为tabBarController的子控制器




我们现在看看tabBarController怎么写


import UIKit
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
addChildViewControllers()
}
private func addChildViewControllers(){
addChildViewController(HomeViewController(), title: "礼物说", imageName: "tabbar_home")
addChildViewController(HotViewController(), title: "热门", imageName: "tabbar_gift")
addChildViewController(ClassifyViewController(), title: "分类", imageName: "tabbar_category")
addChildViewController(MeViewController(), title: "我", imageName: "tabbar_me")
}
private func addChildViewController(controller: UIViewController, title:String, imageName:String){
controller.tabBarItem.image = UIImage(named: imageName)
controller.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
controller.tabBarItem.title = title
let nav = NavigationController()
nav.addChildViewController(controller)
addChildViewController(nav)
}
}

然后看看NavigationController怎么写


import UIKit
class NavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
self.interactivePopGestureRecognizer!.delegate = nil;
let appearance = UINavigationBar.appearance()
appearance.translucent = false
appearance.setBackgroundImage(UIImage.imageWithColor(Color_NavBackground, size: CGSizeMake(1, 1)), forBarMetrics: UIBarMetrics.Default)
var textAttrs: [String : AnyObject] = Dictionary()
textAttrs[NSForegroundColorAttributeName] = UIColor.whiteColor()
textAttrs[NSFontAttributeName] = UIFont.systemFontOfSize(16)
appearance.titleTextAttributes = textAttrs
}
lazy var backBtn: UIButton = UIButton(backTarget: self, action: #selector(NavigationController.backBtnAction))
func backBtnAction() {
self.popViewControllerAnimated(true)
}
override func pushViewController(viewController: UIViewController, animated: Bool) {
if self.childViewControllers.count > 0 {
viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backBtn)
viewController.hidesBottomBarWhenPushed = true
}
super.pushViewController(viewController, animated: animated)
}
}

就是这么简单,主流框架就搭建好了!


最后我跟大家总结一下学习方法,就是不要问为什么,对着敲,敲完以后看到不懂的,回去看我之前写的文章,找语法知识点巩固一下!


我是子祖,来了就点个赞再走,喜欢就关注我,我还会陆续更新更多项目让大家去练手,或者你有什么语言想了解的都可以和我聊聊!




分享给朋友:
您可能感兴趣的文章:
随机阅读: