iOS-swift

swift4.0仿头条下拉刷新效果

2018-07-12  本文已影响134人  千年积木

效果还差那么两丢丢
代码可以直接用

import UIKit
import MJRefresh
class TTMJTestVC: UIViewController {

    lazy var tableViewm:UITableView = {
        let tb = UITableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        return tb;
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        configureTb()
        setMJ();
    }

    func configureTb() {
        self.view.addSubview(tableViewm)
        tableViewm.delegate = self;
        tableViewm.dataSource = self;
        tableViewm.backgroundColor = UIColor.white
        tableViewm.snp.makeConstraints { (make) in
            make.edges.equalToSuperview();
        }
        
    }

    func setMJ() {
        tableViewm.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
                self.endMJrefreshAction(scrollView: self.tableViewm, viewHeigh: 30, titles: "!!我爱你!!")
            })
        })
        
    }
    
    func endMJrefreshAction(scrollView:UIScrollView,viewHeigh:Int,titles:String) {
        scrollView.mj_header.isAutomaticallyChangeAlpha = true
        print(scrollView.mj_offsetY)
        print(scrollView.mj_header.mj_h)
        let mjhead = scrollView.mj_header
        
        UIView.animate(withDuration: 0.2, animations: {
        }) { (iscompletion) in
            
            var vv = mjhead?.viewWithTag(9999)
            var lb = mjhead?.viewWithTag(9998) as? UILabel
            
            if let _ = vv {vv?.isHidden = false }else{
                vv = UIView.init(frame: CGRect.init(x: SCREEN_WIDTH/4, y: 27, width: SCREEN_WIDTH/2, height: 27))
                vv?.backgroundColor = UIColor.blue.withAlphaComponent(0.6)
                vv?.tag = 9999;
                mjhead?.addSubview(vv!)
            }
            
            if let _ = lb {lb?.isHidden = false}else{
                lb = UILabel.init(frame: CGRect.init(x: 0, y: 27, width: SCREEN_WIDTH, height: 27))
                lb?.text = titles
                lb?.textColor = UIColor.red
                lb?.textAlignment = .center
                lb?.tag = 9998
                mjhead?.addSubview(lb!)
            }
            
            UIView.animate(withDuration: 0.15, animations: {
                vv!.transform = CGAffineTransform.init(scaleX: 2, y: 1)
            }, completion: { (isfinish) in
                
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2, execute: {
                    mjhead?.endRefreshing();
                    
                    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
                        vv?.isHidden = true
                        lb?.isHidden = true
                        vv?.transform = CGAffineTransform.identity
                        
                    })
                })
                
            })
            
        }
       
        
    }
    
}

extension TTMJTestVC:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell:TTFDCell? = tableView.dequeueReusableCell(withIdentifier:String.init(describing: TTFDCell.self)) as? TTFDCell
        if let _ = cell {}else{
            cell = Bundle.main.loadNibNamed(String.init(describing: TTFDCell.self), owner: nil, options: nil)?.first as? TTFDCell
        }
        setConfigure(witf: cell, at: indexPath)
        return cell!
    }
    
    func setConfigure(witf cell:TTFDCell?,at indexPath: IndexPath){
        cell?.separatorInset.right = 16;
        cell?.setModel = TTDateTool.share.cellDataArr?[indexPath.row]
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 160;
    }
}

extension TTMJTestVC:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
    }
}

上一篇下一篇

猜你喜欢

热点阅读