Skip to content

Commit

Permalink
added ViewController class for accordionview
Browse files Browse the repository at this point in the history
  • Loading branch information
jrtaal committed Mar 19, 2014
1 parent 133911b commit af2a8a4
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 7 deletions.
6 changes: 4 additions & 2 deletions AccordionView.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
@class AccordionView;
@protocol AccordionViewDelegate <NSObject>
@optional
- (void)accordion:(AccordionView *)accordion didChangeSelection:(NSIndexSet *)selection;
- (void)accordion:(AccordionView *)accordion willChangeFromSelection:(NSIndexSet *)fromSelection toSelection:(NSIndexSet*) toSelection;
- (void)accordion:(AccordionView *)accordion didChangeFromSelection:(NSIndexSet *)fromSelection toSelection:(NSIndexSet*) toSelection;
@end

@interface AccordionView : UIView <UIScrollViewDelegate> {
Expand All @@ -48,4 +49,5 @@
@property (nonatomic, assign) BOOL startsClosed;
@property (nonatomic, assign) BOOL allowsEmptySelection;

@end

@end
47 changes: 42 additions & 5 deletions AccordionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,37 @@ @implementation AccordionView
@synthesize selectedIndex, isHorizontal, animationDuration, animationCurve;
@synthesize allowsMultipleSelection, selectionIndexes, delegate, startsClosed, allowsEmptySelection;

#pragma mark UIView methods

-(void)initSetup {

views = [NSMutableArray new];
headers = [NSMutableArray new];
originalSizes = [NSMutableArray new];

self.backgroundColor = [UIColor clearColor];


self.userInteractionEnabled = YES;

animationDuration = 0.3;
animationCurve = UIViewAnimationCurveEaseIn;

self.autoresizesSubviews = NO;

self.allowsMultipleSelection = NO;
}

-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initSetup];
}
return self;
}



- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
Expand Down Expand Up @@ -82,8 +113,8 @@ - (void)addHeader:(UIControl *)aHeader withView:(id)aView {
[aView setFrame:frame];
}

[scrollView addSubview:aView];
[scrollView addSubview:aHeader];
[self addSubview:aView];
[self addSubview:aHeader];

if ([aHeader respondsToSelector:@selector(addTarget:action:forControlEvents:)]) {
[aHeader setTag:[headers count] - 1];
Expand Down Expand Up @@ -138,12 +169,15 @@ - (void)setSelectionIndexes:(NSIndexSet *)aSelectionIndexes {

[cleanIndexes addIndex:idx];
}];

if ([delegate respondsToSelector:@selector(accordion:willChangeFromSelection:toSelection:)]) {
[delegate accordion:self willChangeFromSelection:selectionIndexes toSelection:cleanIndexes];
}
NSIndexSet * oldIndexes = selectionIndexes;
selectionIndexes = cleanIndexes;
[self setNeedsLayout];

if ([delegate respondsToSelector:@selector(accordion:didChangeSelection:)]) {
[delegate accordion:self didChangeSelection:self.selectionIndexes];
if ([delegate respondsToSelector:@selector(accordion:didChangeFromSelection:toSelection:)]) {
[delegate accordion:self didChangeFromSelection:oldIndexes toSelection: self.selectionIndexes ];
}
}

Expand All @@ -155,6 +189,8 @@ - (NSInteger)selectedIndex {
return [selectionIndexes firstIndex];
}


#pragma mark Display
- (void)setOriginalSize:(CGSize)size forIndex:(NSUInteger)index {
if (index >= [views count]) return;

Expand Down Expand Up @@ -291,4 +327,5 @@ - (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
}
}


@end
17 changes: 17 additions & 0 deletions AccordionViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// AccordionViewController.h
// Vidacle
//
// Created by Jacco Taal on 18-02-13.
// Copyright (c) 2013 Jacco Taal. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "MyAccordionView.h"

@interface AccordionViewController : UIViewController <AccordionViewDelegate>

@property MyAccordionView * accordionView;
@property (strong, atomic) NSArray * viewControllers;

@end
139 changes: 139 additions & 0 deletions AccordionViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
//
// AccordionViewController.m
// Vidacle
//
// Created by Jacco Taal on 18-02-13.
// Copyright (c) 2013 Jacco Taal. All rights reserved.
//

#import "AccordionViewController.h"

//#import <UIGlossyButton.h>
#import <QuartzCore/QuartzCore.h>

#import "LSAccordionButton.h"
#import "LSButton.h"

#define kAccordionButtonHeight 40.0


@interface AccordionViewController () {
NSArray __strong * _myViewControllers;
}

@end

@implementation AccordionViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

if ( [self.view class] != [MyAccordionView class])
self.view = (UIView*) [[MyAccordionView alloc] initWithFrame:self.view.frame];
self.accordionView = (MyAccordionView *)self.view;
self.accordionView.delegate = self;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (BOOL)shouldAutorotate {
return false;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}

- (NSArray *)viewControllers {
return self->_myViewControllers;
}

- (void)setViewControllers:(NSArray *)viewControllers {
NSArray * oldVCs = self->_myViewControllers;
self->_myViewControllers = [[NSArray alloc] initWithArray: viewControllers];

[oldVCs enumerateObjectsUsingBlock:^(UIViewController * child, NSUInteger idx, BOOL *stop) {
if ([viewControllers indexOfObject:child] == NSNotFound) {

if ( [self.childViewControllers indexOfObject:child] != NSNotFound) {
[child removeFromParentViewController];
}
}
//if ([oldVCs indexOfObject:child] != NSNotFound)
[self.accordionView removeSectionWithView: child.view ];
}];
NSUInteger N = viewControllers.count;

//NSLog(@"accordionview bounds %@", NSStringFromCGRect(self.accordionView.bounds));
[viewControllers enumerateObjectsUsingBlock:^(UIViewController * vc, NSUInteger idx, BOOL *stop) {
//UIButton *header = [UIButton buttonWithType:UIButtonTypeCustom ];
//header.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.3];

//LSAccordionButton *header = [LSAccordionButton buttonWithType:UIButtonTypeCustom ];
LSButton *header = [LSButton buttonWithType:UIButtonTypeCustom ];
//header.buttonCornerRadius = 0;
//header.buttonBorderWidth =0.0;
//header.layer.borderColor = [UIColor colorWithWhite:0.3 alpha:1.0].CGColor;
header.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds) , kAccordionButtonHeight);
[header setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[header setTitle:(vc.tabBarItem.title ? vc.tabBarItem.title : vc.title) forState:UIControlStateNormal];
//header.tintColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.2];

vc.view.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), //vc.view.bounds.size.width,
self.accordionView.bounds.size.height-N*kAccordionButtonHeight);
//NSLog(@"subview %d bounds %@", idx, NSStringFromCGRect(vc.view.bounds));
[self.accordionView addHeader:header withView:vc.view];
}];
if (viewControllers.count>0)
[self addChildViewController: viewControllers[0]];

[self.accordionView setNeedsLayout];
[self.accordionView setAllowsMultipleSelection:NO];
}


#pragma AccordionViewDelegate
- (void)accordion:(MyAccordionView *)accordion willChangeFromSelection:(NSIndexSet *)fromSelection toSelection:(NSIndexSet *)toSelection {
//NSLog(@"Index: %@ -> %@", fromSelection, toSelection);
for ( int i=0; i<self.viewControllers.count; i++) {
if ( [toSelection containsIndex:i] && ! [fromSelection containsIndex:i]) {
[self addChildViewController: self.viewControllers[i]];
[self.viewControllers[i] beginAppearanceTransition:true animated:true];
};
if ( ![toSelection containsIndex:i] && [fromSelection containsIndex:i]) {
[self.viewControllers[i] willMoveToParentViewController:nil];
[self.viewControllers[i] beginAppearanceTransition:false animated:true];
}
}

}
- (void)accordion:(MyAccordionView *)accordion didChangeFromSelection:(NSIndexSet *)fromSelection toSelection:(NSIndexSet *)toSelection {
//NSLog(@"Index: %@ -> %@", fromSelection, toSelection);
for ( int i=0; i<self.viewControllers.count; i++) {
if ( [toSelection containsIndex:i] && ! [fromSelection containsIndex:i]) {
[self.viewControllers[i] endAppearanceTransition];
[self.viewControllers[i] didMoveToParentViewController:self];
};
if ( ![toSelection containsIndex:i] && [fromSelection containsIndex:i]) {
if ( ((UIViewController*) self.viewControllers[i]).parentViewController)
[self.viewControllers[i] endAppearanceTransition];
[self.viewControllers[i] removeFromParentViewController];
}
}
}

@end

0 comments on commit af2a8a4

Please sign in to comment.