moaible
11/1/2017 - 10:30 PM

SlideView.swift

SlideView.swift

final class SlideView: UIView {
    
    // MARK: - Property
    
    private lazy var scrollView = UIScrollView()
    
    private var contentViews: [UIView] = []
    
    // MARK: - Initialize
    
    required convenience init(with views: [UIView]) {
        self.init(frame: CGRect.zero)
        self.scrollView.delegate = self
        self.scrollView.isPagingEnabled = true
        self.addSubview(self.scrollView)
        self.contentViews = views
        for contentView in views {
            self.scrollView.addSubview(contentView)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // MARK: -
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.scrollView.frame = self.bounds
        self.scrollView.contentSize = CGSize(
            width: self.scrollView.frame.width * CGFloat(self.contentViews.count),
            height: self.scrollView.frame.height)
        for (idx, contentView) in self.contentViews.enumerated() {
            contentView.frame = CGRect(
                x: self.scrollView.frame.width * CGFloat(idx),
                y: 0,
                width: self.scrollView.frame.width,
                height: self.scrollView.frame.height)
        }
    }
}

extension SlideView: UIScrollViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
    }
}