12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import Foundation
- import UIKit
- class XFBanner: UIView, UIScrollViewDelegate {
-
- var viewArr: NSArray? {
- didSet {
-
- setupView()
- }
- }
- lazy var pageControl: UIPageControl = {
- let pageV = UIPageControl.init()
- pageV.currentPage = 0
- pageV.currentPageIndicatorTintColor = UIColor.red
- pageV.pageIndicatorTintColor = UIColor.lightGray
- return pageV
- }()
- func setupView() {
- print(viewArr!)
- for i in 0 ..< viewArr!.count {
- let view: CommonDataView = viewArr![i] as! CommonDataView
- scrollView.addSubview(view)
- view.frame = CGRect(x: self.frame.width * CGFloat(i), y: 0, width: self.frame.width, height: self.frame.height)
- }
- scrollView.contentSize = CGSize(width: self.frame.width * CGFloat(viewArr!.count), height: self.frame.height)
- let w = self.frame.width
- let h = self.frame.height
- pageControl.frame = CGRect.init(x: (w - 80)/2, y: h - 40, width: 80, height: 40)
- self.addSubview(pageControl)
- pageControl.numberOfPages = viewArr!.count
- }
- lazy var scrollView: UIScrollView = {
- let scrol = UIScrollView.init()
- scrol.frame = self.bounds
- scrol.delegate = self
- scrol.isPagingEnabled = true
- scrol.bounces = false
- scrol.showsVerticalScrollIndicator = false
- scrol.showsHorizontalScrollIndicator = false
- return scrol
- }()
-
- func scrollViewDidScroll(_ scrollView: UIScrollView) {
- self.pageControl.currentPage = Int(scrollView.contentOffset.x / self.frame.width)
- }
- override init(frame: CGRect) {
- super.init(frame: frame)
- self.addSubview(scrollView)
- }
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- }
|