Skip to content

Commit

Permalink
minor updates (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacharyKhan authored Sep 7, 2021
1 parent ad37195 commit 35d5d27
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 48 deletions.
4 changes: 2 additions & 2 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/ZKCarousel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
Expand All @@ -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)";
Expand Down
19 changes: 5 additions & 14 deletions Example/ZKCarousel/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 4 additions & 18 deletions ZKCarousel/Classes/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
16 changes: 12 additions & 4 deletions ZKCarousel/Classes/ZKCarousel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 15 additions & 5 deletions ZKCarousel/Classes/ZKCarouselCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions ZKCarousel/Classes/ZKCarouselSlide.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 35d5d27

Please sign in to comment.