策略
广告显示策略=
1)只有1张时,显示3秒;
2)多余1张时,每张显示1秒,最多三张;
3)首次加载不显示广告,并缓存广告(链接),第二次开始显示广告并缓存图片;
4)缓存过期时间7天
5)预留点击响应接口
这么晚了,还是直接上代码吧
class BannerView: UIView {
private var scrollView: UIScrollView?
public var currentPageIndex: Int = 0
fileprivate var animationTimer: Timer?
fileprivate var pageControl : UIPageControl?
//block
public var contentViewAtIndex : ((_ pageIndex: Int)->UIImageView)?
public var tapActionBlock: ((_ pageIndex: Int)-> Void)?
public var endActionBlock: (() -> Void)?
//private
private var contentViews : [UIImageView] = []
fileprivate var animationInterval : TimeInterval?
private var totalPages : Int?
private var jumpButton = UIButton(type: .custom);
deinit {
scrollView?.delegate = nil
if self.animationTimer != nil {
self.animationTimer?.invalidate();
self.animationTimer = nil;
}
}
public init(frame: CGRect ,_ duration: TimeInterval,iamgeUrls: [URL]) {
super.init(frame: frame)
if duration > 0 {
animationTimer = Timer.scheduledTimer(timeInterval: duration, target: self, selector: #selector(animationTimerDidFire(timer:)), userInfo: nil, repeats: true)
}
animationInterval = duration
self.clipsToBounds = true
scrollView = UIScrollView(frame: self.bounds)
scrollView?.scrollsToTop = true
scrollView?.isPagingEnabled = true
scrollView?.delegate = self
scrollView?.contentOffset = CGPoint(x: 0, y: 0)
scrollView?.contentSize = CGSize(width: CGFloat(iamgeUrls.count) * ScreenWidth, height: self.bounds.size.height)
self.addSubview(scrollView!)
pageControl = UIPageControl(frame: CGRect(x: 0, y: self.bounds.size.height - 30, width: ScreenWidth, height: 10))
pageControl?.isUserInteractionEnabled = false
self.addSubview(pageControl!)
for index in 0..<iamgeUrls.count {
let imageView = UIImageView()
imageView.setImage(with: iamgeUrls[index] )
imageView.frame = CGRect(x: 0 + CGFloat(index) * ScreenWidth , y: 0, width: ScreenWidth, height: self.bounds.size.height)
imageView.isUserInteractionEnabled = true;
imageView.tag = 200 + index;
let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(contentViewTapped(sender:)))
imageView.addGestureRecognizer(tapGesture);
scrollView?.addSubview(imageView);
}
jumpButton.frame = CGRect(x: ScreenWidth - 70.0, y: 34, width: 60, height: 30);
jumpButton.backgroundColor = UIColor.black;
jumpButton.alpha = 0.4;
jumpButton.setTitle(String(format: "跳过(%d)", iamgeUrls.count), for: .normal)
jumpButton.setTitleColor(UIColor.white, for: .normal)
jumpButton.titleLabel?.font = UIFont.systemFont(ofSize: 13);
jumpButton.addTarget(self, action: #selector(jumpButttonAction(sender:)), for: .touchUpInside)
self.addSubview(jumpButton);
}
//MARK:设置总页数后,启动动画
public func setTotalPagesCount(totalPageCout: (()->(Int))) {
self.totalPages = totalPageCout()
if self.totalPages! > 3 {
self.totalPages = 3;
}
// print("totalPages = \(self.totalPages)")
self.pageControl?.numberOfPages = self.totalPages!
pageControl?.backgroundColor = UIColor.clear
pageControl?.isUserInteractionEnabled = true
self.currentPageIndex = 0
if self.totalPages == 1 {
self.totalPages = 2
scrollView?.contentSize = CGSize(width: ScreenWidth, height: self.bounds.size.height)
//configureContentViews()
self.pageControl?.isHidden = true
animationTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countDown(timer:)), userInfo: nil, repeats: true)
self.animationTimer?.resume()
}else{
self.pageControl?.isHidden = false
}
if self.totalPages! > 0 && self.totalPages! != 1 {
//configureContentViews()
self.animationTimer?.resumeAfter(interval: self.animationInterval!)
}
}
func countDown(timer:Timer) {
self.totalPages = self.totalPages! - 1;
jumpButton.setTitle(String(format: "跳过(%d)", self.totalPages! ), for: .normal)
if self.totalPages == 0 {
if let endActionBlock = self.endActionBlock {
endActionBlock();
scrollView?.delegate = nil
if self.animationTimer != nil {
self.animationTimer?.invalidate();
self.animationTimer = nil;
}
}
}
}
func jumpButttonAction(sender: UIButton) {
if let endActionBlock = self.endActionBlock {
endActionBlock();
scrollView?.delegate = nil
if self.animationTimer != nil {
self.animationTimer?.invalidate();
self.animationTimer = nil;
}
}
}
//点击事件
func contentViewTapped(sender: UIGestureRecognizer){
let index: NSInteger = (sender.view?.tag)! - 200;
if self.tapActionBlock != nil {
self.tapActionBlock!(index)
}
}
@objc fileprivate func animationTimerDidFire(timer:Timer){
self.currentPageIndex = self.currentPageIndex + 1;
self.scrollView?.setContentOffset(CGPoint(x: ScreenWidth * CGFloat(currentPageIndex ), y: 0),animated: true)
self.countDown(timer: timer)
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension BannerView: UIScrollViewDelegate {
public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.animationTimer?.pause()
}
public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
self.animationTimer?.resumeAfter(interval: self.animationInterval!)
}
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.pageControl?.currentPage = self.currentPageIndex
}
public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollView.setContentOffset(CGPoint(x: ScreenWidth, y: 0), animated: true)
}
}
contrller的代码
let urls: [URL] = (imageUrls?.map { (string) -> URL in
return URL(string: string)!;
})!;
let imageView = UIImageView();
imageView.image = UIImage(named: "Image_ad_log");
//imageView.frame = CGRect(x: 0, y: ScreenHeight * 0.7, width: ScreenWidth, height: ScreenHeight * 0.3);
self.bannerScroller = BannerView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight - 170), 1,iamgeUrls: urls)
self.view.addSubview(bannerScroller!);
self.view.addSubview(imageView);
imageView.snp.makeConstraints { (make) in
make.centerX.equalTo(self.view)
make.width.equalTo(200)
make.height.equalTo(170)
make.centerY.equalTo(ScreenHeight - 85)
}
guard urls.count > 0 else {
let appdelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate;
appdelegate.custmozizeRootViewContrller()
return
}
//点击回调响应
bannerScroller?.tapActionBlock = {(index) in
print("点击==\(index)")
}
bannerScroller?.endActionBlock = { () in
let appdelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate;
appdelegate.custmozizeRootViewContrller()
}
网友评论