Skip to content

Commit

Permalink
as well as being able to drag the bottom scrollview to move between p…
Browse files Browse the repository at this point in the history
…ages (like you always used to be able to do), you can now drag along the top scroller too.

Also moved the code that updates the page number in the UIPageControl to happen when the scrolling has finished, to stop it glitching between pages if you move it quickly (as the scroll view "bounces" at the end of a quick drag)
  • Loading branch information
Tom Thorpe committed Apr 20, 2013
1 parent 3e35f2d commit aec62f1
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
6 changes: 6 additions & 0 deletions UIScrollSlidingPages.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
9F9B9B9C1703758A00CD45B4 /* TTViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9F9B9B9A1703758A00CD45B4 /* TTViewController.xib */; };
9F9B9BA117037C0B00CD45B4 /* TTScrollSlidingPagesController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9F9B9BA017037C0B00CD45B4 /* TTScrollSlidingPagesController.m */; };
9F9B9BA51703836D00CD45B4 /* TTSlidingPage.m in Sources */ = {isa = PBXBuildFile; fileRef = 9F9B9BA41703836D00CD45B4 /* TTSlidingPage.m */; };
9FFDC5CC1722EEA1002A5689 /* TTScrollViewWrapper.m in Sources */ = {isa = PBXBuildFile; fileRef = 9FFDC5CB1722EEA1002A5689 /* TTScrollViewWrapper.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -73,6 +74,8 @@
9F9B9BA31703836D00CD45B4 /* TTSlidingPage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TTSlidingPage.h; sourceTree = "<group>"; };
9F9B9BA41703836D00CD45B4 /* TTSlidingPage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TTSlidingPage.m; sourceTree = "<group>"; };
9F9B9BA7170387BE00CD45B4 /* TTSlidingPagesDataSource.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TTSlidingPagesDataSource.h; sourceTree = "<group>"; };
9FFDC5CA1722EEA1002A5689 /* TTScrollViewWrapper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TTScrollViewWrapper.h; sourceTree = "<group>"; };
9FFDC5CB1722EEA1002A5689 /* TTScrollViewWrapper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TTScrollViewWrapper.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -180,6 +183,8 @@
9F9B9BA7170387BE00CD45B4 /* TTSlidingPagesDataSource.h */,
9F602A65171E083F00088A9C /* TTBlackTriangle.h */,
9F602A66171E083F00088A9C /* TTBlackTriangle.m */,
9FFDC5CA1722EEA1002A5689 /* TTScrollViewWrapper.h */,
9FFDC5CB1722EEA1002A5689 /* TTScrollViewWrapper.m */,
);
path = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -306,6 +311,7 @@
9F9B9BA51703836D00CD45B4 /* TTSlidingPage.m in Sources */,
9F936B55170650EF001622D8 /* TTSlidingPageTitle.m in Sources */,
9F602A67171E083F00088A9C /* TTBlackTriangle.m in Sources */,
9FFDC5CC1722EEA1002A5689 /* TTScrollViewWrapper.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
19 changes: 13 additions & 6 deletions UIScrollViewSlidingPages/Source/TTScrollSlidingPagesController.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ this software and associated documentation files (the "Software"), to deal in
#import "TTSlidingPageTitle.h"
#import <QuartzCore/QuartzCore.h>
#import "TTBlackTriangle.h"
#import "TTScrollViewWrapper.h"

@interface TTScrollSlidingPagesController ()

Expand Down Expand Up @@ -104,9 +105,10 @@ - (void)viewDidLoad
topScrollView.backgroundColor = [UIColor clearColor];
topScrollView.pagingEnabled = self.pagingEnabled;
topScrollView.delegate = self; //move the bottom scroller proportionally as you drag the top.
UIView *topScrollViewWrapper = [[UIView alloc] initWithFrame:CGRectMake(0, nextYPosition, self.view.frame.size.width, self.titleScrollerHeight)];//make the view to put the scroll view inside.
TTScrollViewWrapper *topScrollViewWrapper = [[TTScrollViewWrapper alloc] initWithFrame:CGRectMake(0, nextYPosition, self.view.frame.size.width, self.titleScrollerHeight) andUIScrollView:topScrollView];//make the view to put the scroll view inside which will allow the background colour, and allow dragging from anywhere in this wrapper to be passed to the scrollview.
topScrollViewWrapper.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
topScrollViewWrapper.backgroundColor = self.titleScrollerBackgroundColour; //set the background colour (the whole point of having the wrapper)
topScrollViewWrapper.backgroundColor = self.titleScrollerBackgroundColour;
//pass touch events from the wrapper onto the scrollview (so you can drag from the entire width, as the scrollview itself only lives in the very centre, but with clipToBounds turned off)
[topScrollViewWrapper addSubview:topScrollView];//put the top scroll view in the wrapper.
[self.view addSubview:topScrollViewWrapper]; //put the wrapper in this view.
nextYPosition += self.titleScrollerHeight;
Expand Down Expand Up @@ -363,13 +365,18 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
topScrollView.delegate = self;
}

int currentPage = [self getCurrentDisplayedPage];

//set the correct page on the pagedots
pageControl.currentPage = currentPage;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

//update the pagedots pagenuber
if (!self.disableUIPageControl){
int currentPage = [self getCurrentDisplayedPage];

//set the correct page on the pagedots
pageControl.currentPage = currentPage;
}

/*Just do a quick check, that if the paging enabled property is YES (paging is enabled), the user should not define widthForPageOnSlidingPagesViewController on the datasource delegate because scrollviews do not cope well with paging being enabled for scrollviews where each subview is not full width! */
if (self.pagingEnabled == YES && [self.dataSource respondsToSelector:@selector(widthForPageOnSlidingPagesViewController:atIndex:)]){
NSLog(@"Warning: TTScrollSlidingPagesController. You have paging enabled in the TTScrollSlidingPagesController (pagingEnabled is either not set, or specifically set to YES), but you have also implemented widthForPageOnSlidingPagesViewController:atIndex:. ScrollViews do not cope well with paging being disabled when items have custom widths. You may get weird behaviour with your paging, in which case you should either disable paging (set pagingEnabled to NO) and keep widthForPageOnSlidingPagesViewController:atIndex: implented, or not implement widthForPageOnSlidingPagesViewController:atIndex: in your datasource for the TTScrollSlidingPagesController instance.");
Expand Down
17 changes: 17 additions & 0 deletions UIScrollViewSlidingPages/Source/TTScrollViewWrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// TTScrollViewWrapper.h
// UIScrollSlidingPages
//
// Created by Thomas Thorpe on 20/04/2013.
// Copyright (c) 2013 Thomas Thorpe. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TTScrollViewWrapper : UIView{
UIScrollView *scrollView;
}

- (id)initWithFrame:(CGRect)frame andUIScrollView: (UIScrollView *)scroll;

@end
57 changes: 57 additions & 0 deletions UIScrollViewSlidingPages/Source/TTScrollViewWrapper.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// TTScrollViewWrapper.m
// UIScrollSlidingPages
//
// The purpose of this class is purely to be a wrapper that you can put a UIScrollView inside, and it will pass all it's touch events to the UIScrollView. This is so you can turn the ClipToBounds property of the UIScrollView off (so that a paged scroller still shows items to the left and right such as in the topScroller title), but you can still drag anywhere along the ScrollViewWrapper, and the drag gets passed to the scrollView.
//
// Created by Thomas Thorpe on 20/04/2013.
// Copyright (c) 2013 Thomas Thorpe. All rights reserved.
//

/*
Copyright (c) 2012 Tom Thorpe. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#import "TTScrollViewWrapper.h"

@implementation TTScrollViewWrapper

- (id)initWithFrame:(CGRect)frame andUIScrollView: (UIScrollView *)scroll
{
self = [super initWithFrame:frame];
if (self) {
scrollView = scroll;
}
return self;
}

//This method means that when anything hits inside of this view, it will actually pass on the hit to scrollView instead.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if ([self pointInside:point withEvent:event])
{
return scrollView;
}
return nil;
}


@end

0 comments on commit aec62f1

Please sign in to comment.