Skip to content

Commit 7d8f16a

Browse files
committed
Added scrolling to a specific item
1 parent 62ef41f commit 7d8f16a

File tree

8 files changed

+33
-18
lines changed

8 files changed

+33
-18
lines changed

Example/MSPeekCollectionViewDelegateImplementation/Base.lproj/Main.storyboard

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<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">
3-
<device id="retina4_7" orientation="portrait">
4-
<adaptation id="fullscreen"/>
5-
</device>
2+
<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">
3+
<device id="retina4_7" orientation="portrait" appearance="light"/>
64
<dependencies>
75
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
6+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15510"/>
97
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
108
</dependencies>
119
<scenes>
@@ -22,7 +20,7 @@
2220
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
2321
<subviews>
2422
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="LPX-Xy-ffX">
25-
<rect key="frame" x="0.0" y="88" width="375" height="200"/>
23+
<rect key="frame" x="0.0" y="68" width="375" height="200"/>
2624
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
2725
<constraints>
2826
<constraint firstAttribute="height" constant="200" id="mPI-O6-6Yu"/>
@@ -45,15 +43,15 @@
4543
</cells>
4644
</collectionView>
4745
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="SqK-AS-fCp">
48-
<rect key="frame" x="0.0" y="312" width="375" height="355"/>
46+
<rect key="frame" x="0.0" y="292" width="375" height="375"/>
4947
<subviews>
5048
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="PhW-LY-2UV">
5149
<rect key="frame" x="0.0" y="0.0" width="375" height="46"/>
5250
<subviews>
5351
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="SOE-YZ-ngU">
5452
<rect key="frame" x="16" y="8" width="343" height="30"/>
5553
<subviews>
56-
<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">
54+
<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">
5755
<rect key="frame" x="191" y="0.0" width="154" height="31"/>
5856
<constraints>
5957
<constraint firstAttribute="width" constant="150" id="VgY-Sb-nMy"/>
@@ -95,7 +93,7 @@
9593
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="2oh-g2-WZ8">
9694
<rect key="frame" x="16" y="8" width="343" height="30"/>
9795
<subviews>
98-
<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">
96+
<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">
9997
<rect key="frame" x="191" y="0.0" width="154" height="31"/>
10098
<constraints>
10199
<constraint firstAttribute="width" constant="150" id="N6Q-qa-K5J"/>
@@ -315,7 +313,7 @@
315313
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="HLk-Dk-Q2u" sceneMemberID="viewController">
316314
<toolbarItems/>
317315
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="oae-6G-97C">
318-
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
316+
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
319317
<autoresizingMask key="autoresizingMask"/>
320318
</navigationBar>
321319
<nil name="viewControllers"/>

Example/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- MSPeekCollectionViewDelegateImplementation (3.0.0)
2+
- MSPeekCollectionViewDelegateImplementation (3.0.1)
33

44
DEPENDENCIES:
55
- MSPeekCollectionViewDelegateImplementation (from `../`)
@@ -9,7 +9,7 @@ EXTERNAL SOURCES:
99
:path: "../"
1010

1111
SPEC CHECKSUMS:
12-
MSPeekCollectionViewDelegateImplementation: e1c53a3d70dc37850d529f9fc9b07c29767fccf7
12+
MSPeekCollectionViewDelegateImplementation: fdb8ae5c41baaad0962f489b7fa78a653037cc17
1313

1414
PODFILE CHECKSUM: 8d230cee08c8a8b7e62859eff8016277efe2311d
1515

Example/Pods/Local Podspecs/MSPeekCollectionViewDelegateImplementation.podspec.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Manifest.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Target Support Files/MSPeekCollectionViewDelegateImplementation/MSPeekCollectionViewDelegateImplementation-Info.plist

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MSPeekCollectionViewDelegateImplementation.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

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

MSPeekCollectionViewDelegateImplementation/Classes/MSCollectionViewPeekingBehavior.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public class MSCollectionViewPeekingBehavior {
6363
paging.dataSource = self
6464
}
6565

66+
/// Scrolls to an item at a specific index with or without animation
67+
public func scrollToItem(at index: Int, animated: Bool) {
68+
layout.collectionView?.setContentOffset(layout.startingPointForItem(index: index), animated: animated)
69+
}
70+
6671
/// Required function to be called when the `scrollViewWillEndDragging` `UICollectionViewDelegate` function is called
6772
public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
6873
paging.collectionViewWillEndDragging(scrollDirection: scrollDirection, withVelocity: velocity, targetContentOffset: targetContentOffset)

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@ extension ViewController: UICollectionViewDelegate {
184184
}
185185
```
186186

187+
## Features
188+
### Scrolling to a specific item
189+
The `MSCollectionViewPeekingBehavior` now has a function to scroll to a specific index
190+
```swift
191+
public func scrollToItem(at index: Int, animated: Bool)
192+
```
193+
194+
You can do something like:
195+
```swift
196+
behavior.scrollToItem(at: 1, animated: true)
197+
```
198+
187199
## Customization
188200
### Vertical Scroll Direction
189201
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:

0 commit comments

Comments
 (0)