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
原创粉丝点击