Skip to content

Commit

Permalink
renamed classes, enforce size constraints, removed nibs
Browse files Browse the repository at this point in the history
  • Loading branch information
mlilback committed Jul 20, 2012
1 parent b923cc6 commit f2de2d7
Show file tree
Hide file tree
Showing 13 changed files with 398 additions and 442 deletions.
41 changes: 17 additions & 24 deletions CustomSplitView.xcodeproj/project.pbxproj
Expand Up @@ -13,13 +13,13 @@
08DF3D1B15B898C5000A339D /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 08DF3D1915B898C5000A339D /* InfoPlist.strings */; };
08DF3D1D15B898C5000A339D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 08DF3D1C15B898C5000A339D /* main.m */; };
08DF3D2115B898C5000A339D /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 08DF3D2015B898C5000A339D /* AppDelegate.m */; };
08DF3D2415B898C5000A339D /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 08DF3D2315B898C5000A339D /* ViewController.m */; };
08DF3D2715B898C5000A339D /* ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 08DF3D2515B898C5000A339D /* ViewController.xib */; };
08DF3D2415B898C5000A339D /* AMResizableSplitViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 08DF3D2315B898C5000A339D /* AMResizableSplitViewController.m */; };
08DF3D3015B898F3000A339D /* ImageViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 08DF3D2E15B898F3000A339D /* ImageViewController.m */; };
08DF3D3115B898F3000A339D /* ImageViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 08DF3D2F15B898F3000A339D /* ImageViewController.xib */; };
08DF3D3315B89D32000A339D /* pdolls.png in Resources */ = {isa = PBXBuildFile; fileRef = 08DF3D3215B89D32000A339D /* pdolls.png */; };
08DF3D3515B89D7B000A339D /* background.png in Resources */ = {isa = PBXBuildFile; fileRef = 08DF3D3415B89D7B000A339D /* background.png */; };
08DF3D3815B8A112000A339D /* SplitterView.m in Sources */ = {isa = PBXBuildFile; fileRef = 08DF3D3715B8A112000A339D /* SplitterView.m */; };
08DF3D3815B8A112000A339D /* AMResizableSplitterView.m in Sources */ = {isa = PBXBuildFile; fileRef = 08DF3D3715B8A112000A339D /* AMResizableSplitterView.m */; };
08DF3D3F15B9F095000A339D /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08DF3D3E15B9F095000A339D /* QuartzCore.framework */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -33,23 +33,24 @@
08DF3D1E15B898C5000A339D /* CustomSplitView-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "CustomSplitView-Prefix.pch"; sourceTree = "<group>"; };
08DF3D1F15B898C5000A339D /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
08DF3D2015B898C5000A339D /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
08DF3D2215B898C5000A339D /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
08DF3D2315B898C5000A339D /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
08DF3D2615B898C5000A339D /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController.xib; sourceTree = "<group>"; };
08DF3D2215B898C5000A339D /* AMResizableSplitViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AMResizableSplitViewController.h; sourceTree = "<group>"; };
08DF3D2315B898C5000A339D /* AMResizableSplitViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AMResizableSplitViewController.m; sourceTree = "<group>"; };
08DF3D2D15B898F3000A339D /* ImageViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageViewController.h; sourceTree = "<group>"; };
08DF3D2E15B898F3000A339D /* ImageViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ImageViewController.m; sourceTree = "<group>"; };
08DF3D2F15B898F3000A339D /* ImageViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ImageViewController.xib; sourceTree = "<group>"; };
08DF3D3215B89D32000A339D /* pdolls.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = pdolls.png; sourceTree = "<group>"; };
08DF3D3415B89D7B000A339D /* background.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = background.png; sourceTree = "<group>"; };
08DF3D3615B8A112000A339D /* SplitterView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SplitterView.h; sourceTree = "<group>"; };
08DF3D3715B8A112000A339D /* SplitterView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SplitterView.m; sourceTree = "<group>"; };
08DF3D3615B8A112000A339D /* AMResizableSplitterView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AMResizableSplitterView.h; sourceTree = "<group>"; };
08DF3D3715B8A112000A339D /* AMResizableSplitterView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMResizableSplitterView.m; sourceTree = "<group>"; };
08DF3D3E15B9F095000A339D /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
08DF3D0915B898C5000A339D /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
08DF3D3F15B9F095000A339D /* QuartzCore.framework in Frameworks */,
08DF3D1115B898C5000A339D /* UIKit.framework in Frameworks */,
08DF3D1315B898C5000A339D /* Foundation.framework in Frameworks */,
08DF3D1515B898C5000A339D /* CoreGraphics.framework in Frameworks */,
Expand Down Expand Up @@ -79,6 +80,7 @@
08DF3D0F15B898C5000A339D /* Frameworks */ = {
isa = PBXGroup;
children = (
08DF3D3E15B9F095000A339D /* QuartzCore.framework */,
08DF3D1015B898C5000A339D /* UIKit.framework */,
08DF3D1215B898C5000A339D /* Foundation.framework */,
08DF3D1415B898C5000A339D /* CoreGraphics.framework */,
Expand All @@ -91,11 +93,10 @@
children = (
08DF3D1F15B898C5000A339D /* AppDelegate.h */,
08DF3D2015B898C5000A339D /* AppDelegate.m */,
08DF3D2215B898C5000A339D /* ViewController.h */,
08DF3D2315B898C5000A339D /* ViewController.m */,
08DF3D2515B898C5000A339D /* ViewController.xib */,
08DF3D3615B8A112000A339D /* SplitterView.h */,
08DF3D3715B8A112000A339D /* SplitterView.m */,
08DF3D2215B898C5000A339D /* AMResizableSplitViewController.h */,
08DF3D2315B898C5000A339D /* AMResizableSplitViewController.m */,
08DF3D3615B8A112000A339D /* AMResizableSplitterView.h */,
08DF3D3715B8A112000A339D /* AMResizableSplitterView.m */,
08DF3D2D15B898F3000A339D /* ImageViewController.h */,
08DF3D2E15B898F3000A339D /* ImageViewController.m */,
08DF3D2F15B898F3000A339D /* ImageViewController.xib */,
Expand Down Expand Up @@ -169,7 +170,6 @@
buildActionMask = 2147483647;
files = (
08DF3D1B15B898C5000A339D /* InfoPlist.strings in Resources */,
08DF3D2715B898C5000A339D /* ViewController.xib in Resources */,
08DF3D3115B898F3000A339D /* ImageViewController.xib in Resources */,
08DF3D3315B89D32000A339D /* pdolls.png in Resources */,
08DF3D3515B89D7B000A339D /* background.png in Resources */,
Expand All @@ -185,9 +185,9 @@
files = (
08DF3D1D15B898C5000A339D /* main.m in Sources */,
08DF3D2115B898C5000A339D /* AppDelegate.m in Sources */,
08DF3D2415B898C5000A339D /* ViewController.m in Sources */,
08DF3D2415B898C5000A339D /* AMResizableSplitViewController.m in Sources */,
08DF3D3015B898F3000A339D /* ImageViewController.m in Sources */,
08DF3D3815B8A112000A339D /* SplitterView.m in Sources */,
08DF3D3815B8A112000A339D /* AMResizableSplitterView.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -202,14 +202,6 @@
name = InfoPlist.strings;
sourceTree = "<group>";
};
08DF3D2515B898C5000A339D /* ViewController.xib */ = {
isa = PBXVariantGroup;
children = (
08DF3D2615B898C5000A339D /* en */,
);
name = ViewController.xib;
sourceTree = "<group>";
};
/* End PBXVariantGroup section */

/* Begin XCBuildConfiguration section */
Expand Down Expand Up @@ -305,6 +297,7 @@
08DF3D2C15B898C5000A339D /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
Binary file not shown.
34 changes: 34 additions & 0 deletions CustomSplitView/AMResizableSplitViewController.h
@@ -0,0 +1,34 @@
//
// AMResizableSplitViewController.h
//
// Created by Mark Lilback on 7/19/12.
// Copyright (c) 2012 Agile Monks, LLC. All rights reserved.
//

#import <UIKit/UIKit.h>

@class AMResizableSplitterView;

@interface AMResizableSplitViewController : UIViewController

//the view controllers displayed in the split view
@property (nonatomic) UIViewController *controller1;
@property (nonatomic) UIViewController *controller2;

//the splitter view
@property (nonatomic) AMResizableSplitterView *splitterView;

//the midpoint of the splitter view (x/y depending on orientation)
@property (nonatomic) CGFloat splitterPosition;

//the minimum size (width used when landscape, height used when portrait) for the first view. default is 100, 100
@property (nonatomic) CGSize minimumView1Size;

//the minimum size (width used when landscape, height used when portrait) for the second view. default is 100, 100
@property (nonatomic) CGSize minimumView2Size;

// designated initializer
-(id)init;

-(void)setSplitterPosition:(CGFloat)splitterPosition animated:(BOOL)animated;
@end
215 changes: 215 additions & 0 deletions CustomSplitView/AMResizableSplitViewController.m
@@ -0,0 +1,215 @@
//
// AMResizableSplitViewController.m
//
// Created by Mark Lilback on 7/19/12.
// Copyright (c) 2012 Agile Monks, LLC. All rights reserved.
//

#import "AMResizableSplitViewController.h"
#import "AMResizableSplitterView.h"

#define SPLITTER_LENGTH 20

#pragma mark - class extension
@interface AMResizableSplitViewController () <AMResizableSplitterViewDelegate> {
BOOL _handlingMove;
}
@property (nonatomic) UIView *view1;
@property (nonatomic) UIView *view2;
@end

#pragma mark - implementation

@implementation AMResizableSplitViewController

-(id)init
{
if ((self = [super init])) {
self.minimumView1Size = CGSizeMake(100, 100);
self.minimumView2Size = CGSizeMake(100, 100);
}
return self;
}

#pragma mark - UIViewController overrides

-(void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
view.autoresizesSubviews=NO;
self.view = view;
}

-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.splitterView = [[AMResizableSplitterView alloc] initWithFrame:CGRectMake(502, 0, SPLITTER_LENGTH, 748)];
self.splitterView.delegate = self;
[self.view addSubview:self.splitterView];
}

-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self adjustToDefaultFrames];
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[UIView animateWithDuration:duration animations:^{
[self rotateChildren:toInterfaceOrientation];
}];
}

#pragma mark - meat & potatos

-(void)rotateChildren:(UIInterfaceOrientation)destOrientation
{
BOOL toLand = UIInterfaceOrientationIsLandscape(destOrientation);
CGRect dividerFrame = self.splitterView.frame;
CGRect r1 = self.controller1.view.frame;
CGRect r2 = self.controller2.view.frame;
//adjust view1's frame
CGFloat tmp = r1.size.height;
r1.size.height = r1.size.width;
r1.size.width = tmp;
//adjust splitter's frame
tmp = dividerFrame.origin.y;
dividerFrame.origin.y = dividerFrame.origin.x;
dividerFrame.origin.x = tmp;
tmp = dividerFrame.size.height;
dividerFrame.size.height = dividerFrame.size.width;
dividerFrame.size.width = tmp;
//adjust view2's frame
tmp = r2.origin.y;
r2.origin.y = r2.origin.x;
r2.origin.x = tmp;
tmp = r2.size.height;
r2.size.height = r2.size.width;
r2.size.width = tmp;
//adjust rects for status bar height
if (toLand) {
r1.size.height -= 20;
r2.size.height -= 20;
dividerFrame.size.height -= 20;
} else {
r1.size.width += 20;
r2.size.width += 20;
dividerFrame.size.width += 20;
}
//set the frames
self.splitterView.frame = dividerFrame;
self.controller1.view.frame = r1;
self.controller2.view.frame = r2;
}

-(void)adjustToDefaultFrames
{
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
self.controller1.view.frame = CGRectMake(0, 0, 502, 748);
self.controller2.view.frame = CGRectMake(522, 0, 502, 748);
self.splitterView.frame = CGRectMake(502, 0, SPLITTER_LENGTH, 748);
} else {
self.controller1.view.frame = CGRectMake(0, 0, 768, 502);
self.controller2.view.frame = CGRectMake(0, 522, 768, 502);
self.splitterView.frame = CGRectMake(0, 502, 768, SPLITTER_LENGTH);
}
}

-(void)splitterView:(AMResizableSplitterView*)splitterView moveByOffset:(CGFloat)offset
{
BOOL isLand = UIInterfaceOrientationIsLandscape(self.interfaceOrientation);
CGRect dividerRect = splitterView.frame;

//validate the offset is in the acceptable range
CGFloat curLoc = isLand ? self.splitterView.frame.origin.x : self.splitterView.frame.origin.y;
CGFloat newLoc = curLoc + offset;
CGFloat minOffset = isLand ? self.minimumView1Size.width : self.minimumView1Size.height;
CGFloat maxOffset = isLand ? self.view.bounds.size.width : self.view.bounds.size.height;
maxOffset -= isLand ? self.minimumView2Size.width : self.minimumView2Size.height;
if (newLoc < minOffset)
offset = 0;
if (newLoc > maxOffset)
offset = maxOffset - curLoc;
if (offset == 0)
return;

CGRect r1 = self.controller1.view.frame;
CGRect r2 = self.controller2.view.frame;
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
dividerRect.origin.x = dividerRect.origin.x + offset;
r1.size.width += offset;
r2.origin.x += offset;
r2.size.width = self.view.bounds.size.width - r2.origin.x;
} else {
dividerRect.origin.y = dividerRect.origin.y + offset;
r1.size.height += offset;
r2.origin.y += offset;
r2.size.height = self.view.bounds.size.height - r2.origin.y;
}
splitterView.frame = dividerRect;
self.controller1.view.frame = r1;
self.controller2.view.frame = r2;

[self.view setNeedsDisplay];
}

#pragma mark - accessors

-(void)setController1:(UIViewController *)controller1
{
if (_controller1) {
[_controller1 willMoveToParentViewController:nil];
[_controller1.view removeFromSuperview];
[_controller1 removeFromParentViewController];
}
_controller1 = controller1;
[self addChildViewController:controller1];
[self.view addSubview:controller1.view];
[controller1 didMoveToParentViewController:self];
}

-(void)setController2:(UIViewController *)controller2
{
if (_controller2) {
[_controller2 willMoveToParentViewController:nil];
[_controller2.view removeFromSuperview];
[_controller2 removeFromParentViewController];
}
_controller2 = controller2;
[self addChildViewController:controller2];
[self.view addSubview:controller2.view];
[controller2 didMoveToParentViewController:self];
}

-(CGFloat)splitterPosition
{
if ((UIInterfaceOrientationIsLandscape(self.interfaceOrientation)))
return self.splitterView.frame.origin.x + floorf(SPLITTER_LENGTH/2);
return self.splitterView.frame.origin.y + floorf(SPLITTER_LENGTH/2);
}

-(void)setSplitterPosition:(CGFloat)splitterPosition
{
[self setSplitterPosition:splitterPosition animated:NO];
}

-(void)setSplitterPosition:(CGFloat)splitterPosition animated:(BOOL)animated
{
CGFloat offset = splitterPosition;
if ((UIInterfaceOrientationIsLandscape(self.interfaceOrientation)))
offset -= self.splitterView.frame.origin.x;
else
offset -= self.splitterView.frame.origin.y;
[self splitterView:self.splitterView moveByOffset:offset - floorf(SPLITTER_LENGTH/2)];
}

@synthesize controller1=_controller1;
@synthesize controller2=_controller2;
@synthesize splitterView=_splitterView;
@synthesize minimumView1Size=_minimumView1Size;
@synthesize minimumView2Size=_minimumView2Size;

@end
19 changes: 19 additions & 0 deletions CustomSplitView/AMResizableSplitterView.h
@@ -0,0 +1,19 @@
//
// AMResizableSplitterView.h
//
// Created by Mark Lilback on 7/19/12.
// Copyright (c) 2012 Agile Monks, LLC. All rights reserved.
//

#import <UIKit/UIKit.h>

@class AMResizableSplitterView;

@protocol AMResizableSplitterViewDelegate <NSObject>
-(void)splitterView:(AMResizableSplitterView*)splitterView moveByOffset:(CGFloat)offset;

@end

@interface AMResizableSplitterView : UIView
@property (nonatomic, weak) id<AMResizableSplitterViewDelegate> delegate;
@end

0 comments on commit f2de2d7

Please sign in to comment.