swift倒计时动画



import Foundation

class UploadView: UIView {
    
    var selfView :UIView? = nil
    
    @IBOutlet weak var verTip: UILabel!
    
    var uploadDic : Dictionary<String,Any>? = nil
    
    
    @IBOutlet weak var whiteView: UIView!
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        selfView = Bundle.main.loadNibNamed("UploadView", owner: self, options: nil)?.last as! UIView
        self.addSubview(selfView!)
        addConstraints(contentView: selfView!)
        selfView!.layer.backgroundColor = RGBA(r: 0.0, g: 0.0, b: 0.0, a: 0.38).cgColor
        
        selfView?.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(self.tap)))
        
        verTip.layer.borderWidth = 1
        verTip.layer.borderColor = UIColor.lightGray.cgColor
    }
    
    @objc func tap() -> Void {
        zoomView(view: whiteView, big: false, remove: true) { () -> (Void) in
            self.removeFromSuperview()
        }
    }
    
    func addConstraints(contentView : UIView) {
        contentView.translatesAutoresizingMaskIntoConstraints = false
        var constraint = NSLayoutConstraint(item: contentView, attribute: .leading,
                                            relatedBy: .equal, toItem: self, attribute: .leading,
                                            multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: contentView, attribute: .trailing,
                                        relatedBy: .equal, toItem: self, attribute: .trailing,
                                        multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal,
                                        toItem: self, attribute: .top, multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: contentView, attribute: .bottom,
                                        relatedBy: .equal, toItem: self, attribute: .bottom,
                                        multiplier: 1, constant: 0)
        addConstraint(constraint)
    }
    
    
    func config(dic : Dictionary<String,Any>) -> Void {
        uploadDic = dic
        
        verTip.text = "v" + (dic["ver"] as? String ?? "null")
        
        if ((dic["ver"] as? NSNumber ?? NSNumber.init(value: 0.0)).floatValue > AppNowVersion) {
            self.isHidden = false
            
            zoomView(view: whiteView, big: true, remove: false, removeBlock: nil)
        }else{
            self.removeFromSuperview()
        }
        
        
        
    }
    
    @IBAction func clickUploadNow(_ sender: Any) {
        //itms-apps://itunes.apple.com/app/id1071810806
        let upUrl : String = uploadDic!["down"] as? String ?? ""
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(URL.init(string: upUrl)!, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(URL.init(string: upUrl)!)
        }
        
    }
    
    
    
    
    
}








class AdView: UIView {
    var selfView :UIView? = nil
    
    
    @IBOutlet weak var coverImg: UIImageView!
    
    
    
    @IBOutlet weak var skipBtn: UIButton!
    
    
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    var  scrollImgView : SDCycleScrollView? = nil
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        selfView = (Bundle.main.loadNibNamed("AdView", owner: self, options: nil)?.last as! UIView)
        self.addSubview(selfView!)
        addConstraints(contentView: selfView!)
        
        scrollImgView = SDCycleScrollView.init(frame:CGRect.init(x: 0.0, y: 0.0, width: frame.size.width, height: frame.size.height), delegate: self, placeholderImage: nil)
        coverImg.addSubview(scrollImgView!)
        
        let constX:NSLayoutConstraint = NSLayoutConstraint(item: scrollImgView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: coverImg, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0);
        
        let constY:NSLayoutConstraint = NSLayoutConstraint(item: scrollImgView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: coverImg, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0);
        
        let constW:NSLayoutConstraint = NSLayoutConstraint(item: scrollImgView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: coverImg, attribute: NSLayoutAttribute.width, multiplier: 1, constant: 0);
        
        let constH:NSLayoutConstraint = NSLayoutConstraint(item: scrollImgView as Any, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: coverImg, attribute: NSLayoutAttribute.height, multiplier: 1, constant: 0);
        scrollImgView?.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([constX,constY,constW,constH])
        
        scrollImgView?.layer.cornerRadius = 5.0
        scrollImgView?.layer.masksToBounds = true
        
        scrollImgView?.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated
        scrollImgView?.autoScrollTimeInterval = ScrollDelayTime;
        scrollImgView!.scrollDirection = UICollectionViewScrollDirection(rawValue: 1)!;
        //zyp
        scrollImgView?.backgroundColor = .white;
    }
    
    func addConstraints(contentView : UIView) {
        contentView.translatesAutoresizingMaskIntoConstraints = false
        var constraint = NSLayoutConstraint(item: contentView, attribute: .leading,
                                            relatedBy: .equal, toItem: self, attribute: .leading,
                                            multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: contentView, attribute: .trailing,
                                        relatedBy: .equal, toItem: self, attribute: .trailing,
                                        multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal,
                                        toItem: self, attribute: .top, multiplier: 1, constant: 0)
        addConstraint(constraint)
        constraint = NSLayoutConstraint(item: contentView, attribute: .bottom,
                                        relatedBy: .equal, toItem: self, attribute: .bottom,
                                        multiplier: 1, constant: 0)
        addConstraint(constraint)
    }
    
    var adInfoList : [Dictionary<String,Any>]? = nil
    func config(data : [Dictionary<String,Any>]) {
        
        adInfoList = data
        
        var imgPaths : [String] = [""]
        imgPaths.removeAll()
        for dic in adInfoList! {
            if ((dic["picture"]  as? String) != nil && NSString.init(string: dic["picture"]  as! String).contains("http") ){
                imgPaths.append(dic["picture"]  as! String)
            }
        }
        scrollImgView!.imageURLStringsGroup = [imgPaths]
    }
    
    func config(dic : Dictionary<String,Any>) -> Void {
        let coverUrl : String = dic["ad"] as? String ?? ""
        if (coverUrl.contains("http")) {
            coverImg.sd_setImage(with: URL.init(string: coverUrl), completed: nil)
            aniConfig()
        }else{
            self.removeFromSuperview()
        }
        
    }
    
    func aniConfig() -> Void {
        var edgePath = UIBezierPath.init()
        edgePath.addArc(withCenter:CGPoint.init(x: self.skipBtn.frame.size.width/2.0, y: self.skipBtn.frame.size.width/2.0),  radius: self.skipBtn.frame.size.width/2.0, startAngle: 0.0, endAngle: CGFloat(Double.pi*2.0), clockwise: true)
        var edgeLayer = CAShapeLayer.init()
        edgeLayer.path = edgePath.cgPath
        edgeLayer.fillColor = RGBA(r: 0.0, g: 0.0, b: 0.0, a: 0.38).cgColor
        edgeLayer.strokeColor = UIColor.white.cgColor
        edgeLayer.lineWidth = 2
        edgeLayer.frame = CGRect.init(x: 0, y: 0, width: skipBtn.frame.size.width, height: skipBtn.frame.size.height)
        skipBtn.layer.addSublayer(edgeLayer)
        
        var pathAnimation = CABasicAnimation.init(keyPath: "strokeEnd")
        pathAnimation.duration = 3.0
        pathAnimation.timingFunction = CAMediaTimingFunction.init(name:kCAMediaTimingFunctionEaseInEaseOut )
        pathAnimation.fromValue = 0.0
        pathAnimation.toValue = 1.0;
        pathAnimation.autoreverses = false;
        pathAnimation.isRemovedOnCompletion = true;
        pathAnimation.delegate = self
        edgeLayer.add(pathAnimation, forKey: "strokeEndAnimation")
        
    }
    
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        
        self.removeFromSuperview()
        
    }
    
    
    @IBAction func clickSkip(_ sender: Any) {
        skipBtn.layer.removeAllAnimations()
        self.removeFromSuperview()
    }
    
    
    
    
    
    
}









 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值