material-components_materia.../components/PageControl/examples/PageControlTypicalUseExample.swift
featherless 23110ef116
[Catalog] Make all swift copycat demos be dragons. (#3443)
Also removed "(Swift)" from any examples that remained as non-dragons. The catalog does not need to advertise which language a given demo is in.

Pivotal story: https://www.pivotaltracker.com/story/show/156939611

## Screenshots

Before:
![simulator screen shot - iphone se - 2018-04-23 at 14 04 14](https://user-images.githubusercontent.com/45670/39144588-36095450-46ff-11e8-868d-bdb7e8ee5438.png)

After:
![simulator screen shot - iphone se - 2018-04-23 at 13 53 39](https://user-images.githubusercontent.com/45670/39144567-2ad7bf9a-46ff-11e8-9fd5-ef0739d427b3.png)
2018-04-24 15:52:13 -04:00

133 lines
4.5 KiB
Swift

/*
Copyright 2016-present the Material Components for iOS authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Foundation
import MaterialComponents.MaterialPageControl
class PageControlSwiftExampleViewController: UIViewController, UIScrollViewDelegate {
static let pageColors = [
MDCPalette.cyan.tint300,
MDCPalette.cyan.tint500,
MDCPalette.cyan.tint700,
MDCPalette.cyan.tint300,
MDCPalette.cyan.tint500,
MDCPalette.cyan.tint700
]
let pageControl = MDCPageControl()
let scrollView = UIScrollView()
let pageLabels: [UILabel] = PageControlSwiftExampleViewController.pageColors.enumerated().map {
enumeration in
let (i, pageColor) = enumeration
let pageLabel = UILabel()
pageLabel.text = "Page \(i + 1)"
pageLabel.font = pageLabel.font.withSize(50)
pageLabel.textColor = UIColor(white: 0, alpha: 0.8)
pageLabel.backgroundColor = pageColor
pageLabel.textAlignment = NSTextAlignment.center
pageLabel.autoresizingMask = [.flexibleTopMargin, .flexibleBottomMargin]
return pageLabel
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
scrollView.frame = self.view.bounds
scrollView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
scrollView.delegate = self
scrollView.isPagingEnabled = true
scrollView.contentSize = CGSize(width: view.bounds.width * CGFloat(pageLabels.count),
height: view.bounds.height)
scrollView.showsHorizontalScrollIndicator = false
view.addSubview(scrollView)
// Add pages to scrollView.
for (i, pageLabel) in pageLabels.enumerated() {
let pageFrame = view.bounds.offsetBy(dx: CGFloat(i) * view.bounds.width, dy: 0)
pageLabel.frame = pageFrame
scrollView.addSubview(pageLabel)
}
pageControl.numberOfPages = pageLabels.count
pageControl.addTarget(self, action: #selector(didChangePage), for: .valueChanged)
pageControl.autoresizingMask = [.flexibleTopMargin, .flexibleWidth]
view.addSubview(pageControl)
}
// MARK: - Frame changes
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let pageBeforeFrameChange = pageControl.currentPage
for (i, pageLabel) in pageLabels.enumerated() {
pageLabel.frame = view.bounds.offsetBy(dx: CGFloat(i) * view.bounds.width, dy: 0)
}
scrollView.contentSize = CGSize(width: view.bounds.width * CGFloat(pageLabels.count),
height: view.bounds.height)
var offset = scrollView.contentOffset
offset.x = CGFloat(pageBeforeFrameChange) * view.bounds.width
// This non-anmiated change of offset ensures we keep the same page
scrollView.contentOffset = offset
var edgeInsets = UIEdgeInsets.zero;
#if swift(>=3.2)
if #available(iOS 11, *) {
edgeInsets = self.view.safeAreaInsets
}
#endif
let pageControlSize = pageControl.sizeThatFits(view.bounds.size)
let yOffset = self.view.bounds.height - pageControlSize.height - 8 - edgeInsets.bottom;
pageControl.frame =
CGRect(x: 0, y: yOffset, width: view.bounds.width, height: pageControlSize.height)
}
// MARK: - UIScrollViewDelegate
func scrollViewDidScroll(_ scrollView: UIScrollView) {
pageControl.scrollViewDidScroll(scrollView)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
pageControl.scrollViewDidEndDecelerating(scrollView)
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
pageControl.scrollViewDidEndScrollingAnimation(scrollView)
}
// MARK: - User events
@objc func didChangePage(_ sender: MDCPageControl) {
var offset = scrollView.contentOffset
offset.x = CGFloat(sender.currentPage) * scrollView.bounds.size.width
scrollView.setContentOffset(offset, animated: true)
}
// MARK: - CatalogByConvention
@objc class func catalogBreadcrumbs() -> [String] {
return [ "Page Control", "Swift example"]
}
@objc class func catalogIsPrimaryDemo() -> Bool {
return false
}
}