RefreshView.swift: 用了Then协议库

这个博客介绍了如何在RefreshView中应用Then协议库来创建一个下拉刷新视图。内容包括设置圆环形状的CAShapeLayer,使用UIActivityIndicatorView,并提供了开始刷新、结束刷新以及刷新状态管理的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import UIKit


class RefreshView: UIView {


    let circleLayer =CAShapeLayer()


    let indicatorView =UIActivityIndicatorView().then {

        $0.frame = CGRect(x:0, y: 0, width:16, height: 16)

    }

    

    fileprivatevar refreshing = false

    fileprivatevar endRef = false

    

    overrideinit(frame: CGRect) {

        super.init(frame: frame)

        creatCircleLayer()

    }

    

    overridefunc layoutSubviews() {

        super.layoutSubviews()

        circleLayer.position =CGPoint(x: frame.width/2, y: frame.height/2)

        indicatorView.center = CGPoint(x: frame.width/2, y: frame.height/2)

    }

    

    func creatCircleLayer() {

        circleLayer.path =UIBezierPath(arcCenter: CGPoint(x: 8, y:8),

                               radius: 8,

                               startAngle: CGFloat(M_PI_2),

                               endAngle: CGFloat(M_PI_2 +2*M_PI),

                               clockwise: true).cgPath

        circleLayer.strokeColor =UIColor.white.cgColor

        circleLayer.fillColor =UIColor.clear.cgColor

        circleLayer.strokeStart =0.0

        circleLayer.strokeEnd =0.0

        circleLayer.lineWidth =1.0

        circleLayer.lineCap =kCALineCapRound

        circleLayer.bounds =CGRect(x: 0, y:0, width: 16, height:16)

        circleLayer.anchorPoint =CGPoint(x: 0.5, y:0.5)

        layer.addSublayer(circleLayer)

    }

    

    requiredinit?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }


}


extension RefreshView {

    //向下拖拽视图准备刷新的过程会响应

    func pullToRefresh(progress:CGFloat) {

        circleLayer.strokeEnd = progress

    }

    //开始刷新

    func beginRefresh(begin:@escaping () -> Void) {

        ifrefreshing {

            //防止刷新未结束又开始请求刷新

            return

        }

        refreshing =true

        circleLayer.removeFromSuperlayer()

        addSubview(indicatorView)

        indicatorView.startAnimating()

        begin()

    }

    //结束刷新

    func endRefresh() {

        refreshing =false

        indicatorView.stopAnimating()

        indicatorView.removeFromSuperview()

    }

    //重制刷新控件

    func resetLayer() {

        DispatchQueue.main.asyncAfter(deadline:DispatchTime.now() +0.5) {

            self.creatCircleLayer()

        }

    }

    

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值