Giter VIP home page Giter VIP logo

Comments (12)

rechsteiner avatar rechsteiner commented on May 24, 2024

There is no built in option for defining this, but you can achieve it pretty easily by subclassing PagingCell and setting the background color based on the selected state. Just create your own subclass and override setPagingItem:

class CustomPagingCell: PagingTitleCell {
  
  override func setPagingItem(_ pagingItem: PagingItem, selected: Bool, theme: PagingTheme) {
    super.setPagingItem(pagingItem, selected: selected, theme: theme)
    
    if selected {
      contentView.backgroundColor = .red
    } else {
      contentView.backgroundColor = .white
    }
}

Then set the menuItemClass property to CustomPagingCell.self:

struct Options: PagingOptions {
  let menuItemClass: PagingCell.Type = CustomPagingCell.self
}

And pass that into the PagingViewController:

FixedPagingViewController(viewControllers: viewControllers, options: Options())

This will set the background color when the selected state changes, without any animation. If you want to tween between the colors based on how much the user has scrolled, you can override apply:layoutAttributes instead of setPagingItem:

class CustomPagingCell: PagingTitleCell {
 
  open override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
    super.apply(layoutAttributes)
    if let attributes = layoutAttributes as? PagingCellLayoutAttributes {
      contentView.backgroundColor = UIColor.interpolate(
        from: .white,
        to: .red,
        with: attributes.progress)
    }
  }
  
}

This will interpolate between the selected background color based on the scroll progress. I'm using UIColor.interpolate here which you can find the source code for inUIColor+interpolation.swift.

from parchment.

miguelalvescosta avatar miguelalvescosta commented on May 24, 2024

huum thanks, one more question, oh i get click on menu cell?

from parchment.

rechsteiner avatar rechsteiner commented on May 24, 2024

If you are using FixedPagingViewController you can use the FixedPagingViewControllerDelegate to get notified when an item was scrolled to:

public protocol FixedPagingViewControllerDelegate : class {
  func fixedPagingViewController(fixedPagingViewController: FixedPagingViewController, willScrollToItem: ViewControllerItem, atIndex index: Int)
  func fixedPagingViewController(fixedPagingViewController: FixedPagingViewController, didScrollToItem: ViewControllerItem, atIndex index: Int)
}

Or if you need the actual select event you create your own subclass and override didSelectItem:

class MyPagingViewController: FixedPagingViewController {
  override public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    super.collectionView(collectionView, didSelectItemAt: indexPath)
    print("did select")
  }
}

from parchment.

miguelalvescosta avatar miguelalvescosta commented on May 24, 2024

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let firstViewController = storyboard.instantiateViewController(withIdentifier: "FirstViewController")
let secondViewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController")


let pagingViewController = FixedPagingViewController(viewControllers: [
  firstViewController,
  secondViewController
])



addChildViewController(pagingViewController)
view.addSubview(pagingViewController.view)
view.constrainToEdges(pagingViewController.view)
pagingViewController.didMove(toParentViewController: self)

}
}

sorry for my poor knowledge, but in a class like this how I create mine subclass and override didselectitem?

from parchment.

rechsteiner avatar rechsteiner commented on May 24, 2024

You need to create a new class outside ViewController – like the MyPagingViewController snippet above – and replace this line:

let pagingViewController = FixedPagingViewController(viewControllers: [

with this:

let pagingViewController = MyPagingViewController(viewControllers: [

from parchment.

miguelalvescosta avatar miguelalvescosta commented on May 24, 2024

thanks for support :)

from parchment.

rechsteiner avatar rechsteiner commented on May 24, 2024

No problem, happy to help!

from parchment.

miguelalvescosta avatar miguelalvescosta commented on May 24, 2024

hi, its me again..... can u explain me "If you are using FixedPagingViewController you can use the FixedPagingViewControllerDelegate to get notified when an item was scrolled to", i dnt know who to use delegate :/..

from parchment.

rechsteiner avatar rechsteiner commented on May 24, 2024

No worries, the documentation for this isn't very good. You need to conform to the FixedPagingViewControllerDelegate protocol in you ViewController class. Here an example:

extension ViewController: FixedPagingViewControllerDelegate {

  func fixedPagingViewController(fixedPagingViewController: FixedPagingViewController, willScrollToItem: ViewControllerItem, atIndex index: Int) {
    print("will scroll to item")
  }

  func fixedPagingViewController(fixedPagingViewController: FixedPagingViewController, didScrollToItem: ViewControllerItem, atIndex index: Int) {
     print("did scroll to item")
  }

}

Then set the itemDelegate property in viewDidLoad:

pagingViewController.itemDelegate = self

from parchment.

miguelalvescosta avatar miguelalvescosta commented on May 24, 2024

hi, i think i found a bug, in fixedPageViewController u have :
open override func em_pageViewController(_ pageViewController: EMPageViewController, didFinishScrollingFrom startingViewController: UIViewController?, destinationViewController: UIViewController, transitionSuccessful: Bool) {
super.em_pageViewController(pageViewController, didFinishScrollingFrom: startingViewController, destinationViewController: destinationViewController, transitionSuccessful: transitionSuccessful)

    if let index = items.index(where: { $0.viewController == destinationViewController }) {
        itemDelegate?.fixedPagingViewController(
            fixedPagingViewController: self,
            didScrollToItem: items[index],
            atIndex: index)
    }  
}

i think u need check if transition is successful because if u do a "fake swipe" and dnt swipe to next menu tab and stay on same menu tab and didscroolToItem is activated.... in my opinion the code looks like this:

open override func em_pageViewController(_ pageViewController: EMPageViewController, didFinishScrollingFrom startingViewController: UIViewController?, destinationViewController: UIViewController, transitionSuccessful: Bool) {
super.em_pageViewController(pageViewController, didFinishScrollingFrom: startingViewController, destinationViewController: destinationViewController, transitionSuccessful: transitionSuccessful)
if transitionSuccessful == true {

    if let index = items.index(where: { $0.viewController == destinationViewController }) {
        itemDelegate?.fixedPagingViewController(
            fixedPagingViewController: self,
            didScrollToItem: items[index],
            atIndex: index)
        }

     }
  }

from parchment.

rechsteiner avatar rechsteiner commented on May 24, 2024

Yeah, that's definitely a bug. I also found another bug were the willScrollItemItem was not called at all. Fixed in #63 – will release a new version soon. Thanks for catching that 🙌

from parchment.

miguelalvescosta avatar miguelalvescosta commented on May 24, 2024

u welcome :)

from parchment.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.