Swift開發(fā)應(yīng)用實踐

最近正好有時間可以用Swift把應(yīng)用給重寫一遍,由于是第一次用swift來開發(fā)。所以記錄一下。
開發(fā)模式用MVVM+RAC來實現(xiàn)高聚合 ,低耦合。

所用到的第三方庫

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
    }
}

開發(fā)中的各個模塊

導(dǎo)航欄設(shè)置

導(dǎo)航欄設(shè)置有兩種方案,一種是在自定義導(dǎo)航控制器中設(shè)置,另一種是在視圖控制器設(shè)置,我采用后一種方案,不過為了避免代碼重復(fù)冗余,我

  • 導(dǎo)航欄背景色

self.navigationController?.navigationBar.barTintColor = mainColor
  • 導(dǎo)航欄字體顏色控制

self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor(),NSFontAttributeName:UIFont.systemFontOfSize(17)];
  • 導(dǎo)航欄返回按鈕設(shè)置

     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)
        }
    }
  • 狀態(tài)欄控制

1,將info.plist文件的View controller-based status bar appearance設(shè)置為NO
2,self.navigationController?.navigationBar.barStyle = .Black

標(biāo)簽欄處理:UITabbarController

有兩種方案:方案一是在自定義的UITabbarController中設(shè)置,方案二是在視圖控制器中設(shè)置,我采用的是方案一

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
    }
}

效果

12.gif

MVVM+RAC的使用

其實處理起來很簡單,就是把所有數(shù)據(jù)處理都放到viewModel中,當(dāng)數(shù)據(jù)處理結(jié)束后通過RAC通知controller或者view

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容