showcase

Horizontal Scrolling - Stack of cards - using Swift 2

by
published on
So looks like cards like layout and horizontal scrolling is really getting some tractions and many apps have started using it. Offcourse there are several ways to do it but i found a nifty easy trick to build this using UICollectionViewController with least amount of code. Here is how it looks like.
gify
  Fancy right ;) But no clue about why i came up with "Swappy Dagger"   Anyways the whole magic is in this secret sauce:
    func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        
        let pageWidth:Float = 310 + 25;
        
        let currentOffSet:Float = Float(scrollView.contentOffset.x)
        
        print(currentOffSet)
        let targetOffSet:Float = Float(targetContentOffset.memory.x)
        
        print(targetOffSet)
        var newTargetOffset:Float = 0
        
        if(targetOffSet > currentOffSet){
            newTargetOffset = ceilf(currentOffSet / pageWidth) * pageWidth
        }else{
            newTargetOffset = floorf(currentOffSet / pageWidth) * pageWidth
        }
        
        if(newTargetOffset < 0){
            newTargetOffset = 0;
        }else if (newTargetOffset > Float(scrollView.contentSize.width)){
            newTargetOffset = Float(scrollView.contentSize.width)
        }
        
        targetContentOffset.memory.x = CGFloat(currentOffSet)
        scrollView.setContentOffset(CGPointMake(CGFloat(newTargetOffset), 0), animated: true)
        
    }
Without that you won't get the feel. Anyways to get the code you can download it from my Git Repo.  If this is useful spread the love by sharing. Cheers!