Skip to content
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

Closed
HristiyanZahariev opened this issue Sep 25, 2018 · 5 comments
Closed

Active cell support #25

HristiyanZahariev opened this issue Sep 25, 2018 · 5 comments

Comments

@HristiyanZahariev
Copy link

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?

@MaherKSantina
Copy link
Owner

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 MSPeekCollectionViewDelegateImplementation to monitor when the user scrolls to a new cell (I assume you need to update the index in the controller every time the user scrolls a cell). You can do that as follows:

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 CustomDelegateImplementation:

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 PeekImplementationActiveDelegate:

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 viewDidLoad function, create a new instance for the implementation and set the delegate to self

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)
    }
}

@HristiyanZahariev
Copy link
Author

HristiyanZahariev commented Sep 25, 2018

Thank you very much 💯

@MaherKSantina
Copy link
Owner

@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?

@HristiyanZahariev
Copy link
Author

I think I can do it on the weekend. If it's not too late I will create a pull request

@MaherKSantina
Copy link
Owner

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants