• Stars
    star
    103
  • Rank 333,046 (Top 7 %)
  • Language
    Swift
  • Created almost 8 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Custom CollectionViewLayout with a custom paging size by overriding scrollViewWillEndDragging

#Custom UICollectionView

Create custom UICollectionViewLayoutWith with a custom paging size of UICollectionViewCell by overriding scrollViewWillEndDragging

alt text

let collectionMargin = CGFloat(16)
let itemSpacing = CGFloat(10)
let itemHeight = CGFloat(322)
var itemWidth = CGFloat(0)
var currentItem = 0

1 Custom CollectionViewLayout

override func viewDidLoad() {
    super.viewDidLoad()
    
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()

    itemWidth =  UIScreen.main.bounds.width - collectionMargin * 2.0
    
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    layout.itemSize = CGSize(width: itemWidth, height: itemHeight)
    layout.headerReferenceSize = CGSize(width: collectionMargin, height: 0)
    layout.footerReferenceSize = CGSize(width: collectionMargin, height: 0)
    layout.minimumLineSpacing = itemSpacing
    layout.scrollDirection = .horizontal

    collectionView!.collectionViewLayout = layout
    collectionView?.decelerationRate = UIScrollViewDecelerationRateFast

}

2 Custom Paging size

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    
    let pageWidth = Float(itemWidth + itemSpacing)
    let targetXContentOffset = Float(targetContentOffset.pointee.x)
    let contentWidth = Float(collectionView!.contentSize.width  )
    var newPage = Float(self.pageControl.currentPage)
    
    if velocity.x == 0 {
        newPage = floor( (targetXContentOffset - Float(pageWidth) / 2) / Float(pageWidth)) + 1.0
    } else {
        newPage = Float(velocity.x > 0 ? self.pageControl.currentPage + 1 : self.pageControl.currentPage - 1)
        if newPage < 0 {
            newPage = 0
        }
        if (newPage > contentWidth / pageWidth) {
            newPage = ceil(contentWidth / pageWidth) - 1.0
        }
    }
    self.pageControl.currentPage = Int(newPage)
    let point = CGPoint (x: CGFloat(newPage * pageWidth), y: targetContentOffset.pointee.y)
    targetContentOffset.pointee = point
}

More Repositories

1

UIView-UpdateAutoLayoutConstraints

An easy way to create and update AutoLayout Constraints (Mainly to update Width and Height of UIView)
Objective-C
102
star
2

VisibleFormViewController

An extended UIviewController allowing to not hide the content behind the keyboard opened.
Objective-C
40
star
3

UIButton-setBackgroundColor-forState-

an Objective-c Category to add the method setBackgroundColor:forState: to the UIButton
Objective-C
18
star
4

NSString-Matcher

An Objective-c category providing a easy way to match NSString with a Regex
Objective-C
8
star
5

NSObject-FromClassName

In swift, get quickly an NSObject from its classname
Swift
6
star
6

express-sync

How to use an express server with browser-sync (without gulp)
JavaScript
5
star
7

CustomTabBarController

To understand how build a view container of navigationcontroller, the best way is to recreate the UITabBarController.
Objective-C
3
star
8

jquery-vertical-slider

Jquery Mobile don't allow to display slider vertically. I need it to working on a project, then I continued the work of @elmundio87
JavaScript
2
star
9

RFDuino-adafruit-lSM9DS0

I'm Working on a RFDUINO and a Adafruit LSM9DS0 (Accelerometer + Gyro + Magnetometer). RFDUINO send Accelerometer's data to an IOS app by Bluetooth
Objective-C
2
star
10

damienromito

Portfolio
JavaScript
1
star
11

WDMessage

Display notifications in the app with several options and simple customisations
Objective-C
1
star
12

UIView-AutoYPositioning

A simple way to stack dynamic UIView
Objective-C
1
star
13

SwiftInObjectiveC

A very simple demo of a working practice to use Swift into an Objective-c project
Objective-C
1
star
14

DRLabel

A Subclass of UILabel allowing to have padding in my Label
Objective-C
1
star