Created
June 27, 2019 12:20
-
-
Save Coder-ACJHP/7956a1a2929144a82f575b03745e24b0 to your computer and use it in GitHub Desktop.
Handle view component events inside UICollectionView Cell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ChallengeViewController.swift | |
// Challenge | |
// | |
// Created by Coder ACJHP on 27.06.2019. | |
// Copyright © 2019 Fitbest Bilgi Teknolojileri. All rights reserved. | |
// | |
import UIKit | |
extension UICollectionView { | |
var centerPoint : CGPoint { | |
get { | |
return CGPoint(x: self.center.x + self.contentOffset.x, y: 45); | |
} | |
} | |
var centerCellIndexPath: IndexPath? { | |
return self.indexPathForItem(at: centerPoint) | |
} | |
} | |
var globalCounter: Int = 0 | |
class ChallengeCell: UICollectionViewCell { | |
public var color: UIColor = .orange { | |
didSet { | |
self.backgroundColor = color | |
} | |
} | |
private lazy var actionButton: UIButton = { | |
let btn = UIButton(type: .system) | |
btn.backgroundColor = UIColor.lightGray | |
btn.layer.cornerRadius = 5 | |
btn.layer.masksToBounds = true | |
btn.setTitle("Press Me!", for: .normal) | |
btn.setTitle("Press Me!", for: .selected) | |
btn.setTitleColor(.black, for: .normal) | |
btn.setTitleColor(.black, for: .selected) | |
btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18) | |
btn.translatesAutoresizingMaskIntoConstraints = false | |
btn.addTarget(self, action: #selector(hanldePress(_:)), for: .touchUpInside) | |
return btn | |
}() | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
commonInit() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
commonInit() | |
} | |
private func commonInit() { | |
self.layer.cornerRadius = 7 | |
self.layer.masksToBounds = true | |
self.addSubview(actionButton) | |
actionButton.widthAnchor.constraint(equalToConstant: 120).isActive = true | |
actionButton.heightAnchor.constraint(equalToConstant: 45).isActive = true | |
actionButton.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true | |
actionButton.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true | |
} | |
override func prepareForReuse() { | |
super.prepareForReuse() | |
} | |
@objc public func hanldePress(_ sender: UIButton) { | |
globalCounter += 1 | |
print("ChallengeCell action button pressed with event 'touchUpInside' \nTotal press count: \(globalCounter)") | |
} | |
} | |
class ChallengeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { | |
private let challengeCellId: String = "reusableChallengeCellId" | |
// MARK: - Public elements | |
public lazy var challengeCollectionView: UICollectionView = { | |
let flowLayout = UICollectionViewFlowLayout() | |
flowLayout.scrollDirection = .horizontal | |
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout) | |
collectionView.backgroundColor = .clear | |
collectionView.register(ChallengeCell.self, forCellWithReuseIdentifier: challengeCellId) | |
collectionView.showsVerticalScrollIndicator = false | |
collectionView.showsHorizontalScrollIndicator = false | |
collectionView.translatesAutoresizingMaskIntoConstraints = false | |
return collectionView | |
}() | |
public var challengeList: [Int] = Array(0...4) | |
public var challengeColorList: [UIColor] = [ | |
.init(hexString: "#ead777"), .init(hexString: "#8d5de6"), | |
.init(hexString: "#484af3"), .init(hexString: "#e85cc3"), .orange | |
] | |
private lazy var pageDotView: UIPageControl = { | |
let control = UIPageControl(frame: .zero) | |
control.numberOfPages = challengeList.count | |
control.currentPage = 0 | |
control.pageIndicatorTintColor = UIColor(white: 1, alpha: 0.5) | |
control.currentPageIndicatorTintColor = .white | |
control.translatesAutoresizingMaskIntoConstraints = false | |
return control | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .purple | |
// Digit CollectionView | |
challengeCollectionView.delegate = self | |
challengeCollectionView.dataSource = self | |
view.addSubview(challengeCollectionView) | |
challengeCollectionView.widthAnchor.constraint(equalToConstant: view.bounds.width).isActive = true | |
challengeCollectionView.heightAnchor.constraint(equalToConstant: 190).isActive = true | |
challengeCollectionView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true | |
challengeCollectionView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true | |
view.addSubview(pageDotView) | |
pageDotView.topAnchor.constraint(equalTo: challengeCollectionView.bottomAnchor, constant: 5).isActive = true | |
pageDotView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true | |
} | |
//MARK: - Collection view delegate & dataSource | |
func numberOfSections(in collectionView: UICollectionView) -> Int { | |
return 1 | |
} | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return challengeList.count | |
} | |
func collectionView(_ collectionView: UICollectionView, | |
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
guard let challengeCell = collectionView.dequeueReusableCell( withReuseIdentifier: challengeCellId, | |
for: indexPath) as? ChallengeCell else | |
{ return UICollectionViewCell() } | |
challengeCell.color = challengeColorList[indexPath.item] | |
return challengeCell | |
} | |
func collectionView(_ collectionView: UICollectionView, | |
didSelectItemAt indexPath: IndexPath) { | |
guard let selectedCell = collectionView.cellForItem(at: indexPath) as? DigitCell else { return } | |
print(selectedCell.range) | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { | |
return 20 | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { | |
return 20 | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
return .init(width: view.bounds.width - 50, height: 170) | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { | |
return .init(top: 0, left: 30, bottom: 0, right: 30) | |
} | |
func scrollViewDidScroll(_ scrollView: UIScrollView) { | |
guard let centerIndexPath = challengeCollectionView.centerCellIndexPath else { return } | |
pageDotView.currentPage = centerIndexPath.item | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment