iOS开放小技巧
来源:互联网 发布:淘宝管理团队 编辑:程序博客网 时间:2024/06/05 06:14
1.kvc 简单使用
/// 获取数据 /// /// - Parameter dic: <#dic description#> init(dic: [String : AnyObject]){ super.init(); /* created_at = dic["created_at"] as! String?; id = dic["id"] as! Int; text = dic["text"] as! String?; source = dic["source"] as! String?; pic_urls = dic["pic_urls"] as! [[String : AnyObject]]?; */ //kvc 方法 必须要 super.init(); setValuesForKeys(dic) } /// setValuesForKeys内部会调用以下方法 /// /// - Parameters: /// - value: <#value description#> /// - key: <#key description#> override func setValue(_ value: Any?, forKey key: String) { if key == "user" { let _ = User(dic: (value as! [String : AnyObject])); return; } super.setValue(value, forKey: key); } /// 用 kvc 时,如果没有数据,会报错,从写方法 /// /// - Parameters: /// - value: <#value description#> /// - key: <#key description#> override func setValue(_ value: Any?, forUndefinedKey key: String) { }
2.tableView的小技巧
// 注册一个cell tableView.register(Home_TableViewCell.self, forCellReuseIdentifier: HomeReuseIdentifier); //默认 uiviewCell 的 高度是200 tableView.estimatedRowHeight = 200 //uiviewCell 高度可以动态 tableView.rowHeight = UITableViewAutomaticDimension; //不要 uiviewcell 的分割线 tableView.separatorStyle = UITableViewCellSeparatorStyle.none;
3线程锁的用法
// 1.创建一个组 let group = dispatch_group_create() // 1.缓存图片 for status in list UIView.animate(withDuration: 0.2) { //动画效果 //transform 形变后的 frame self.imageView_ArrowItem.transform = self.imageView_ArrowItem.transform.rotated(by: CGFloat(M_PI)); }{ for url in status.storedPicURLS! { // 将当前的下载操作添加到组中(与上面的线程同步) dispatch_group_enter(group) // 缓存图片 SDWebImageManager.sharedManager().downloadImageWithURL(url, options: SDWebImageOptions(rawValue: 0), progress: nil, completed: { (_, _, _, _, _) -> Void in // 离开当前组 dispatch_group_leave(group)// print("OK") }) }
3.1线程锁的用法swift3.0
//创建一个组(线程组) let group = DispatchGroup(); for status in list { if status.storedPicURLS == nil{ continue; } for url in status.storedPicURLS! { // 将当前的下载操作添加到组中 group.enter() // 缓存图片 SDWebImageManager.shared().downloadImage(with: url as URL!, options: SDWebImageOptions(rawValue: 0), progress: nil, completed: { (_, _, _, _, _) -> Void in // 离开当前组 group.leave() print("OK") finished(list, nil); }) } }
4 自由添加 view,位置
//转发的 button 在 contentView 下面添加(其实就是添加顺序) contentView.insertSubview(forwardButton, belowSubview: contentView); //forwardLabel 在forwardButton上面添加 (其实就是添加顺序) contentView.insertSubview(forwardLabel, aboveSubview: forwardButton);
5 自UItableView 中,iOS 自带的刷新,下来刷新控件
self.refreshControl = UIRefreshControl();//停止刷新self.refreshControl?.endRefreshing();
6 形变动画(swift3.0) 旋转180度
UIView.animate(withDuration: 0.2) { //动画效果 //transform 形变后的 frame self.imageView_ArrowItem.transform = self.imageView_ArrowItem.transform.rotated(by: CGFloat(M_PI)); }
7 UITextView 判断是不是有 text
/// textView 开始编辑 /// /// - Parameter textView: <#textView description#> func textViewDidChange(_ textView: UITextView) { //是不是有字体 let hasText = txteView.hasText; promptLabel.isHidden = hasText; }
8 键盘问题
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated); //开启键盘 // 主动召唤键盘 txteView.becomeFirstResponder() } /// 要关闭 /// /// - Parameter animated: <#animated description#> override func viewWillDisappear(_ animated: Bool) { // 主动隐藏键盘 txteView.resignFirstResponder() }
9. view 与 contentView 的区别
在UITableViewCell实例上添加子视图,有两种方式:[cell addSubview:view]或[cell.contentView addSubview:view],一般情况下,两种方式没有区别。
但是在多选编辑状态,直接添加到cell上的子视图将不会移动,而添加在contentView上的子视图会随着整体右移。所以,推荐使用[cell.contentView addSubview:view]方式添加子视图。
别人的详细说明 {http://blog.csdn.net/nextstudio/article/details/32101605}
10.选择了那过 item,触发 collectionVeiw
//选择了那过 item ,触发 collectionVeiw collectionVeiw.scrollToItem(at: IndexPath(item: 0, section: item.tag), at: UICollectionViewScrollPosition.left, animated: true);
11.textView 中的一些方法
// self.textView.replace 替换 text // self.textView.selectedTextRange 选中的 text self.textView.replace(self.textView.selectedTextRange!, withText: emotion.emojiStr!);
12.引用 block 函数 [防止引用][weak self]
private lazy var emoticon : EmoticonViewController = EmoticonViewController.init { [weak self] (emotion) in //判断是不是emoji 表情 if emotion.emojiStr != nil { // self.textView.replace 替换 text // self.textView.selectedTextRange 选中的 text self!.textView.replace(self!.textView.selectedTextRange!, withText: emotion.emojiStr!); } }; deinit { print("滚"); }
0 0
- iOS开放小技巧
- 使用开放的showapi接口小技巧
- IOS小积累&小技巧
- iOS 小技巧积累
- iOS开发 小技巧
- ios小技巧
- iOS 小技巧
- ios小技巧
- iOS小技巧
- iOS 开发小技巧
- IOS NSUserDefaults 小技巧
- ios的小技巧
- ios开发小技巧
- ios开发小技巧
- iOS 开发小技巧
- IOS 开发小技巧
- IOS-小技巧
- iOS开发小技巧
- springmvc实现restful返回xml格式的字符串
- [kuangbin带你飞]专题十 匹配问题 L - Cat VS Dog (二分图最大独立集)
- Flask-Migrate用法(Target database is not up to date原因、No changes in schema detected原因)
- ios 圆环加载动画
- 线性收敛的随机优化算法之 SAG、SVRG(随机梯度下降)
- iOS开放小技巧
- Unity-GlobalStatic
- Requirejs高级应用(五):模块的唯一性鉴别
- LintCode80:中位数
- 检测android手机是否支持nfc和hce
- jsp页面展示flash
- Hyperledger Sawtooth Lake的PoET简介
- 单纯的Mongdb学习
- 向大家推荐一个好用的 内存清理及内存工具类