Skip to content

Commit

Permalink
Added scrolling to a specific item
Browse files Browse the repository at this point in the history
  • Loading branch information
MaherKSantina committed Dec 13, 2019
1 parent 62ef41f commit 7d8f16a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="HLk-Dk-Q2u">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="HLk-Dk-Q2u">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15510"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -22,7 +20,7 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="LPX-Xy-ffX">
<rect key="frame" x="0.0" y="88" width="375" height="200"/>
<rect key="frame" x="0.0" y="68" width="375" height="200"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="200" id="mPI-O6-6Yu"/>
Expand All @@ -45,15 +43,15 @@
</cells>
</collectionView>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="SqK-AS-fCp">
<rect key="frame" x="0.0" y="312" width="375" height="355"/>
<rect key="frame" x="0.0" y="292" width="375" height="375"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="PhW-LY-2UV">
<rect key="frame" x="0.0" y="0.0" width="375" height="46"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="SOE-YZ-ngU">
<rect key="frame" x="16" y="8" width="343" height="30"/>
<subviews>
<slider opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" minValue="0.0" maxValue="100" translatesAutoresizingMaskIntoConstraints="NO" id="HGB-Vl-6y1" customClass="CustomSlider" customModule="MSPeekCollectionViewDelegateImplementation_Example" customModuleProvider="target">
<slider opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" value="20" minValue="0.0" maxValue="100" translatesAutoresizingMaskIntoConstraints="NO" id="HGB-Vl-6y1" customClass="CustomSlider" customModule="MSPeekCollectionViewDelegateImplementation_Example" customModuleProvider="target">
<rect key="frame" x="191" y="0.0" width="154" height="31"/>
<constraints>
<constraint firstAttribute="width" constant="150" id="VgY-Sb-nMy"/>
Expand Down Expand Up @@ -95,7 +93,7 @@
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="2oh-g2-WZ8">
<rect key="frame" x="16" y="8" width="343" height="30"/>
<subviews>
<slider opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" minValue="0.0" maxValue="100" translatesAutoresizingMaskIntoConstraints="NO" id="jow-9B-5z4" customClass="CustomSlider" customModule="MSPeekCollectionViewDelegateImplementation_Example" customModuleProvider="target">
<slider opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" value="20" minValue="0.0" maxValue="100" translatesAutoresizingMaskIntoConstraints="NO" id="jow-9B-5z4" customClass="CustomSlider" customModule="MSPeekCollectionViewDelegateImplementation_Example" customModuleProvider="target">
<rect key="frame" x="191" y="0.0" width="154" height="31"/>
<constraints>
<constraint firstAttribute="width" constant="150" id="N6Q-qa-K5J"/>
Expand Down Expand Up @@ -315,7 +313,7 @@
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="HLk-Dk-Q2u" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="oae-6G-97C">
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- MSPeekCollectionViewDelegateImplementation (3.0.0)
- MSPeekCollectionViewDelegateImplementation (3.0.1)

DEPENDENCIES:
- MSPeekCollectionViewDelegateImplementation (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
MSPeekCollectionViewDelegateImplementation: e1c53a3d70dc37850d529f9fc9b07c29767fccf7
MSPeekCollectionViewDelegateImplementation: fdb8ae5c41baaad0962f489b7fa78a653037cc17

PODFILE CHECKSUM: 8d230cee08c8a8b7e62859eff8016277efe2311d

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion MSPeekCollectionViewDelegateImplementation.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'MSPeekCollectionViewDelegateImplementation'
s.version = '3.0.0'
s.version = '3.0.1'
s.summary = 'A custom paging behavior that peeks the previous and next items in a collection view'
s.swift_version = '5.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public class MSCollectionViewPeekingBehavior {
paging.dataSource = self
}

/// Scrolls to an item at a specific index with or without animation
public func scrollToItem(at index: Int, animated: Bool) {
layout.collectionView?.setContentOffset(layout.startingPointForItem(index: index), animated: animated)
}

/// Required function to be called when the `scrollViewWillEndDragging` `UICollectionViewDelegate` function is called
public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
paging.collectionViewWillEndDragging(scrollDirection: scrollDirection, withVelocity: velocity, targetContentOffset: targetContentOffset)
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ extension ViewController: UICollectionViewDelegate {
}
```

## Features
### Scrolling to a specific item
The `MSCollectionViewPeekingBehavior` now has a function to scroll to a specific index
```swift
public func scrollToItem(at index: Int, animated: Bool)
```

You can do something like:
```swift
behavior.scrollToItem(at: 1, animated: true)
```

## Customization
### Vertical Scroll Direction
The implementation supports collection views with vertical directions and will automatically position cells correctly, you can set the scrolling and peeking to be vertical using:
Expand Down

0 comments on commit 7d8f16a

Please sign in to comment.