4 Commits

Author SHA1 Message Date
Pete Smith 7bc09a4947 Bump version 2018-05-28 10:37:34 +04:00
Pete Smith e2e0a59efc Merge pull request #39 from pabsanmez/fixOnDeviceRotation
Fix ScalingCarousel on Device Rotation
2018-05-28 10:34:08 +04:00
pabsanmez cf744094b0 Added feature to example 2018-05-27 21:35:02 +02:00
pabsanmez 8f7b1d1b1d Fix Device Rotated and autolayout mainView in code version 2018-05-27 21:20:35 +02:00
7 changed files with 84 additions and 6 deletions
@@ -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
}
}
+3 -1
View File
@@ -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
}
+22
View File
@@ -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
+1 -1
View File
@@ -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() {