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

Delegate for Programmatically Scroll + Lock\Unlock a Page #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@
<string>PWParallaxScrollView</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>0D02EFF2-E474-4994-9FFA-5E26408D6717</key>
<string>https://github.com/wpsteak/PWParallaxScrollView.git</string>
<key>3E183C459E55170D25FCA0466CE3115E287F8D92</key>
<string>github.com:iradicator/PWParallaxScrollView.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>PWParallaxScrollView.xcodeproj/project.xcworkspace</string>
<string>PWParallaxScrollView.xcodeproj</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>0D02EFF2-E474-4994-9FFA-5E26408D6717</key>
<key>3E183C459E55170D25FCA0466CE3115E287F8D92</key>
<string>../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>https://github.com/wpsteak/PWParallaxScrollView.git</string>
<string>github.com:iradicator/PWParallaxScrollView.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>110</integer>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>0D02EFF2-E474-4994-9FFA-5E26408D6717</string>
<string>3E183C459E55170D25FCA0466CE3115E287F8D92</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>0D02EFF2-E474-4994-9FFA-5E26408D6717</string>
<string>3E183C459E55170D25FCA0466CE3115E287F8D92</string>
<key>IDESourceControlWCCName</key>
<string>PWParallaxScrollView</string>
</dict>
Expand Down
3 changes: 3 additions & 0 deletions PWParallaxScrollView/PWParallaxScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
@property (nonatomic, assign) UIEdgeInsets foregroundScreenEdgeInsets;

@property (nonatomic, assign) NSInteger maxAllowableItem;
@property (nonatomic, assign) BOOL enabled;
@property (nonatomic, assign) BOOL showsScrollIndicator;

- (void)prevItem;
- (void)nextItem;
Expand All @@ -43,6 +45,7 @@
- (void)parallaxScrollView:(PWParallaxScrollView *)scrollView didChangeIndex:(NSInteger)index;
- (void)parallaxScrollView:(PWParallaxScrollView *)scrollView didEndDeceleratingAtIndex:(NSInteger)index;
- (void)parallaxScrollView:(PWParallaxScrollView *)scrollView didRecieveTapAtIndex:(NSInteger)index;
- (void)parallaxScrollView:(PWParallaxScrollView *)scrollView didEndScrollingAnimation:(NSInteger)index;

@end

Expand Down
39 changes: 36 additions & 3 deletions PWParallaxScrollView/PWParallaxScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ - (id)initWithFrame:(CGRect)frame
return self;
}

-(void)setFrame:(CGRect)frame{
[super setFrame:frame];
_touchScrollView.frame = self.bounds;
_foregroundScrollView.frame = self.bounds;
_backgroundScrollView.frame = self.bounds;
}

- (void)setDataSource:(id<PWParallaxScrollViewDataSource>)dataSource
{
_dataSource = dataSource;
Expand Down Expand Up @@ -92,14 +99,18 @@ - (void)initControl
[self addSubview:_backgroundScrollView];
[self addSubview:_foregroundScrollView];
[self addSubview:_touchScrollView];

_enabled = YES;
}

#pragma mark - public method

- (void)moveToIndex:(NSInteger)index
{
CGFloat newOffsetX = index * CGRectGetWidth(_touchScrollView.frame);
[_touchScrollView scrollRectToVisible:CGRectMake(newOffsetX, 0, CGRectGetWidth(_touchScrollView.frame), CGRectGetHeight(_touchScrollView.frame)) animated:YES];
if (_enabled) {
CGFloat newOffsetX = index * CGRectGetWidth(_touchScrollView.frame);
[_touchScrollView scrollRectToVisible:CGRectMake(newOffsetX, 0, CGRectGetWidth(_touchScrollView.frame), CGRectGetHeight(_touchScrollView.frame)) animated:YES];
}
}

- (void)prevItem
Expand Down Expand Up @@ -144,6 +155,21 @@ - (void)reloadData
}
}

- (void)setEnabled:(BOOL)enabled
{
_enabled = enabled;
_touchScrollView.multipleTouchEnabled = enabled;
_touchScrollView.pagingEnabled = enabled;
_backgroundScrollView.pagingEnabled = enabled;
_touchScrollView.delegate = (enabled? self : nil);
}

- (void)setShowsScrollIndicator:(BOOL)showsScrollIndicator {
_touchScrollView.showsHorizontalScrollIndicator = showsScrollIndicator;
_foregroundScrollView.showsHorizontalScrollIndicator = showsScrollIndicator;
_backgroundScrollView.showsHorizontalScrollIndicator = showsScrollIndicator;
}

#pragma mark - private method
- (void)touchScrollViewTapped:(id)sender
{
Expand Down Expand Up @@ -304,6 +330,13 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
}
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
if([self.delegate respondsToSelector:@selector(parallaxScrollView:didEndScrollingAnimation:)]){
[self.delegate parallaxScrollView:self didEndScrollingAnimation:self.currentIndex];
}
}

#pragma mark hitTest
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
Expand All @@ -316,7 +349,7 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
}
}

return [super hitTest:point withEvent:event];
return (_enabled)? [super hitTest:point withEvent:event] : nil;
}

@end
13 changes: 13 additions & 0 deletions PWParallaxScrollViewExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ @interface ViewController () <PWParallaxScrollViewDataSource, PWParallaxScrollVi
@property (nonatomic, strong) NSArray *photos;

@property (nonatomic, weak) IBOutlet UIPageControl *pageControl;
@property (nonatomic, weak) IBOutlet UIButton *lockButton;

@end

@implementation ViewController
Expand All @@ -34,6 +36,11 @@ - (IBAction)jumpToItem:(id)sender
[_scrollView moveToIndex:3];
}

- (IBAction)toggleLock:(UIButton*)senderButton
{
[senderButton setTitle:(_scrollView.enabled? @"Unlock" : @"Lock") forState:UIControlStateNormal];
[_scrollView setEnabled:!_scrollView.enabled];
}
#pragma mark - PWParallaxScrollViewSource

- (NSInteger)numberOfItemsInScrollView:(PWParallaxScrollView *)scrollView
Expand Down Expand Up @@ -86,6 +93,11 @@ - (void)parallaxScrollView:(PWParallaxScrollView *)scrollView didEndDecelerating

}

- (void)parallaxScrollView:(PWParallaxScrollView *)scrollView didEndScrollingAnimation:(NSInteger)index
{

}

#pragma mark - view's life cycle

- (void)initControl
Expand All @@ -94,6 +106,7 @@ - (void)initControl

// _scrollView.foregroundScreenEdgeInsets = UIEdgeInsetsZero;
_scrollView.foregroundScreenEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 100);
_scrollView.showsScrollIndicator = NO;
[self.view insertSubview:_scrollView atIndex:0];
}

Expand Down
27 changes: 20 additions & 7 deletions PWParallaxScrollViewExample/en.lproj/ViewController_iPad.xib
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.iPad.XIB" version="3.0" toolsVersion="5056" systemVersion="13C64" targetRuntime="iOS.CocoaTouch.iPad" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.iPad.XIB" version="3.0" toolsVersion="6245" systemVersion="13F34" targetRuntime="iOS.CocoaTouch.iPad" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment defaultVersion="1552" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6238"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="ViewController">
<connections>
<outlet property="lockButton" destination="8cG-Qd-rjo" id="agh-ph-KW8"/>
<outlet property="pageControl" destination="uyn-Mt-3QN" id="jMC-Yl-n2h"/>
<outlet property="view" destination="2" id="3"/>
</connections>
Expand All @@ -18,7 +18,6 @@
<subviews>
<button opaque="NO" alpha="0.80000001192092896" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="jV5-jH-wQp">
<rect key="frame" x="978" y="363" width="46" height="41"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.098039215690000001" green="0.098039215690000001" blue="0.098039215690000001" alpha="0.59999999999999998" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<state key="normal" title="next">
Expand All @@ -31,7 +30,6 @@
</button>
<button opaque="NO" alpha="0.80000001192092896" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="n2H-nx-Hc3">
<rect key="frame" x="0.0" y="363" width="46" height="41"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.098039215690000001" green="0.098039215690000001" blue="0.098039215690000001" alpha="0.59999999999999998" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<state key="normal" title="prev">
Expand All @@ -44,7 +42,6 @@
</button>
<button opaque="NO" alpha="0.80000001192092896" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="CI1-Eg-D74">
<rect key="frame" x="833" y="707" width="171" height="41"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.098039215690000001" green="0.098039215690000001" blue="0.098039215690000001" alpha="0.59999999999999998" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<state key="normal" title="move to Index 3">
Expand All @@ -57,12 +54,28 @@
</button>
<pageControl opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" numberOfPages="3" translatesAutoresizingMaskIntoConstraints="NO" id="uyn-Mt-3QN">
<rect key="frame" x="492" y="709" width="39" height="37"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</pageControl>
<button opaque="NO" alpha="0.80000001192092896" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="8cG-Qd-rjo">
<rect key="frame" x="8" y="707" width="171" height="41"/>
<color key="backgroundColor" red="0.098039215690000001" green="0.098039215690000001" blue="0.098039215690000001" alpha="0.59999999999999998" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<state key="normal" title="Lock">
<color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="toggleLock:" destination="-1" eventType="touchUpInside" id="HaT-1g-uQS"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<simulatedStatusBarMetrics key="simulatedStatusBarMetrics" statusBarStyle="blackOpaque"/>
<simulatedOrientationMetrics key="simulatedOrientationMetrics" orientation="landscapeRight"/>
</view>
</objects>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>
<simulatedScreenMetrics key="destination"/>
</simulatedMetricsContainer>
</document>
28 changes: 20 additions & 8 deletions PWParallaxScrollViewExample/en.lproj/ViewController_iPhone.xib
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="5056" systemVersion="13C64" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6245" systemVersion="13F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment defaultVersion="1552" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6238"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="ViewController">
<connections>
<outlet property="lockButton" destination="SeT-De-fD6" id="yGf-HS-zXK"/>
<outlet property="pageControl" destination="D7h-4N-qEE" id="uEG-YQ-z5x"/>
<outlet property="view" destination="6" id="7"/>
</connections>
Expand All @@ -18,7 +19,6 @@
<subviews>
<button opaque="NO" alpha="0.80000000000000004" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="vb0-Az-XYC">
<rect key="frame" x="508" y="139" width="60" height="41"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.098039215686274508" green="0.098039215686274508" blue="0.098039215686274508" alpha="0.59999999999999998" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<state key="normal" title="next">
Expand All @@ -31,7 +31,6 @@
</button>
<button opaque="NO" alpha="0.80000000000000004" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="BnA-Ag-Ota">
<rect key="frame" x="0.0" y="139" width="60" height="41"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.098039215686274508" green="0.098039215686274508" blue="0.098039215686274508" alpha="0.59999999999999998" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<state key="normal" title="prev">
Expand All @@ -44,7 +43,6 @@
</button>
<button opaque="NO" alpha="0.80000000000000004" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="4i9-yP-AuL">
<rect key="frame" x="370" y="283" width="183" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.098039215686274508" green="0.098039215686274508" blue="0.098039215686274508" alpha="0.59999999999999998" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<state key="normal" title="move to Index 3">
Expand All @@ -57,12 +55,26 @@
</button>
<pageControl opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" numberOfPages="3" translatesAutoresizingMaskIntoConstraints="NO" id="D7h-4N-qEE">
<rect key="frame" x="265" y="279" width="39" height="37"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</pageControl>
<button opaque="NO" alpha="0.80000001192092896" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="SeT-De-fD6">
<rect key="frame" x="8" y="283" width="183" height="29"/>
<color key="backgroundColor" red="0.098039215690000001" green="0.098039215690000001" blue="0.098039215690000001" alpha="0.59999999999999998" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="24"/>
<state key="normal" title="Lock">
<color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="toggleLock:" destination="-1" eventType="touchUpInside" id="S3R-21-def"/>
</connections>
</button>
</subviews>
<simulatedStatusBarMetrics key="simulatedStatusBarMetrics"/>
<simulatedOrientationMetrics key="simulatedOrientationMetrics" orientation="landscapeRight"/>
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina4"/>
</view>
</objects>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>
<simulatedScreenMetrics key="destination" type="retina4"/>
</simulatedMetricsContainer>
</document>