最近正好有时间可以用Swift把应用给重写一遍,由于是第一次用swift来开发。所以记录一下。
开发模式用MVVM+RAC来实现高聚合 ,低耦合。
所用到的第三方库
- Alamofire网络请求库
- ReactiveCocoa响应式编程,能够实现代码的高聚合
- EVReflection (2.38.2)数据建模
- SDWebImage网络图片框架
-
IconFont
帮助文档是OC版的使用方式,我写了个swift版本的使用方式
class ViewController: UIViewController {
@IBOutlet weak var icon: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
icon.image = UIImage().imageWithIconFont("\u{e649}", size: 33, color: UIColor.redColor())
}
}
extension UIImage{
func imageWithIconFont(let name:String,let size:CGFloat ,let color:UIColor) -> UIImage {
let imageSize = CGSizeMake(size, size)
UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.mainScreen().scale)
let labelImage:UILabel = UILabel.init(frame: CGRectMake(0, 0, size, size))
labelImage.font = UIFont(name:"iconfont",size:20)
labelImage.text = name
labelImage.textColor = color
labelImage.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext();
return image
}
}
开发中的各个模块
导航栏设置
导航栏设置有两种方案,一种是在自定义导航控制器中设置,另一种是在视图控制器设置,我采用后一种方案,不过为了避免代码重复冗余,我
-
导航栏背景色
self.navigationController?.navigationBar.barTintColor = mainColor
-
导航栏字体颜色控制
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor(),NSFontAttributeName:UIFont.systemFontOfSize(17)];
-
导航栏返回按钮设置
let backImage:UIImage = UIImage().imageWithIconFont("\u{0000e61a}", size: 22, color: UIColor.whiteColor())
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(image:backImage, style:.Plain, target: self, action: #selector(backItemTapped))
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
}
func backItemTapped() {
if self.navigationController?.viewControllers.count>1 {
self.navigationController?.popViewControllerAnimated(true)
}else{
self.dismissViewControllerAnimated(true, completion: nil)
}
}
-
状态栏控制
1,将info.plist文件的View controller-based status bar appearance设置为NO
2,self.navigationController?.navigationBar.barStyle = .Black
标签栏处理:UITabbarController
有两种方案:方案一是在自定义的UITabbarController中设置,方案二是在视图控制器中设置,我采用的是方案一
import UIKit
class MYFTabBarController: UITabBarController,UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self;
setupItemImage()
}
func setupItemImage() {
let selectedImages:[UIImage] = [UIImage().imageWithIconFont("\u{E60A}", size: 22, color: UIColor.redColor()).imageWithRenderingMode(.AlwaysOriginal),
UIImage().imageWithIconFont("\u{e63d}", size: 22, color: UIColor.redColor()).imageWithRenderingMode(.AlwaysOriginal),
UIImage().imageWithIconFont("\u{E60D}", size: 22, color: UIColor.redColor()).imageWithRenderingMode(.AlwaysOriginal)]
let unSelectedImages:[UIImage] = [UIImage().imageWithIconFont("\u{E603}", size: 22, color: UIColor.lightGrayColor()),
UIImage().imageWithIconFont("\u{e63b}", size: 22, color: UIColor.lightGrayColor()),
UIImage().imageWithIconFont("\u{E616}", size: 22, color: UIColor.lightGrayColor())]
let itemTitles:[String] = ["1","2","3"]
for var i:NSInteger in 0...(viewControllers?.count)!-1{
let controller:UIViewController = viewControllers![i];
controller.tabBarItem.title = itemTitles[i]
controller.tabBarItem.image = unSelectedImages[i]
controller.tabBarItem.selectedImage = selectedImages[i]
}
}
}
extension UIImage{
func imageWithIconFont(let name:String,let size:CGFloat ,let color:UIColor) -> UIImage {
let imageSize = CGSizeMake(size, size)
UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.mainScreen().scale)
let labelImage:UILabel = UILabel.init(frame: CGRectMake(0, 0, size, size))
labelImage.font = UIFont(name:"iconfont",size:size)
labelImage.text = name
labelImage.textColor = color
labelImage.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext();
return image
}
}
效果
MVVM+RAC的使用
其实处理起来很简单,就是把所有数据处理都放到viewModel中,当数据处理结束后通过RAC通知controller或者view