其他分享
首页 > 其他分享> > 我在树食中学到的

我在树食中学到的

作者:互联网

继续加油哦!

关于动画

CGAffineTransform:UIView有个属性transform,是CGAffineTransform类型。可以使其在二维界面做旋转、平移、缩放单独或者组合动画

平移

UIView.animate(withDuration: 2.0) {
            self.showview.transform = CGAffineTransform(translationX: 100, y: 100)
        }

缩放

UIView.animate(withDuration: 2.0) {
            self.showview.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
        }

旋转

UIView.animate(withDuration: 2.0) {
            self.showview.transform = CGAffineTransform(rotationAngle: 4 * M_PI_4)
        

rotationAngle后面的参数是旋转的角度

关于报错(在解析JSON数据时)

Thread 1: fatal error: Nil was found unexpectedly while expanding optional values

线程1:致命错误:在展开可选值时意外发现nil

// 文件路径
let path = Bundle.main.path(forResource: "homeList", ofType: "json")
 // json转NSData
let jsonData = NSData(contentsOfFile: path!)
// 解析json
let json = JSON(jsonData!)
homeData = JSONDeserializer<HomeData>.deserializeFrom(json: json["data"].description)!

此时path! jsonData! ()! 后面都报了此错误,原因是path等是可选类型可能为nil了

那么修改的方法就是不让path为nil

if let path = Bundle.main.path(forResource: "homeList", ofType: "json"){
   //json转NSData
    let jsonData = NSData(contentsOfFile: path)
     //解析json
     let json = JSON(jsonData!)
     homeData = JSONDeserializer<HomeData>.deserializeFrom(json: json["data"].description)!
 }

关于Date

NSCalendar 获得年份(月份与日子同理)

func year() -> Int {
        let calendar = NSCalendar.current
        let com = calendar.dateComponents([.year, .month, .day], from: self)
        return com.year!
    }

DateFormatter 获得时间(小时为例)

func hour() -> Int {
        let formatter = DateFormatter()
        formatter.dateFormat = "HH:mm:ss.SSS"
        let time = formatter.string(from: self) as String
        let hour = time.prefix(2)
        return Int(hour) ?? 25
    }

今天星期几

func WeekDay() -> Int {
        let interval = Int(timeIntervalSince1970)//1970年以来的时间间隔
        let days = Int(interval / 86400) //24*60*60
        let weekday = ((days + 4) % 7 + 7) % 7
        return weekday == 0 ? 7 : weekday
    }

还有一种方法

func getWeekDay(dateTime: String) -> String {
        let dateFmt = DateFormatter()
        dateFmt.dateFormat = "yyyy-MM-dd"
        let date = dateFmt.date(from: dateTime)
        let interval = Int(date!.timeIntervalSince1970)
        let days = Int(interval / 86400)
        let weekday = ((days + 5) % 7 + 7) % 7
        let weekDays = ["周一", "周二", "周三", "周四", "周五", "周六"]
        return weekDays[weekday]
    }

当月天数

func countOfDaysInMonth() -> Int {
        let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
        let range = (calendar as NSCalendar?)?.range(of: NSCalendar.Unit.day, in: NSCalendar.Unit.month, for: self)
        return (range?.length)!
    }

当月第一天是星期几

func firstWeekDay() -> Int {
        //1.Sun 2.Mon...
        let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
        let firstWeekDay = (calendar as NSCalendar?)?.ordinality(of: NSCalendar.Unit.weekday, in: NSCalendar.Unit.weekOfMonth, for: self)
        return firstWeekDay! - 1
    }

闭包回调

在一个CollectionView1中还有一个CollectionView2,当1要将解析后的数据传值到2中时

1、在2上声明一个闭包

public var cellCallBack: ((Dish, Species) -> Void)?

2、在2的collectionView中didSelectItemAt函数中进行调用(可以不在此函数中,但必须有个东西调用此闭包,例如button中的addTarget)

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if  let callback = cellCallBack {
            callback(data[indexPath.row], FoodType[indexPath.row])
        }
    }

3、在1中cellForItemAt函数中定义此闭包

cell.cellCallBack = { data, type in
                let vc = DishDetailViewController()
                vc.updateUI(with: data, types: type)
                self.navigationController?.pushViewController(vc, animated: true)
            }

这样当点击CollectionView2上的cell时会执行闭包

关于PageView

先设置PageStyle

lazy var style: PageStyle = {
        let style = PageStyle()
        style.isTitleScaleEnabled = true//是否启用了标题视图滚动
        style.titleMargin = 20//标题边缘
        style.titleInset = 20//标题插图
        style.titleSelectedFont = UIFont.systemFont(ofSize: 10)
        style.isTitleScaleEnabled = true//是否启用了标题比例
        return style
    }()

再设置title

let title = ["头条", "视频", "娱乐", "要问", "体育", "科技", "图片", "游戏", "房产"]

设置childViewController(这里是添加了addChild)

for i in 0..<title.count {
            let controller = ContentViewController()
            controller.view.backgroundColor = UIColor.randomColor
            controller.index = i
            addChild(controller)
        }

​ 最后设置PageView

let y = UIApplication.shared.statusBarFrame.height + (navigationController?.navigationBar.frame.height ?? 0)
let size = UIScreen.main.bounds.size
        
let pageView = PageView(frame: CGRect(x: 0, y: y, width: size.width, height: size.height - y), style: style, titles: title, childViewControllers: children, currentIndex: 1)

关于 UISearchController

lazy var searchController: UISearchController = {
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self//搜索结果更新程序
  			//总是显示搜索框,如果不设置,会随着滚动而消失
				navigationItem.hidesSearchBarWhenScrolling = false
        controller.delegate = self
        controller.searchBar.delegate = self
        return controller
    }()
UISearchBarDelegate
extension SearchViewController: UISearchBarDelegate {
    func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
        return true;
    }
    
    func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
        return true;
    }
}
UISearchControllerDelegate, UISearchResultsUpdating
extension SearchViewController: UISearchControllerDelegate, UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {
        let inputStr = searchController.searchBar.text
        if resultData.count > 0 {
            resultData.removeAll()
        }
        for item in data {
            if((item.name.lowercased() as NSString).range(of: inputStr?.lowercased() ?? "").location != NSNotFound) {
                resultData.append(item)
            }
        }
        self.tableView.reloadData()
    }

    func didPresentSearchController(_ searchController: UISearchController) {
        
        if out {
            searchController.searchBar.becomeFirstResponder()
            self.out = false
        }
        
    }
}

标签:style,中学,return,self,树食,json,let,func
来源: https://blog.csdn.net/weixin_52259822/article/details/122617675