diff --git a/Example/Pods/Pods.xcodeproj/project.pbxproj b/Example/Pods/Pods.xcodeproj/project.pbxproj index 2b47775..a9505a4 100644 --- a/Example/Pods/Pods.xcodeproj/project.pbxproj +++ b/Example/Pods/Pods.xcodeproj/project.pbxproj @@ -719,7 +719,7 @@ INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - MARKETING_VERSION = 1.1.0; + MARKETING_VERSION = 1.1.1; MODULEMAP_FILE = "Target Support Files/ZKCarousel/ZKCarousel.modulemap"; PRODUCT_MODULE_NAME = ZKCarousel; PRODUCT_NAME = ZKCarousel; @@ -753,7 +753,7 @@ INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - MARKETING_VERSION = 1.1.0; + MARKETING_VERSION = 1.1.1; MODULEMAP_FILE = "Target Support Files/ZKCarousel/ZKCarousel.modulemap"; PRODUCT_MODULE_NAME = ZKCarousel; PRODUCT_NAME = ZKCarousel; diff --git a/Example/ZKCarousel.xcodeproj/project.pbxproj b/Example/ZKCarousel.xcodeproj/project.pbxproj index e84158a..78bf9aa 100644 --- a/Example/ZKCarousel.xcodeproj/project.pbxproj +++ b/Example/ZKCarousel.xcodeproj/project.pbxproj @@ -376,7 +376,7 @@ INFOPLIST_FILE = ZKCarousel/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - MARKETING_VERSION = 1.1.0; + MARKETING_VERSION = 1.1.1; MODULE_NAME = ExampleApp; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -396,7 +396,7 @@ INFOPLIST_FILE = ZKCarousel/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - MARKETING_VERSION = 1.1.0; + MARKETING_VERSION = 1.1.1; MODULE_NAME = ExampleApp; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/Example/ZKCarousel/ViewController.swift b/Example/ZKCarousel/ViewController.swift index 042bcf4..720ddca 100644 --- a/Example/ZKCarousel/ViewController.swift +++ b/Example/ZKCarousel/ViewController.swift @@ -12,7 +12,7 @@ import ZKCarousel class ViewController: UIViewController { // Instantiated and used with Storyboards - @IBOutlet var carousel: ZKCarousel! = ZKCarousel() + @IBOutlet var carousel: ZKCarousel? = ZKCarousel() override func viewDidLoad() { super.viewDidLoad() @@ -31,25 +31,16 @@ class ViewController: UIViewController { let slide2 = ZKCarouselSlide(image: #imageLiteral(resourceName: "demo2"), title: "Another Demo Slide", description: "lorem ipsum devornum cora fusoa foen ebakldf shjbesd ljkhf") - let slide3 = ZKCarouselSlide(image: #imageLiteral(resourceName: "demo2"), - title: "Hello There", - description: "Welcome to the ZKCarousel demo! Swipe left to view more slides!") - let slide4 = ZKCarouselSlide(image: #imageLiteral(resourceName: "demo"), - title: "A Demo Slide", - description: "lorem ipsum devornum cora fusoa foen sdie ha odab ebakldf shjbesd ljkhf") - let slide5 = ZKCarouselSlide(image: #imageLiteral(resourceName: "demo2"), - title: "Another Demo Slide", - description: "lorem ipsum devornum cora fusoa foen ebakldf shjbesd ljkhf") - + // Add the slides to the carousel - self.carousel.slides = [slide, slide1, slide2, slide3, slide4, slide5] + self.carousel?.slides = [slide, slide1, slide2] // You can optionally use the 'interval' property to set the timing for automatic slide changes. The default is 1 second. - self.carousel.interval = 1.5 + self.carousel?.interval = 3 // OPTIONAL - use this function to automatically start traversing slides. - self.carousel.start() + self.carousel?.start() // OPTIONAL - use this function to stop automatically traversing slides. // self.carousel.stop() diff --git a/README.md b/README.md index 690a7ba..1c06943 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,6 @@ There is an option to let ZKCarousel switch between slides automatically. All yo See example for a working implementation of this. #### Custom Gesture Recognizers - If you would like to add a custom action upon tapping the carousel (as opposed to the default action, which changes to the next slide) you will need to first call `disableTap()` to remove the default gesture recognizer, then add your own gesture recognizer to the UICollectionView. ## Contributions diff --git a/ZKCarousel/Classes/Extensions.swift b/ZKCarousel/Classes/Extensions.swift index 158e78a..b6cff09 100644 --- a/ZKCarousel/Classes/Extensions.swift +++ b/ZKCarousel/Classes/Extensions.swift @@ -5,29 +5,15 @@ // Created by Zachary Khan on 8/22/20. // -extension UIView { - - func addConstraintsWithFormat(_ format: String, views: UIView...) { - - var viewsDictionary = [String: UIView]() - for (index, view) in views.enumerated() { - let key = "v\(index)" - viewsDictionary[key] = view - view.translatesAutoresizingMaskIntoConstraints = false - } - - addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: viewsDictionary)) - } - - -} +import UIKit extension UIImageView { func addBlackGradientLayer(frame: CGRect){ let gradient = CAGradientLayer() gradient.frame = frame - gradient.colors = [UIColor.clear.cgColor, UIColor.black.withAlphaComponent(0.8).cgColor] - gradient.locations = [0.0, 0.6] + gradient.colors = [UIColor.clear.cgColor, UIColor.black.withAlphaComponent(0.6).cgColor] + gradient.locations = [0.0, 0.5] + layer.insertSublayer(gradient, at: 0) } } diff --git a/ZKCarousel/Classes/ZKCarousel.swift b/ZKCarousel/Classes/ZKCarousel.swift index 7f551b6..eba41c9 100644 --- a/ZKCarousel/Classes/ZKCarousel.swift +++ b/ZKCarousel/Classes/ZKCarousel.swift @@ -12,7 +12,10 @@ import UIKit func carouselDidScroll() } -final public class ZKCarousel: UIView, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource { +final public class ZKCarousel: UIView, + UICollectionViewDelegateFlowLayout, + UICollectionViewDelegate, + UICollectionViewDataSource { // MARK: - Properties private var timer: Timer = Timer() @@ -56,7 +59,7 @@ final public class ZKCarousel: UIView, UICollectionViewDelegateFlowLayout, UICol cv.delegate = self cv.dataSource = self cv.isPagingEnabled = true - cv.register(ZKCarouselCell.self, forCellWithReuseIdentifier: "slideCell") + cv.register(ZKCarouselCell.self, forCellWithReuseIdentifier: ZKCarouselCell.identifier) cv.clipsToBounds = true cv.backgroundColor = .clear cv.showsHorizontalScrollIndicator = false @@ -104,7 +107,9 @@ final public class ZKCarousel: UIView, UICollectionViewDelegateFlowLayout, UICol let index = visibleIndexPath.item let indexPathToShow = IndexPath(item: index == slides.count - 1 ? 0 : index + 1, section: 0) - collectionView.selectItem(at: indexPathToShow, animated: true, scrollPosition: .centeredHorizontally) + collectionView.selectItem(at: indexPathToShow, + animated: true, + scrollPosition: .centeredHorizontally) } private func updateUI() { @@ -139,7 +144,10 @@ final public class ZKCarousel: UIView, UICollectionViewDelegateFlowLayout, UICol // MARK: - UICollectionViewDelegate & DataSource public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { - let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ZKCarouselCell.identifier, for: indexPath) as! ZKCarouselCell + guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ZKCarouselCell.identifier, + for: indexPath) as? ZKCarouselCell else { + return ZKCarouselCell() + } cell.slide = slides[indexPath.item] return cell } diff --git a/ZKCarousel/Classes/ZKCarouselCell.swift b/ZKCarousel/Classes/ZKCarouselCell.swift index dfa40c6..167b579 100644 --- a/ZKCarousel/Classes/ZKCarouselCell.swift +++ b/ZKCarousel/Classes/ZKCarouselCell.swift @@ -5,17 +5,20 @@ // Created by Zachary Khan on 8/22/20. // -public class ZKCarouselCell: UICollectionViewCell { +import UIKit - static let identifier = "slideCell" +public class ZKCarouselCell: UICollectionViewCell { // MARK: - Properties + static let identifier = "carousel_slide_default_Cell" + public var slide : ZKCarouselSlide? { didSet { guard let slide = slide else { - print("ZKCarousel could not parse the slide you provided.") + print("ZKCarousel is unable to parse the ZKCarouselSlide that was provided.") return } + parseData(forSlide: slide) } } @@ -33,7 +36,7 @@ public class ZKCarouselCell: UICollectionViewCell { private var titleLabel : UILabel = { let label = UILabel() label.adjustsFontSizeToFitWidth = true - label.font = UIFont.boldSystemFont(ofSize: 40) + label.font = UIFont.boldSystemFont(ofSize: 20) label.textColor = .white label.textAlignment = .center label.translatesAutoresizingMaskIntoConstraints = false @@ -42,7 +45,7 @@ public class ZKCarouselCell: UICollectionViewCell { private var descriptionLabel : UILabel = { let label = UILabel() - label.font = UIFont.systemFont(ofSize: 19) + label.font = UIFont.systemFont(ofSize: 14) label.textColor = .white label.textAlignment = .center label.numberOfLines = 0 @@ -61,6 +64,13 @@ public class ZKCarouselCell: UICollectionViewCell { fatalError("init(coder:) has not been implemented") } + public override func prepareForReuse() { + super.prepareForReuse() + imageView.image = nil + titleLabel.text = nil + descriptionLabel.text = nil + } + // MARK: - Actions private func setup() { backgroundColor = .clear diff --git a/ZKCarousel/Classes/ZKCarouselSlide.swift b/ZKCarousel/Classes/ZKCarouselSlide.swift index b27b163..ad622c3 100644 --- a/ZKCarousel/Classes/ZKCarouselSlide.swift +++ b/ZKCarousel/Classes/ZKCarouselSlide.swift @@ -5,12 +5,17 @@ // Created by Zachary Khan on 8/22/20. // +import UIKit + public struct ZKCarouselSlide { - public var image : UIImage? + public var image : UIImage public var title : String? public var description: String? - public init(image: UIImage?, title: String?, description: String?) { + public init(image: UIImage, + title: String? = nil, + description: String? = nil) { + self.image = image self.title = title self.description = description