FlowlayoutDemo
protocol TagViewDelegate {
func tagViewTabs(_ selectedTabIndex:Int)
func tagViewItem(_ selectedItem:SCause)
}
class TagView: UIView {
var delegate:TagViewDelegate!
@IBOutlet weak var exploreBtn: UIButton!
@IBOutlet weak var nextBtn: UIButton!
// @IBOutlet weak var footerFavBadge: UILabel!
@IBOutlet weak var favBtn: MIBadgeButton!
private let config = TKConfigurationManager.sharedInstance
@IBOutlet weak var collectionView:UICollectionView!
fileprivate let kCollectionCellReuseId = "tagcell"
var TAGS = [SCause]()
var niggleInfo = NiggleInfo()
var subissueselectiondone = false
var subissueid = 0
var selectedSubTopicsArr = [Int]()
override func awakeFromNib() {
let collectionNib = UINib(nibName: "TagCollectionCell", bundle: nil)
self.collectionView.register(collectionNib, forCellWithReuseIdentifier: kCollectionCellReuseId)
self.exploreBtn.tintColor = config.mainThemeColor
let origImage = UIImage(named: "star_toolscount")
let tintedImage = origImage?.withRenderingMode(.alwaysTemplate)
self.favBtn.setImage(tintedImage, for: .normal)
self.favBtn.tintColor = config.mainThemeColor
self.nextBtn.tintColor = config.mainThemeColor
self.favBtn.badgeEdgeInsets = UIEdgeInsetsMake(25, -69, 0, 15)
self.favBtn.badgeTextColor = UIColor.white
self.favBtn.badgeBackgroundColor = config.mainThemeColor
// Set the PinterestLayout delegate
if let layout = collectionView?.collectionViewLayout as? NitHorizantalFlowLayout {
layout.delegate = self
// layout.scrollDirection = .horizontal
}
}
@IBAction func didTapBottom(_ sender:UIButton){
sender.isSelected = !sender.isSelected
switch sender.tag {
case 10:
//Scale the height to close to zero
self.exploreBtn.tintColor = config.mainThemeColor
if sender.isSelected {
self.exploreBtn.tintColor = UIColor.white
}
self.favBtn.tintColor = config.mainThemeColor
self.nextBtn.tintColor = config.mainThemeColor
self.delegate.tagViewTabs(10)
DispatchQueue.main.async {
self.collectionView.setNeedsLayout()
self.collectionView.collectionViewLayout.invalidateLayout()
}
for const in self.constraints{
if const.identifier == "height"{
UIView.animate(withDuration: 0.4, animations: {
const.constant = sender.isSelected ? 180 : 50
self.layer.cornerRadius = 0
if #available(iOS 11.0, *) {
self.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
} else {
// Fallback on earlier versions
}
if const.constant == CGFloat(180) {
self.layer.cornerRadius = 10
if #available(iOS 11.0, *) {
self.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
} else {
// Fallback on earlier versions
}
}
self.superview?.layoutIfNeeded()
self.backgroundColor = sender.isSelected ? self.config.mainThemeColor : HELPER.hexStringToUIColor(hex: "FBFBFA")
})
}
}
break
case 11:
// self.exploreBtn.tintColor = config.mainThemeColor
// self.favBtn.tintColor = config.mainThemeColor
//self.nextBtn.tintColor = UIColor.white
self.delegate.tagViewTabs(11)
break
case 12:
// self.exploreBtn.tintColor = config.mainThemeColor
// self.favBtn.tintColor = UIColor.white
// self.nextBtn.tintColor = config.mainThemeColor
self.delegate.tagViewTabs(12)
break
default:
break
}
}
}
extension TagView:UICollectionViewDelegate, UICollectionViewDataSource, NitLayoutDelegate{
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return TAGS.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let tagcell = collectionView.dequeueReusableCell(withReuseIdentifier: kCollectionCellReuseId, for: indexPath) as! TagCollectionCell
if let tagInfo = tagcell.viewWithTag(10) as? UIMarginLabel{
let subIssue = TAGS[indexPath.item]
tagInfo.text = subIssue.label
tagInfo.textColor = UIColor.white
tagInfo.leftInset = 6
tagInfo.rightInset = 6
let filterArr = selectedSubTopicsArr.filter({$0 == subIssue.id})
if filterArr.count > 0 {
tagInfo.backgroundColor = config.auxThemeColor
}
else {
tagInfo.backgroundColor = UIColor(red: 200.0/255.0, green: 200.0/255.0, blue: 200.0/255.0, alpha:0.2)
subissueselectiondone = true
}
}
// else {
// let selectedissues = niggleInfo.issues.count
// if selectedissues == 1 && subissueselectiondone == false {
// if let myid = niggleInfo.issues.first?.khlSubIssueId {
// subissueid = myid
// }
// }
// else if selectedissues == 2 {
// if let myid = niggleInfo.issues.first?.khlSubIssueId {
// if subissueselectiondone == false {
// subissueid = myid
// }
// else {
// subissueid = niggleInfo.issues[1].khlSubIssueId
// }
// }
// }
// else if selectedissues == 3 {
// if let myid = niggleInfo.issues.first?.khlSubIssueId {
// if subissueselectiondone == false {
// subissueid = myid
// }
// else {
// if subissueid == niggleInfo.issues[1].khlSubIssueId {
// subissueid = niggleInfo.issues[2].khlSubIssueId
// }
// else {
// subissueid = niggleInfo.issues[1].khlSubIssueId
// }
//
// }
// }
// }
// if let tagInfo = tagcell.viewWithTag(10) as? UIMarginLabel{
// let subIssue = TAGS[indexPath.item]
// tagInfo.text = subIssue.label
// tagInfo.textColor = UIColor.white
// tagInfo.leftInset = 6
// tagInfo.rightInset = 6
// if subissueid == subIssue.id {
// tagInfo.backgroundColor = config.auxThemeColor
// }
// else {
// tagInfo.backgroundColor = UIColor(red: 200.0/255.0, green: 200.0/255.0, blue: 200.0/255.0, alpha:0.2)
// subissueselectiondone = true
// }
// }
// }
// tagcell.backgroundColor = config.auxThemeColor
return tagcell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(TAGS[indexPath.item])
let tagcell = collectionView.cellForItem(at: indexPath)
if let tagInfo = tagcell?.viewWithTag(10) as? UILabel{
if tagInfo.backgroundColor == config.auxThemeColor {
tagInfo.backgroundColor = UIColor(red: 200.0/255.0, green: 200.0/255.0, blue: 200.0/255.0, alpha:0.2)
}
else {
tagInfo.backgroundColor = config.auxThemeColor
}
}
delegate.tagViewItem(TAGS[indexPath.item])
}
func collectionView(_ collectionView: UICollectionView, sizeForNitItem indexPath: IndexPath) -> CGSize {
let text = TAGS[indexPath.item].label
return textSize(with: text!).size
}
func textSize(with tagName:String) -> CGRect {
let myText = tagName as NSString
let rect = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
let labelSize = myText.boundingRect(with: rect, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 18.0)], context: nil)
return labelSize
}
}
extension CALayer {
func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
let border = CALayer()
switch edge {
case .top:
border.frame = CGRect(x: 0, y: 0, width: frame.width, height: thickness)
case .bottom:
border.frame = CGRect(x: 0, y: frame.height - thickness, width: frame.width, height: thickness)
case .left:
border.frame = CGRect(x: 0, y: 0, width: thickness, height: frame.height)
case .right:
border.frame = CGRect(x: frame.width - thickness, y: 0, width: thickness, height: frame.height)
default:
break
}
border.backgroundColor = color.cgColor;
addSublayer(border)
}
}
Comments
Post a Comment