-
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Active cell support #25
Comments
Thank you Hristiyan for raising this issue. I think this is a common case scenario that other developers might experience. So I'm going to do an update to add this functionality to the repository. However, there's a solution you can use for the time being before I update the pod: 1- You need to subclass the class CustomDelegateImplementation: MSPeekCollectionViewDelegateImplementation {
override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
//Call the super function to handle the peeking behavior on scroll
super.scrollViewWillEndDragging(scrollView, withVelocity: velocity, targetContentOffset: targetContentOffset)
//Get the new active index
let activeIndex = self.scrollView(scrollView, indexForItemAtContentOffset: targetContentOffset.pointee)
// TODO: Return activeIndex to the view controller
}
} 2- To return the active index to the view controller, we can create a delegate protocol for that and let the view controller conform to this protocol: protocol PeekImplementationActiveDelegate: AnyObject {
func peekImplementation(_ peekImplementation: CustomDelegateImplementation, didChangeActiveIndexTo activeIndex: Int)
} 3- Now, we create a delegate variable in the weak var delegate: PeekImplementationActiveDelegate? 4- Call the delegate function in the function we overriden in step 1: override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
//Call the super function to handle the peeking behavior on scroll
super.scrollViewWillEndDragging(scrollView, withVelocity: velocity, targetContentOffset: targetContentOffset)
//Get the new active index
let activeIndex = self.scrollView(scrollView, indexForItemAtContentOffset: targetContentOffset.pointee)
//Pass the active index to the delegate
delegate?.peekImplementation(self, didChangeActiveIndexTo: activeIndex)
} 5- Add an extension for your view controller which conforms to extension ViewController: PeekImplementationActiveDelegate {
func peekImplementation(_ peekImplementation: CustomDelegateImplementation, didChangeActiveIndexTo activeIndex: Int) {
// TODO: Save active index in view controller
print(activeIndex)
}
} 6- Create a variable for the implementation in the view controller: var implementation: CustomDelegateImplementation! 7- In your override func viewDidLoad() {
super.viewDidLoad()
implementation = CustomDelegateImplementation()
implementation.delegate = self
} Final Implementation: import UIKit
import MSPeekCollectionViewDelegateImplementation
class ViewController: UIViewController {
var implementation: CustomDelegateImplementation!
override func viewDidLoad() {
super.viewDidLoad()
implementation = CustomDelegateImplementation()
implementation.delegate = self
}
}
extension ViewController: PeekImplementationActiveDelegate {
func peekImplementation(_ peekImplementation: CustomDelegateImplementation, didChangeActiveIndexTo activeIndex: Int) {
// TODO: Save active index in view controller
print(activeIndex)
}
}
protocol PeekImplementationActiveDelegate: AnyObject {
func peekImplementation(_ peekImplementation: CustomDelegateImplementation, didChangeActiveIndexTo activeIndex: Int)
}
class CustomDelegateImplementation: MSPeekCollectionViewDelegateImplementation {
weak var delegate: PeekImplementationActiveDelegate?
override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
//Call the super function to handle the peeking behavior on scroll
super.scrollViewWillEndDragging(scrollView, withVelocity: velocity, targetContentOffset: targetContentOffset)
//Get the new active index
let activeIndex = self.scrollView(scrollView, indexForItemAtContentOffset: targetContentOffset.pointee)
//Pass the active index to the delegate
delegate?.peekImplementation(self, didChangeActiveIndexTo: activeIndex)
}
}
|
Thank you very much 💯 |
@HristiyanZahariev you're welcome! Please close this issue if it fixes your problem. Do you think you can create a pull request to add this functionality to the main class? |
I think I can do it on the weekend. If it's not too late I will create a pull request |
@HristiyanZahariev sorry for the late reply. I have updated the pod to include active cell support. You can check more information in the merged PR here. Please let me know if you have any more issues |
I want to congratulate you for this lib, it's very cool. However, I think one key feature is missing and it is an active cell support. Is there a way to easily understand (from the controller) which cell is active?
The text was updated successfully, but these errors were encountered: