-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChannelCollectionPagingViewCell.swift
87 lines (69 loc) · 3.1 KB
/
ChannelCollectionPagingViewCell.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// ChannelCollectionPagingViewCell.swift
// SmartStream
//
// Created by Marc Anderson on 3/10/16.
// Copyright © 2016 SmartStream. All rights reserved.
//
import UIKit
protocol ChannelCollectionPagingViewCellDelegate: class {
func channelCollectionPageView(sender: ChannelCollectionPagingViewCell, didPlayChannel channel: Channel)
func shouldInvalidateFeaturedChannelTimer(sender: ChannelCollectionPagingViewCell)
}
class ChannelCollectionPagingViewCell: UICollectionViewCell {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var pageControl: UIPageControl!
private var scrollViewTimer: NSTimer?
var featuredChannels: [Channel]! {
didSet {
let pageWidth = scrollView.bounds.width
let pageHeight = scrollView.bounds.height
let numberOfPages = featuredChannels.count
pageControl.numberOfPages = numberOfPages
scrollView.contentSize = CGSize(width: pageWidth * CGFloat(numberOfPages), height: pageHeight)
for index in 0..<numberOfPages {
let pageView = ChannelCollectionPageView(frame: CGRect(x: (pageWidth * CGFloat(index)), y: 0, width: pageWidth, height: pageHeight))
pageView.delegate = self
pageView.channel = featuredChannels[index]
scrollView.addSubview(pageView)
}
}
}
weak var delegate: ChannelCollectionPagingViewCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
scrollView.delegate = self
let scrollViewWidth = UIScreen.mainScreen().bounds.width
scrollView.frame = CGRect(x: 0, y: 0, width: scrollViewWidth, height: 200)
scrollView.backgroundColor = Theme.Colors.DarkBackgroundColor.color
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(rotateFeaturedChannelView), name: RotateFeaturedChannelNotificatonKey, object: nil)
}
func rotateFeaturedChannelView() {
let pageWidth = scrollView.bounds.width
let pageHeight = scrollView.bounds.height
let numberOfPages = featuredChannels.count
var index = pageControl.currentPage
if index < numberOfPages - 1 {
index += 1
} else {
index = 0
}
let scrollToRect = CGRectMake(pageWidth * CGFloat(index), 0, pageWidth, pageHeight)
scrollView.scrollRectToVisible(scrollToRect, animated: true)
pageControl.currentPage = index
}
}
extension ChannelCollectionPagingViewCell: UIScrollViewDelegate {
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
delegate?.shouldInvalidateFeaturedChannelTimer(self)
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
pageControl.currentPage = Int(scrollView.contentOffset.x / scrollView.bounds.width)
}
}
extension ChannelCollectionPagingViewCell: ChannelCollectionPageViewDelegate {
func channelCollectionPageView(sender: ChannelCollectionPageView, didPlayChannel channel: Channel) {
delegate?.channelCollectionPageView(self, didPlayChannel: channel)
}
}