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()
}
}