本文介绍: 继续加油哦!

继续加油哦!

关于动画

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&gt;.deserializeFrom(json: json["data"].description)!
 }

关于Date

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

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

DateFormatter 获得时间(小时为例)

func hour() -&gt; 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
        }
        
    }
}

原文地址:https://blog.csdn.net/weixin_52259822/article/details/122617675

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_48750.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注