美文网首页
Swift4.1倒计时View

Swift4.1倒计时View

作者: Codepgq | 来源:发表于2018-08-28 20:03 被阅读107次
效果图

作者的脑洞:

1、 效果图氛围上下两个部分,这里使用绘图实现,既然是上下两张图片,所以采用ImageView作为视图,在里面添加一张图片就好了。
2、然后上面的一张图片会呈现递减的动画效果,这里采用maskLayer实现
3、还有一个label

公开属性

  • 为了增强扩展性,两个进度的颜色应该有外界指定(foregroundProgressColor,backgroundProgressColor
  • 倒计时时间应该有外界决定 (maxCountDown)
  • 还有字体以及字体颜色、时间间隔(timeFont,timeColor,timeInterval

公开方法

提供更新时间、颜色的接口

public func update(foregroundProgressColor: UIColor, >backgroundProgressColor: UIColor, maxCountDown: CGFloat) 

提供控制倒计时相关的方法

public func startCountDown()
public func pauseCountDown()
public func stopCountDown()

脑洞结束

为图片添加extension

// MARK: - UIImage extension
extension UIImage {
    class func drawRect(_ size: CGSize, color: UIColor, radius: CGFloat = 0) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
        color.setFill()
        UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: size.width, height: size.height), cornerRadius: radius).fill()
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
}

CGFloat类型添加extension

extension CGFloat {
    func getTotalTime() -> String{
        let hour: Int, minute: Int, second: Int
        minute = Int(self) / 60
        second = Int(self) % 60
        if self > 3600 {
            hour = Int(self) / 3600
            return String(format: "%02d:%02d:%02d", arguments: [hour,minute,second])
        }else{
            return String(format: "%02d:%02d", arguments: [minute,second])
        }
    }
}

代码地址

相关文章

网友评论

      本文标题:Swift4.1倒计时View

      本文链接:https://www.haomeiwen.com/subject/joyqwftx.html