Skip to content
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
21 changes: 21 additions & 0 deletions YSLContainerViewController/Constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Constants.h
// YSLContainerViewControllerDemo
//
// Created by Gabriel Lupu on 03/07/16.
// Copyright © 2016 h.yamaguchi. All rights reserved.
//

#ifndef Constants_h
#define Constants_h

#define kYSLScrollMenuViewWidth 110
#define kYSLScrollMenuViewMargin 10
#define kYSLIndicatorHeight 1.5f
#define kYSLScrollMenuViewHeight 45.5f

#define menuHasShadow NO
#define categoryImageHeight 212.0f
#define middleTitleHeight 40.0f

#endif /* Constants_h */
6 changes: 4 additions & 2 deletions YSLContainerViewController/YSLContainerViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
@property (nonatomic, strong) UIColor *menuIndicatorColor;

- (id)initWithControllers:(NSArray *)controllers
topBarHeight:(CGFloat)topBarHeight
parentViewController:(UIViewController *)parentViewController;
parentViewController:(UIViewController *)parentViewController
heightOfView:(float) heightOfView;

- (void) goToSelectedViewController:(NSInteger)index;

@end
65 changes: 33 additions & 32 deletions YSLContainerViewController/YSLContainerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@

#import "YSLContainerViewController.h"
#import "YSLScrollMenuView.h"

static const CGFloat kYSLScrollMenuViewHeight = 40;
#import "Constants.h"

@interface YSLContainerViewController () <UIScrollViewDelegate, YSLScrollMenuViewDelegate>

@property (nonatomic, assign) CGFloat topBarHeight;
@property (nonatomic, assign) CGFloat heightOfView;
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, strong) YSLScrollMenuView *menuView;

Expand All @@ -22,23 +21,22 @@ @interface YSLContainerViewController () <UIScrollViewDelegate, YSLScrollMenuVie
@implementation YSLContainerViewController

- (id)initWithControllers:(NSArray *)controllers
topBarHeight:(CGFloat)topBarHeight
parentViewController:(UIViewController *)parentViewController
heightOfView:(float) heightOfView
{
self = [super init];
if (self) {
if (self)
{
[parentViewController addChildViewController:self];
[self didMoveToParentViewController:parentViewController];

_topBarHeight = topBarHeight;
_titles = [[NSMutableArray alloc] init];
_childControllers = [[NSMutableArray alloc] init];
_childControllers = [controllers mutableCopy];

_heightOfView = heightOfView;
NSMutableArray *titles = [NSMutableArray array];
for (UIViewController *vc in _childControllers) {
[titles addObject:[vc valueForKey:@"title"]];
for (UIViewController *vc in _childControllers)
{
[titles addObject:[[vc valueForKey:@"title"] uppercaseString]];
}
_titles = [titles mutableCopy];
}
Expand All @@ -58,28 +56,17 @@ - (void)viewDidLoad {

// ContentScrollview setup
_contentScrollView = [[UIScrollView alloc]init];
_contentScrollView.frame = CGRectMake(0,_topBarHeight + kYSLScrollMenuViewHeight, self.view.frame.size.width, self.view.frame.size.height - (_topBarHeight + kYSLScrollMenuViewHeight));
_contentScrollView.frame = CGRectMake(0, kYSLScrollMenuViewHeight, self.view.frame.size.width,_heightOfView);
_contentScrollView.backgroundColor = [UIColor clearColor];
_contentScrollView.pagingEnabled = YES;
_contentScrollView.delegate = self;
_contentScrollView.showsHorizontalScrollIndicator = NO;
_contentScrollView.scrollsToTop = NO;
_contentScrollView.tag = 1001;
[self.view addSubview:_contentScrollView];
_contentScrollView.contentSize = CGSizeMake(_contentScrollView.frame.size.width * self.childControllers.count, _contentScrollView.frame.size.height);

// ContentViewController setup
for (int i = 0; i < self.childControllers.count; i++) {
id obj = [self.childControllers objectAtIndex:i];
if ([obj isKindOfClass:[UIViewController class]]) {
UIViewController *controller = (UIViewController*)obj;
CGFloat scrollWidth = _contentScrollView.frame.size.width;
CGFloat scrollHeght = _contentScrollView.frame.size.height;
controller.view.frame = CGRectMake(i * scrollWidth, 0, scrollWidth, scrollHeght);
[_contentScrollView addSubview:controller.view];
}
}
// meunView
_menuView = [[YSLScrollMenuView alloc]initWithFrame:CGRectMake(0, _topBarHeight, self.view.frame.size.width, kYSLScrollMenuViewHeight)];
_contentScrollView.contentSize = CGSizeMake(_contentScrollView.frame.size.width * self.childControllers.count, _contentScrollView.frame.size.height - kYSLScrollMenuViewHeight);

_menuView = [[YSLScrollMenuView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kYSLScrollMenuViewHeight)];
_menuView.backgroundColor = [UIColor clearColor];
_menuView.delegate = self;
_menuView.viewbackgroudColor = self.menuBackGroudColor;
Expand All @@ -89,7 +76,7 @@ - (void)viewDidLoad {
_menuView.scrollView.scrollsToTop = NO;
[_menuView setItemTitleArray:self.titles];
[self.view addSubview:_menuView];
[_menuView setShadowView];
[_menuView setShadowView:menuHasShadow];

[self scrollMenuViewSelectedIndex:0];
}
Expand All @@ -102,9 +89,14 @@ - (void)setChildViewControllerWithCurrentIndex:(NSInteger)currentIndex
id obj = self.childControllers[i];
if ([obj isKindOfClass:[UIViewController class]]) {
UIViewController *controller = (UIViewController*)obj;
if (i == currentIndex) {
[controller willMoveToParentViewController:self];
if (i == currentIndex)
{
[self addChildViewController:controller];
[controller willMoveToParentViewController:self];
CGFloat scrollWidth = _contentScrollView.frame.size.width;
CGFloat scrollHeght = _contentScrollView.frame.size.height;
controller.view.frame = CGRectMake(i * scrollWidth, 0, scrollWidth, scrollHeght);
[self.contentScrollView addSubview:controller.view];
[controller didMoveToParentViewController:self];
} else {
[controller willMoveToParentViewController:self];
Expand All @@ -116,6 +108,13 @@ - (void)setChildViewControllerWithCurrentIndex:(NSInteger)currentIndex
}
#pragma mark -- YSLScrollMenuView Delegate

- (void) goToSelectedViewController:(NSInteger)index
{
[self scrollMenuViewSelectedIndex:index];
[_menuView moveIndicatorViewToCurrentIndex:kYSLScrollMenuViewMargin*(index+1) + kYSLScrollMenuViewWidth*index];
[_menuView.scrollView setContentOffset:CGPointMake(kYSLScrollMenuViewMargin*index + kYSLScrollMenuViewWidth*index, 0) animated:NO];
}

- (void)scrollMenuViewSelectedIndex:(NSInteger)index
{
[_contentScrollView setContentOffset:CGPointMake(index * _contentScrollView.frame.size.width, 0.) animated:YES];
Expand Down Expand Up @@ -151,7 +150,8 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView
nextItemOffsetX = (_menuView.scrollView.contentSize.width - _menuView.scrollView.frame.size.width) * targetIndex / (_menuView.itemViewArray.count - 1);
currentItemOffsetX = (_menuView.scrollView.contentSize.width - _menuView.scrollView.frame.size.width) * self.currentIndex / (_menuView.itemViewArray.count - 1);

if (targetIndex >= 0 && targetIndex < self.childControllers.count) {
if (targetIndex >= 0 && targetIndex < self.childControllers.count)
{
// MenuView Move
CGFloat indicatorUpdateRatio = ratio;
if (isToNextItem) {
Expand All @@ -162,7 +162,8 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView

indicatorUpdateRatio = indicatorUpdateRatio * 1;
[_menuView setIndicatorViewFrameWithRatio:indicatorUpdateRatio isNextItem:isToNextItem toIndex:self.currentIndex];
} else {
} else
{

CGPoint offset = _menuView.scrollView.contentOffset;
offset.x = currentItemOffsetX - (nextItemOffsetX - currentItemOffsetX) * ratio;
Expand Down
5 changes: 3 additions & 2 deletions YSLContainerViewController/YSLScrollMenuView.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

@interface YSLScrollMenuView : UIView

@property (nonatomic, strong) UIView *indicatorView;
@property (nonatomic, weak) id <YSLScrollMenuViewDelegate> delegate;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *itemTitleArray;
Expand All @@ -27,8 +28,8 @@
@property (nonatomic, strong) UIColor *itemSelectedTitleColor;
@property (nonatomic, strong) UIColor *itemIndicatorColor;

- (void)setShadowView;

- (void)setShadowView:(BOOL) hasShadow;
- (void)moveIndicatorViewToCurrentIndex:(NSInteger) index;
- (void)setIndicatorViewFrameWithRatio:(CGFloat)ratio isNextItem:(BOOL)isNextItem toIndex:(NSInteger)toIndex;

- (void)setItemTextColor:(UIColor *)itemTextColor
Expand Down
33 changes: 19 additions & 14 deletions YSLContainerViewController/YSLScrollMenuView.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@
//

#import "YSLScrollMenuView.h"

static const CGFloat kYSLScrollMenuViewWidth = 90;
static const CGFloat kYSLScrollMenuViewMargin = 10;
static const CGFloat kYSLIndicatorHeight = 3;
#import "Constants.h"

@interface YSLScrollMenuView ()


@property (nonatomic, strong) UIView *indicatorView;

@end

@implementation YSLScrollMenuView
Expand Down Expand Up @@ -102,12 +96,20 @@ - (void)setItemTitleArray:(NSArray *)itemTitleArray

// indicator
_indicatorView = [[UIView alloc]init];
_indicatorView.frame = CGRectMake(10, _scrollView.frame.size.height - kYSLIndicatorHeight, kYSLScrollMenuViewWidth, kYSLIndicatorHeight);
_indicatorView.frame = CGRectMake(0, _scrollView.frame.size.height - kYSLIndicatorHeight , kYSLScrollMenuViewWidth, kYSLIndicatorHeight);
_indicatorView.backgroundColor = self.itemIndicatorColor;

[_scrollView addSubview:_indicatorView];
}
}

- (void)moveIndicatorViewToCurrentIndex:(NSInteger) index
{
CGRect rect = _indicatorView.frame;
rect.origin.x = index;
_indicatorView.frame = rect;
}

#pragma mark -- public

- (void)setIndicatorViewFrameWithRatio:(CGFloat)ratio isNextItem:(BOOL)isNextItem toIndex:(NSInteger)toIndex
Expand All @@ -123,7 +125,6 @@ - (void)setIndicatorViewFrameWithRatio:(CGFloat)ratio isNextItem:(BOOL)isNextIte
return;
}
_indicatorView.frame = CGRectMake(indicatorX, _scrollView.frame.size.height - kYSLIndicatorHeight, kYSLScrollMenuViewWidth, kYSLIndicatorHeight);
// NSLog(@"retio : %f",_indicatorView.frame.origin.x);
}

- (void)setItemTextColor:(UIColor *)itemTextColor
Expand Down Expand Up @@ -154,12 +155,16 @@ - (void)setItemTextColor:(UIColor *)itemTextColor
#pragma mark -- private

// menu shadow
- (void)setShadowView
- (void)setShadowView:(BOOL) hasShadow
{
UIView *view = [[UIView alloc]init];
view.frame = CGRectMake(0, self.frame.size.height - 0.5, CGRectGetWidth(self.frame), 0.5);
view.backgroundColor = [UIColor lightGrayColor];
[self addSubview:view];
if (hasShadow)
{
UIView *view = [[UIView alloc]init];
view.frame = CGRectMake(0, self.frame.size.height - 0.5, CGRectGetWidth(self.frame), 0.5);
view.backgroundColor = [UIColor lightGrayColor];
[self addSubview:view];

}
}

- (void)layoutSubviews
Expand Down
Loading