继续加油哦!
关于动画
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)
关于报错(在解析JSON数据时)
Thread 1: fatal error: Nil was found unexpectedly while expanding optional values
// 文件路径
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了
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
func year() -> Int {
let calendar = NSCalendar.current
let com = calendar.dateComponents([.year, .month, .day], from: self)
return com.year!
}
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中时
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])
}
}
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
}()
let title = ["头条", "视频", "娱乐", "要问", "体育", "科技", "图片", "游戏", "房产"]
设置childViewController(这里是添加了addChild)
for i in 0..<title.count {
let controller = ContentViewController()
controller.view.backgroundColor = UIColor.randomColor
controller.index = i
addChild(controller)
}
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进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。