Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7bc09a4947 | |||
| e2e0a59efc | |||
| cf744094b0 | |||
| 8f7b1d1b1d |
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>2.2</string>
|
||||
<string>2.3</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
|
||||
@@ -16,6 +16,13 @@ class CodeCell: ScalingCarouselCell {
|
||||
|
||||
mainView = UIView(frame: contentView.bounds)
|
||||
contentView.addSubview(mainView)
|
||||
mainView.translatesAutoresizingMaskIntoConstraints = false
|
||||
NSLayoutConstraint.activate([
|
||||
mainView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
|
||||
mainView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
|
||||
mainView.topAnchor.constraint(equalTo: contentView.topAnchor),
|
||||
mainView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
|
||||
])
|
||||
}
|
||||
|
||||
required init?(coder aDecoder: NSCoder) {
|
||||
@@ -40,6 +47,13 @@ class CodeViewController: UIViewController {
|
||||
super.didReceiveMemoryWarning()
|
||||
// Dispose of any resources that can be recreated.
|
||||
}
|
||||
|
||||
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
|
||||
super.viewWillTransition(to: size, with: coordinator)
|
||||
if scalingCarousel != nil {
|
||||
scalingCarousel.deviceRotated()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Configuration
|
||||
|
||||
@@ -76,7 +90,11 @@ extension CodeViewController: UICollectionViewDataSource {
|
||||
if let scalingCell = cell as? ScalingCarouselCell {
|
||||
scalingCell.mainView.backgroundColor = .blue
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
cell.setNeedsLayout()
|
||||
cell.layoutIfNeeded()
|
||||
}
|
||||
|
||||
return cell
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>2.2</string>
|
||||
<string>2.3</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>$(CURRENT_PROJECT_VERSION)</string>
|
||||
<key>NSPrincipalClass</key>
|
||||
@@ -25,6 +25,8 @@
|
||||
<key>UISupportedInterfaceOrientations</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@@ -30,7 +30,11 @@ class StoryboardViewController: UIViewController {
|
||||
|
||||
carouselBottomConstraint.constant = Constants.carouselHideConstant
|
||||
}
|
||||
|
||||
|
||||
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
|
||||
super.viewWillTransition(to: size, with: coordinator)
|
||||
carousel.deviceRotated()
|
||||
}
|
||||
// MARK: - Button Actions
|
||||
|
||||
@IBAction func showHideButtonPressed(_ sender: Any) {
|
||||
@@ -56,6 +60,11 @@ extension CarouselDatasource: UICollectionViewDataSource {
|
||||
if let scalingCell = cell as? ScalingCarouselCell {
|
||||
scalingCell.mainView.backgroundColor = .red
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
cell.setNeedsLayout()
|
||||
cell.layoutIfNeeded()
|
||||
}
|
||||
|
||||
return cell
|
||||
}
|
||||
|
||||
@@ -38,6 +38,12 @@ This property is declared in ScalingCarouselCell. You should add any cell conten
|
||||
cell.setNeedsLayout()
|
||||
cell.layoutIfNeeded()
|
||||
```
|
||||
* Note: To ensure correct displayed of the ScalingCarousel, you need to call the following code in the method `viewWillTransition(to size:, with coordinator:)` of the ViewController:
|
||||
|
||||
```
|
||||
super.viewWillTransition(to: size, with: coordinator)
|
||||
scalingCarousel.deviceRotated()
|
||||
```
|
||||
|
||||
### Code
|
||||
|
||||
@@ -50,6 +56,13 @@ override init(frame: CGRect) {
|
||||
// Initialize the mainView property and add it to the cell's contentView
|
||||
mainView = UIView(frame: contentView.bounds)
|
||||
contentView.addSubview(mainView)
|
||||
mainView.translatesAutoresizingMaskIntoConstraints = false
|
||||
NSLayoutConstraint.activate([
|
||||
mainView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
|
||||
mainView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
|
||||
mainView.topAnchor.constraint(equalTo: contentView.topAnchor),
|
||||
mainView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
|
||||
])
|
||||
}
|
||||
```
|
||||
|
||||
@@ -82,6 +95,15 @@ scalingCarousel.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isAc
|
||||
```
|
||||
cell.setNeedsLayout()
|
||||
cell.layoutIfNeeded()
|
||||
```
|
||||
* Note: To ensure correct displayed of the ScalingCarousel, you need to call the following code in the method `viewWillTransition(to size:, with coordinator:)` of the ViewController, If you have created the ScalingCarousel by code in the viewDidLoad, It is important to verify that it exists when the method `viewWillTransition` is called or we will have a crash if we load the viewController with the device in landscape mode:
|
||||
|
||||
```
|
||||
super.viewWillTransition(to: size, with: coordinator)
|
||||
if scalingCarousel != nil {
|
||||
scalingCarousel.deviceRotated()
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'ScalingCarousel'
|
||||
s.version = '2.2'
|
||||
s.version = '2.3'
|
||||
s.summary = 'A super simple carousel view with scaling transitions written in Swift'
|
||||
|
||||
s.description = <<-DESC
|
||||
|
||||
@@ -16,7 +16,11 @@ import UIKit
|
||||
are scaled as the carousel scrolls.
|
||||
*/
|
||||
open class ScalingCarouselView: UICollectionView {
|
||||
|
||||
|
||||
// MARK: - Properties (Private)
|
||||
|
||||
private var lastCurrentCenterCellIndex: IndexPath?
|
||||
|
||||
// MARK: - Properties (Public)
|
||||
|
||||
/// Inset of the main, center cell
|
||||
@@ -118,6 +122,7 @@ open class ScalingCarouselView: UICollectionView {
|
||||
let originX = (CGFloat(indexPath.item) * (frame.size.width - (inset * 2)))
|
||||
let rect = CGRect(x: originX, y: 0, width: frame.size.width - (inset * 2), height: frame.height)
|
||||
scrollRectToVisible(rect, animated: animated)
|
||||
lastCurrentCenterCellIndex = indexPath
|
||||
}
|
||||
|
||||
override open func didMoveToSuperview() {
|
||||
@@ -141,6 +146,26 @@ open class ScalingCarouselView: UICollectionView {
|
||||
public func didScroll() {
|
||||
scrollViewDidScroll(self)
|
||||
}
|
||||
|
||||
/*
|
||||
This method should ALWAYS be called from the ViewController that handles the ScalingCarousel when
|
||||
the viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) method is called
|
||||
|
||||
e.g Implement:
|
||||
|
||||
func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
|
||||
super.viewWillTransition(to: size, with: coordinator)
|
||||
carousel.deviceRotated()
|
||||
}
|
||||
*/
|
||||
public func deviceRotated() {
|
||||
guard let lastCurrentCenterCellIndex = currentCenterCellIndex ?? lastCurrentCenterCellIndex else { return }
|
||||
DispatchQueue.main.async {
|
||||
self.reloadData()
|
||||
self.scrollToItem(at: lastCurrentCenterCellIndex, at: .centeredHorizontally, animated: false)
|
||||
self.didScroll()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private typealias PrivateAPI = ScalingCarouselView
|
||||
@@ -245,6 +270,8 @@ extension InvisibleScrollDelegate: UIScrollViewDelegate {
|
||||
|
||||
public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
|
||||
delegate?.scrollViewDidEndDecelerating?(scrollView)
|
||||
guard let indexPath = currentCenterCellIndex else { return }
|
||||
lastCurrentCenterCellIndex = indexPath
|
||||
}
|
||||
|
||||
private func updateOffSet() {
|
||||
|
||||
Reference in New Issue
Block a user