Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Add horizontal support (not completed)
Browse files Browse the repository at this point in the history
  • Loading branch information
marciomeschini committed Sep 30, 2012
1 parent 539b1c0 commit d288d5f
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 70 deletions.
46 changes: 32 additions & 14 deletions FWTOverlayView/FWTOverlayView/FWTOverlayScrollViewHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
#import "FWTOverlayScrollViewHelper.h"
#import <objc/runtime.h>
#import <QuartzCore/QuartzCore.h>
#import "UIScrollView+FWTOverlayView.h"

#define DEBUG_ENABLED NO
#define DEBUG_ENABLED YES

static BOOL isMethodPartOfProtocol(SEL aSelector, Protocol *aProtocol)
{
Expand Down Expand Up @@ -222,29 +223,46 @@ - (void)_layoutOverlayView
- (CGRect)_overlayFrame
{
// Work out positions
CGFloat maxTableHeight = self.scrollView.contentSize.height;
CGFloat frameTableHeight = self.scrollView.frame.size.height;
CGFloat workingTableHeight = maxTableHeight - frameTableHeight;
CGFloat currentTablePosition = self.scrollView.contentOffset.y;
CGFloat currentTablePositionPercentage = currentTablePosition / workingTableHeight;
currentTablePositionPercentage = MAX(currentTablePositionPercentage, .0f);
currentTablePositionPercentage = MIN(currentTablePositionPercentage, 1.0f);

CGFloat currentTablePositionPercentage = [self.scrollView fwt_contentOffsetPercentageClampEnabled:YES];
CGSize overlaySize = self.overlayView.frame.size;
CGRect overlayFrame = [self _overlayBounds];
overlayFrame.origin.y += ((CGRectGetHeight(overlayFrame)-overlaySize.height)*currentTablePositionPercentage); // adjust y
overlayFrame.size.height = overlaySize.height;

FWTScrollViewDirection direction = [self.scrollView fwt_scrollDirection];
if (direction == FWTScrollViewDirectionVertical)
{
overlayFrame.origin.y += ((CGRectGetHeight(overlayFrame)-overlaySize.height)*currentTablePositionPercentage); // adjust y
overlayFrame.size.height = overlaySize.height;
}
else if (direction == FWTScrollViewDirectionHorizontal)
{
overlayFrame.origin.x += ((CGRectGetWidth(overlayFrame)-overlaySize.width)*currentTablePositionPercentage); // adjust y
overlayFrame.size.width = overlaySize.width;
}

return overlayFrame;
}

- (CGRect)_overlayBounds
{
CGSize overlaySize = self.overlayView.frame.size;
CGRect toReturn = UIEdgeInsetsInsetRect(self.scrollView.bounds, self.edgeInsets);
if (self.edgeInsets.left == .0f)
toReturn.origin.x += CGRectGetWidth(toReturn)-overlaySize.width;

toReturn.size.width = overlaySize.width;
FWTScrollViewDirection direction = [self.scrollView fwt_scrollDirection];
if (direction == FWTScrollViewDirectionVertical)
{
if (self.edgeInsets.left == .0f)
toReturn.origin.x += CGRectGetWidth(toReturn)-overlaySize.width;

toReturn.size.width = overlaySize.width;
}
else if (direction == FWTScrollViewDirectionHorizontal)
{
if (self.edgeInsets.top == .0f)
toReturn.origin.y += CGRectGetHeight(toReturn)-overlaySize.height;

toReturn.size.height = overlaySize.height;
}

return toReturn;
}

Expand Down
12 changes: 12 additions & 0 deletions FWTOverlayView/FWTOverlayView/UIScrollView+FWTOverlayView.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@

#import <UIKit/UIKit.h>

typedef enum _FWTScrollViewDirection
{
FWTScrollViewDirectionNone,
FWTScrollViewDirectionVertical,
FWTScrollViewDirectionHorizontal,
} FWTScrollViewDirection;

@interface UIScrollView (FWTOverlayView)

@property (nonatomic, retain) UIView *fwt_overlayView;
@property (nonatomic, assign) UIEdgeInsets fwt_overlayViewEdgeInsets;
@property (nonatomic, assign) CGFloat fwt_overlayViewHideAfterDelay;
@property (nonatomic, readonly, assign) CGPoint fwt_overlayViewCenter;

@property (nonatomic, readonly, assign) CGFloat fwt_contentOffsetPercentage;
- (CGFloat)fwt_contentOffsetPercentageClampEnabled:(BOOL)clampEnabled;

@property (nonatomic, readonly, assign) FWTScrollViewDirection fwt_scrollDirection;

@end
35 changes: 35 additions & 0 deletions FWTOverlayView/FWTOverlayView/UIScrollView+FWTOverlayView.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,39 @@ - (CGPoint)fwt_overlayViewCenter
return self.fwt_overlayView.center;
}

- (CGFloat)fwt_contentOffsetPercentage
{
return [self fwt_contentOffsetPercentageClampEnabled:NO];
}

- (CGFloat)fwt_contentOffsetPercentageClampEnabled:(BOOL)clampEnabled
{
BOOL isHorizontal = [self fwt_scrollDirection] == FWTScrollViewDirectionHorizontal ? YES : NO;
CGFloat contentSize = isHorizontal ? self.contentSize.width : self.contentSize.height;
CGFloat frameSize = isHorizontal ? self.frame.size.width : self.frame.size.height;
CGFloat currentTablePosition = isHorizontal ? self.contentOffset.x : self.contentOffset.y;

CGFloat workingTableHeight = contentSize - frameSize;
CGFloat currentTablePositionPercentage = currentTablePosition / workingTableHeight;

if (clampEnabled)
{
currentTablePositionPercentage = MAX(currentTablePositionPercentage, .0f);
currentTablePositionPercentage = MIN(currentTablePositionPercentage, 1.0f);
}

return currentTablePositionPercentage;
}

- (FWTScrollViewDirection)fwt_scrollDirection
{
FWTScrollViewDirection toReturn = FWTScrollViewDirectionNone;
if (self.contentSize.width > CGRectGetWidth(self.frame))
toReturn = FWTScrollViewDirectionHorizontal;
else if (self.contentSize.height > CGRectGetHeight(self.frame))
toReturn = FWTScrollViewDirectionVertical;

return toReturn;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
A99759F21611C5EC00BFB780 /* TableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A99759F11611C5EC00BFB780 /* TableViewController.m */; };
A99759F51611C6B100BFB780 /* OverlayView.m in Sources */ = {isa = PBXBuildFile; fileRef = A99759F41611C6B100BFB780 /* OverlayView.m */; };
A99759F71611C6C100BFB780 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A99759F61611C6C100BFB780 /* QuartzCore.framework */; };
A99759FA1611C99200BFB780 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A99759F91611C99200BFB780 /* ViewController.m */; };
A99759FA1611C99200BFB780 /* ScrollViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A99759F91611C99200BFB780 /* ScrollViewController.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -46,8 +46,8 @@
A99759F31611C6B100BFB780 /* OverlayView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OverlayView.h; sourceTree = "<group>"; };
A99759F41611C6B100BFB780 /* OverlayView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OverlayView.m; sourceTree = "<group>"; };
A99759F61611C6C100BFB780 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
A99759F81611C99200BFB780 /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
A99759F91611C99200BFB780 /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
A99759F81611C99200BFB780 /* ScrollViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScrollViewController.h; sourceTree = "<group>"; };
A99759F91611C99200BFB780 /* ScrollViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ScrollViewController.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -125,8 +125,8 @@
A9975A0C1611E9D200BFB780 /* Sample */ = {
isa = PBXGroup;
children = (
A99759F81611C99200BFB780 /* ViewController.h */,
A99759F91611C99200BFB780 /* ViewController.m */,
A99759F81611C99200BFB780 /* ScrollViewController.h */,
A99759F91611C99200BFB780 /* ScrollViewController.m */,
A99759F01611C5EC00BFB780 /* TableViewController.h */,
A99759F11611C5EC00BFB780 /* TableViewController.m */,
A99759F31611C6B100BFB780 /* OverlayView.h */,
Expand Down Expand Up @@ -205,7 +205,7 @@
A99759EF1611C4D800BFB780 /* SamplePickerViewController.m in Sources */,
A99759F21611C5EC00BFB780 /* TableViewController.m in Sources */,
A99759F51611C6B100BFB780 /* OverlayView.m in Sources */,
A99759FA1611C99200BFB780 /* ViewController.m in Sources */,
A99759FA1611C99200BFB780 /* ScrollViewController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//

#import "SamplePickerViewController.h"
#import "ScrollViewController.h"
#import "TableViewController.h"

@interface SamplePickerViewController ()
{
Expand Down Expand Up @@ -43,8 +45,9 @@ - (NSArray *)items
{
if (!self->_items)
self->_items = [@[
@"ViewController",
@"TableViewController",
[ScrollViewController scrollViewControllerWithContentSize:CGSizeMake(CGRectGetWidth(self.view.frame), 3000.0f)],
[ScrollViewController scrollViewControllerWithContentSize:CGSizeMake(3000.0f, CGRectGetHeight(self.view.frame))],
[[[TableViewController alloc] init] autorelease],
] retain];

return self->_items;
Expand All @@ -62,15 +65,15 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
if (!cell)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

cell.textLabel.text = [self.items objectAtIndex:indexPath.row];
UIViewController *vc = [self.items objectAtIndex:indexPath.row];
cell.textLabel.text = vc.title;

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *className = [self.items objectAtIndex:indexPath.row];
UIViewController *vc = [[[NSClassFromString(className) alloc] init] autorelease];
UIViewController *vc = [self.items objectAtIndex:indexPath.row];
[self.navigationController pushViewController:vc animated:YES];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@interface ScrollViewController : UIViewController

+ (id)scrollViewControllerWithContentSize:(CGSize)contentSize;

@end
67 changes: 67 additions & 0 deletions FWTOverlayView_Test/FWTOverlayView_Test/ScrollViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// ViewController.m
// FWTOverlayView_Test
//
// Created by Marco Meschini on 25/09/2012.
// Copyright (c) 2012 Marco Meschini. All rights reserved.
//

#import "ScrollViewController.h"
#import "OverlayView.h"
#import "UIScrollView+FWTOverlayView.h"

@interface ScrollViewController () <UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, assign) CGSize contentSize;
@end

@implementation ScrollViewController

- (void)dealloc
{
self.scrollView = nil;
[super dealloc];
}

- (void)loadView
{
[super loadView];

self.scrollView = [[[UIScrollView alloc] initWithFrame:self.view.bounds] autorelease];
self.scrollView.autoresizingMask = self.view.autoresizingMask;
self.scrollView.contentSize = self.contentSize;
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];

OverlayView *overlayView = [[[OverlayView alloc] initWithFrame:CGRectMake(.0f, .0f, 70.0f, 30.0f)] autorelease];
overlayView.textLabel.numberOfLines = 0;
self.scrollView.fwt_overlayView = overlayView;

if (self.contentSize.width > self.contentSize.height)
self.scrollView.fwt_overlayViewEdgeInsets = UIEdgeInsetsMake(2.0f, 2.0f, .0f, 2.0f);
else
self.scrollView.fwt_overlayViewEdgeInsets = UIEdgeInsetsMake(2.0f, .0f, 2.0f, 10.0f);
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.scrollView flashScrollIndicators];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat percentage = [scrollView fwt_contentOffsetPercentageClampEnabled:NO];
OverlayView *overlayView = (OverlayView *)scrollView.fwt_overlayView;
overlayView.textLabel.text = [NSString stringWithFormat:@"%f", percentage];
}

+ (id)scrollViewControllerWithContentSize:(CGSize)contentSize
{
ScrollViewController *toReturn = [[[ScrollViewController alloc] init] autorelease];
toReturn.contentSize = contentSize;
toReturn.title = contentSize.width > contentSize.height ? @"horizontal" : @"vertical";
return toReturn;
}

@end
9 changes: 9 additions & 0 deletions FWTOverlayView_Test/FWTOverlayView_Test/TableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ - (void)dealloc
[super dealloc];
}

- (id)init
{
if ((self = [super init]))
{
self.title = @"table";
}
return self;
}

- (void)loadView
{
[super loadView];
Expand Down
44 changes: 0 additions & 44 deletions FWTOverlayView_Test/FWTOverlayView_Test/ViewController.m

This file was deleted.

0 comments on commit d288d5f

Please sign in to comment.