EdisoniOS日常iOS界面特效篇

iOS:类似于美团/支付宝 城市选择器带拼音搜索实现

2017-03-31  本文已影响895人  SoolyChristina

1. 分析原型

效果图 实现demo
要做的页面是一个UITableViewUISearchController

2. UITabelView

cities.plist
    /// 懒加载 城市数据
    lazy var cityDic: [String: [String]] = { () -> [String : [String]] in
        let path = Bundle.main.path(forResource: "cities.plist", ofType: nil)
        let dic = NSDictionary(contentsOfFile: path ?? "") as? [String: [String]]
        return dic ?? [:]
        }()
    /// 懒加载 热门城市
    lazy var hotCities: [String] = {
        let path = Bundle.main.path(forResource: "hotCities.plist", ofType: nil)
        let array = NSArray(contentsOfFile: path ?? "") as? [String]
        return array ?? []
    }()

3. UISearchController

    /// 搜索控制器
    lazy var searchVC: UISearchController = {
       let searchVc = UISearchController(searchResultsController: self.searchResultVC)
        searchVc.delegate = self
        searchVc.searchResultsUpdater = self
        // 不隐藏导航条
        searchVc.hidesNavigationBarDuringPresentation = false
        searchVc.definesPresentationContext = true
        // 是否关闭蒙版
        searchVc.dimsBackgroundDuringPresentation = false
        searchVc.searchBar.frame = CGRect(x: 0, y: 0, width: ScreenWidth - 114, height: 44)
        searchVc.searchBar.placeholder = "输入城市名或拼音查询"
        searchVc.searchBar.delegate = self
        return searchVc
    }()
    /// 搜索结果控制器
    lazy var searchResultVC: ResultTableViewController = ResultTableViewController()
let titleView = UIView(frame: searchVC.searchBar.frame)
        titleView.addSubview(searchVC.searchBar)
        self.navigationItem.titleView = titleView
func chineseToPinyin() -> String {
       
       let stringRef = NSMutableString(string: self) as CFMutableString
       // 转换为带音标的拼音
       CFStringTransform(stringRef,nil, kCFStringTransformToLatin, false)
       // 去掉音标
       CFStringTransform(stringRef, nil, kCFStringTransformStripCombiningMarks, false)
       let pinyin = stringRef as String
       
       return pinyin
   }

GitHub地址

上一篇下一篇

猜你喜欢

热点阅读