国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

swift-教你如何實(shí)現(xiàn)導(dǎo)航上的UISearchController動(dòng)畫效果。

2019-11-14 17:56:08
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

   這個(gè)代碼片段是我這周我從網(wǎng)上找了各種資料然后經(jīng)過自己的修改終于弄好了導(dǎo)航的上下動(dòng)畫效果:

step1:==>因?yàn)檫@個(gè)搜索要有動(dòng)畫效果,所以這個(gè)頁(yè)面必須要有一個(gè)導(dǎo)航控制器:

//1.自定義創(chuàng)建導(dǎo)航控制器

這個(gè)頁(yè)面我是從其他頁(yè)面跳轉(zhuǎn)過來(lái)的,跳轉(zhuǎn)之前我自定義了一個(gè)導(dǎo)航控制器:

   let actionSearchTable=searchTable();

        let navVC = UINavigationController(rootViewController: actionSearchTable);

        navVC.navigationBar.barStyle = UIBarStyle.BlackTranslucent;

        self.PResentViewController(navVC, animated: true, completion: nil);

//2.點(diǎn)擊搜索跳轉(zhuǎn)到 searchTable.swift,這個(gè)頁(yè)面我繼承的是 UITableViewController,而不是UiViewController,一定要注意,不然每當(dāng)點(diǎn)擊一次搜索取消的時(shí)候table上面會(huì)多出一片空白,這個(gè)原理我還不是太明白,如果你們發(fā)現(xiàn)了其中的原理希望能指點(diǎn)一二。

這個(gè)表格的數(shù)據(jù)源我引用的是一個(gè)txt文件。格式如下圖:

 

////  searchResultTable.swift//  搜索框////  Created by 盧洋 on 15/11/6.//  Copyright © 2015年 奈文摩爾. All rights reserved.//import UIKitclass searchTable: UITableViewController,UISearchBarDelegate{    lazy var dismissBtn: UIButton = { UIButton(frame: CGRectMake(0, 0, 24, 24)) }();//返回按鈕        var itemsString:[String]!    var searcher:UISearchController!    var searchBars:UISearchBar?        struct SearchControllerRestorableState {        var wasActive = false        var wasFirstResponder = false    }    var restoredState = SearchControllerRestorableState();        //初始化函數(shù)    override func viewDidLoad() {        super.viewDidLoad();        self.title="查找商家";        initView();            }        //初始化UI    func initView(){        dismissBtnPrepare();        //創(chuàng)建Table        self.tableView=UITableView(frame: CGRectMake(0, 80, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height));        self.tableView.delegate=self;        self.tableView.dataSource=self;        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cells")                //1.讀取表格數(shù)據(jù)        let tablePath = NSBundle.mainBundle().pathForResource("states", ofType: "txt")!;        let tableData=try! NSString(contentsOfFile: tablePath, encoding: NSUTF8StringEncoding);        itemsString = tableData.componentsSeparatedByString("/n") as [String];                let src = searchResultTable(data: itemsString)        searcher = UISearchController(searchResultsController: src)                searcher.searchResultsUpdater = src;        //獲取焦點(diǎn)時(shí)有陰影效果        searcher.dimsBackgroundDuringPresentation=true;        //獲取焦點(diǎn)導(dǎo)航向上移的動(dòng)畫效果        searcher.hidesNavigationBarDuringPresentation=true;        searchBars = searcher.searchBar        tableView.tableHeaderView = searchBars        searchBars?.delegate=self;        searchBars?.placeholder="輸入商家名稱";        //取消按鈕顏色和文本框光標(biāo)顏色        searchBars?.tintColor=UIColor.blueWithTabbar();        //搜索框背景顏色        //searchBars?.barTintColor=UIColor.blackColor();        searcher.searchBar.sizeToFit();        self.tableView.reloadData();
     //背景充滿導(dǎo)航 definesPresentationContext
= true; } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) // Restore the searchController's active state. if restoredState.wasActive { searcher.active = restoredState.wasActive restoredState.wasActive = false if restoredState.wasFirstResponder { searcher.searchBar.becomeFirstResponder() restoredState.wasFirstResponder = false } } } override func viewDidDisappear(animated: Bool) { super.viewDidAppear(animated); //2.3將狀態(tài)欄變?yōu)榘咨?/span> UIapplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent; } func searchBarSearchButtonClicked(searchBar: UISearchBar) { searchBar.resignFirstResponder() } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return itemsString.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cells", forIndexPath: indexPath) cell.textLabel!.text = itemsString[indexPath.row]; return cell } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { //取消選中的樣式 tableView.deselectRowAtIndexPath(indexPath, animated: true); //獲取點(diǎn)擊的行索引 if(indexPath.row == 0){ } } /** 返回按鈕 */ func dismissBtnPrepare(){ dismissBtn.setImage(UIImage(named: "img.bundle/cancel"), forState: UIControlState.Normal) dismissBtn.addTarget(self, action: "dismissLogin", forControlEvents: UIControlEvents.TouchUpInside) self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: dismissBtn) } /** 釋放當(dāng)前頁(yè)面 */ func dismissLogin(){ self.dismissViewControllerAnimated(true, completion: nil) } func searchBarCancelButtonClicked(searchBar: UISearchBar) { print("b"); //2.3將狀態(tài)欄變?yōu)榘咨?/span> UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent; } //搜索框開始編輯事件 func searchBarTextDidBeginEditing(searchBar: UISearchBar) { //2.3將狀態(tài)欄變?yōu)榘咨?/span> UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default; } func searchBarTextDidEndEditing(searchBar: UISearchBar) { //2.3將狀態(tài)欄變?yōu)榘咨?/span> UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent; }}

//3.搜索結(jié)果頁(yè)面  searchResultTable.swift

////  searchResultTable2.swift//  searchBarDemo////  Created by 盧洋 on 15/11/6.//  Copyright © 2015年 奈文摩爾. All rights reserved.//import Foundationimport UIKitclass searchResultTable:UITableViewController,UISearchResultsUpdating{    var tableData:UITableView!;    var originalData:[String]!                  //原數(shù)據(jù)    var filteredData:[String]!                  //過濾數(shù)據(jù)        override func viewDidLoad() {        super.viewDidLoad();        self.title="輸入商家名稱";        initView()    }    init(data:[String]){        originalData = data        super.init(nibName: nil, bundle: nil)            }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }        //初始化UI    func initView(){        //創(chuàng)建Table        tableData=UITableView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height));        self.view.addSubview(tableData);        //tableData.backgroundColor=UIColor.redColor();        tableData.delegate=self;        tableData.dataSource=self;        tableData.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cells")            }         override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        // #warning Incomplete implementation, return the number of rows        return filteredData.count    }     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        let cell = tableView.dequeueReusableCellWithIdentifier("cells", forIndexPath: indexPath)                cell.textLabel!.text = originalData[indexPath.row];                return cell    }    func updateSearchResultsForSearchController(searchController: UISearchController) {        let searchBar=searchController.searchBar;        let target=searchBar.text;        filteredData = originalData.filter()            {                                s in                var options = NSStringCompareOptions.CaseInsensitiveSearch                if searchController.searchBar.selectedScopeButtonIndex == 0                {                    options = options.union(.AnchoredSearch)                }                let found = s.rangeOfString(target!, options: options)                                return (found != nil)        }        tableData.reloadData()            }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning();    }}

 

好最后效果圖如下:但是其中的字符串過濾有一點(diǎn)點(diǎn)問題,解決了一定要告訴我。。

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东兰县| 开原市| 新宾| 汕头市| 都昌县| 北川| 阿尔山市| 磐石市| 丹东市| 聂拉木县| 天镇县| 库车县| 阜宁县| 阳东县| 咸宁市| 嘉鱼县| 阿拉善左旗| 金堂县| 固安县| 开平市| 微博| 沧源| 怀宁县| 和林格尔县| 长武县| 清丰县| 裕民县| 扶余县| 和林格尔县| 云龙县| 五家渠市| 綦江县| 象州县| 武平县| 浦江县| 德惠市| 汪清县| 吕梁市| 新巴尔虎左旗| 化州市| 阳春市|