Comments (12)
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.
huum thanks, one more question, oh i get click on menu cell?
from parchment.
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.
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.
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.
thanks for support :)
from parchment.
No problem, happy to help!
from parchment.
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.
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.
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.
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.
u welcome :)
from parchment.
Related Issues (20)
- Swipe in cell does not work HOT 2
- RTL support HOT 1
- The menu items label is not showing HOT 1
- PagingIndicatorView does not fill the PagingTitleCell HOT 1
- PageView does not work in ScrollView HOT 2
- Focus position on left side HOT 1
- Issue on Index HOT 1
- Can i make the indicator fit the text length?
- There is an issue that accidentally gets stuck and cannot keep clicking other tab item HOT 2
- How To Lazy Load Page HOT 1
- How can I hide the menu? (SwiftUI)
- SwiftUI Scrolling glitch HOT 2
- Feature request: SF Icons instead of title text HOT 1
- My app crash when tap to page item. HOT 4
- The package product 'Parchment' cannot be used as a dependency of this target because it uses unsafe build flags. HOT 3
- Lazy loading VC HOT 1
- keyboard avoidance issue HOT 1
- how to make collectionview rounded and add shadow to collectionview HOT 1
- Improve notification list pull to refresh
- outdated code error in PagingSizeCache HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parchment.