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
8 changes: 4 additions & 4 deletions Class/DYSlideView.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@required
- (NSInteger)DY_numberOfViewControllersInSlideView;
- (NSString *)DY_titleForViewControllerAtIndex:(NSInteger)index;
- (NSAttributedString *)DY_attributedtitleForViewControllerAtIndex:(NSInteger)index;
- (UIViewController *)DY_viewControllerAtIndex:(NSInteger)index;

@optional
Expand All @@ -27,6 +27,7 @@
@property (nonatomic, weak) id <DYSlideViewDelegate> delegate;

@property (strong, nonatomic) NSNumber *indexForDefaultItem;
@property (nonatomic, assign, readonly) NSInteger currentSelectedIndex;

@property (strong, nonatomic) UIColor *slideBarColor;
@property (nonatomic) CGFloat slideBarHeight;
Expand All @@ -35,12 +36,11 @@
@property (nonatomic) CGFloat sliderHeight;
@property (nonatomic) CGFloat sliderScale;

@property (strong, nonatomic) UIColor *buttonNormalColor;
@property (strong, nonatomic) UIColor *buttonSelectedColor;
@property (strong, nonatomic) UIFont *buttonTitleFont;
@property (nonatomic, strong) UIColor *separatorColor;

@property (nonatomic) BOOL scrollViewBounces;
@property (nonatomic) BOOL scrollEnabled;

- (void)selectButtonWithIndex:(NSInteger)newIndex;

@end
261 changes: 166 additions & 95 deletions Class/DYSlideView.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@

#import "DYSlideView.h"

@interface DYSlideView () <UIScrollViewDelegate> {
@interface DYSlideView () <UIScrollViewDelegate>
{
NSInteger _numberOfViewControllers;
UIView * _slideBar;
UIView * _slider;
NSMutableArray *_slideBarButtons;
UIView *_separator;
NSMutableArray *_slideBarButtons, *_addedControllers;
UIButton * _selectedButton;
NSInteger _currentBtnIndex;
UIScrollView *_scrollView;
BOOL _initializating;
}

- (void)_updateSelectedButton:(UIButton *)button;

@end

@implementation DYSlideView

- (void)dealloc{
- (void)dealloc
{
_separator = nil;
_separatorColor = nil;
_scrollView = nil;
_selectedButton = nil;
_slideBarButtons = nil;
Expand All @@ -31,178 +38,242 @@ - (void)dealloc{
_indexForDefaultItem = nil;
_slideBarColor = nil;
_sliderColor = nil;
_buttonNormalColor = nil;
_buttonSelectedColor = nil;
_buttonTitleFont = nil;
}

- (id)init {
- (id)init
{
self = [super init];
if (self) {
if (self)
{
[self setupForInitialization];
}
return self;
}

- (id)initWithFrame:(CGRect)frame{
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
if (self)
{
[self setupForInitialization];
}
return self;
}

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

- (void)setupForInitialization {
- (void)setupForInitialization
{
_initializating = YES;
_numberOfViewControllers = NSNotFound;
_slideBarColor = [UIColor lightGrayColor];
_sliderColor = [UIColor redColor];;
_buttonNormalColor = [UIColor whiteColor];
_buttonSelectedColor = [UIColor blackColor];
_sliderScale = 1.f;
_slideBarHeight = 50.f;
_sliderHeight = 4.f;
_buttonTitleFont = [UIFont systemFontOfSize:16.f];
_scrollViewBounces = YES;
_scrollEnabled = YES;
}

- (void)layoutSubviews {
- (void)layoutSubviews
{
[super layoutSubviews];
if (!_slideBarButtons) {

if ( _initializating )
{
_numberOfViewControllers = [self.delegate DY_numberOfViewControllersInSlideView];
[self addSlideBar];
[self addScrollView];
if (_indexForDefaultItem && [_slideBarButtons count] > [_indexForDefaultItem integerValue]) {
UIButton * button = [_slideBarButtons objectAtIndex:[_indexForDefaultItem integerValue]];
[self updateSelectedButton:button];
[_scrollView setContentOffset:CGPointMake(self.bounds.size.width * button.tag, 0) animated:NO];
} else {
[self updateSelectedButton:nil];
}
[_selectedButton setTitleColor:_buttonSelectedColor forState:UIControlStateNormal];
}
}

- (void)addSlideBar {
if (!_slideBar) {
_slideBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, _slideBarHeight)];
_slideBar = [[UIView alloc] initWithFrame:CGRectZero];
[_slideBar setBackgroundColor:_slideBarColor];
[self addSubview:_slideBar];
}
[self addButtons];
[self addSlider];
}

- (void)addButtons {
_slideBarButtons = [NSMutableArray array];
for (NSInteger i = 0; i < _numberOfViewControllers; i++) {

[_slideBar setFrame:CGRectMake(0, 0, self.bounds.size.width, _slideBarHeight)];

if ( _initializating )
{
_slideBarButtons = [NSMutableArray array];
for (NSInteger i = 0; i < _numberOfViewControllers; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectZero];
[button setTag:i];
[button.titleLabel setTextAlignment:NSTextAlignmentCenter];
[button setAttributedTitle:[_delegate DY_attributedtitleForViewControllerAtIndex:i] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[_slideBarButtons addObject:button];
[_slideBar addSubview:button];
}
}

for (NSInteger i = 0; i < _numberOfViewControllers; i++)
{
UIButton *button = _slideBarButtons[i];
CGFloat width = self.bounds.size.width / _numberOfViewControllers;

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake( width * i, 5, width, 35)];
[button setTag:i];
[button.titleLabel setTextAlignment:NSTextAlignmentCenter];
[button.titleLabel setFont:_buttonTitleFont];
[button setTitleColor:_buttonNormalColor forState:UIControlStateNormal];
[button setTitle:[_delegate DY_titleForViewControllerAtIndex:i]?:@"" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

[_slideBarButtons addObject:button];
[_slideBar addSubview:button];
}
}

- (void)addSlider {
if (!_slider) {
CGFloat buttonWidth = self.bounds.size.width / _numberOfViewControllers;
CGFloat sliderWidth = buttonWidth * _sliderScale;

_slider = [[UIView alloc] initWithFrame:CGRectMake((buttonWidth - sliderWidth)/2, _slideBarHeight-_sliderHeight, sliderWidth, _sliderHeight)];
[_slider setBackgroundColor:_sliderColor];

if ( _initializating )
{
if ( _separatorColor )
{
_separator = [[UIView alloc] initWithFrame:CGRectZero];
[_separator setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin];
[_separator setBackgroundColor:_separatorColor];
[_slideBar addSubview:_separator];
}
}
[_separator setFrame:CGRectMake(0, _slideBarHeight - _sliderHeight + 1, _slideBar.bounds.size.width, 1.0)];

CGFloat buttonWidth = self.bounds.size.width / _numberOfViewControllers;
CGFloat sliderWidth = buttonWidth * _sliderScale;

if ( _initializating )
{
_slider = [[UIView alloc] initWithFrame:CGRectZero];
[_slideBar addSubview:_slider];
[_slider setBackgroundColor:_sliderColor];
}
}

- (void)addScrollView {
if (!_scrollView) {
[_slider setFrame:CGRectMake((buttonWidth - sliderWidth)/2, _slideBarHeight-_sliderHeight-1, sliderWidth, _sliderHeight)];

if ( _initializating )
{
_addedControllers = [[NSMutableArray alloc] init];
_scrollView = [[UIScrollView alloc] init];
[_scrollView setFrame:CGRectMake(0, _slideBarHeight, self.bounds.size.width, self.bounds.size.height - _slideBarHeight)];
[_scrollView setFrame:CGRectZero];
[_scrollView setDirectionalLockEnabled:YES];
[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(_scrollView.bounds.size.width * _numberOfViewControllers, 0)];
[_scrollView setShowsHorizontalScrollIndicator:NO];
[_scrollView setDelegate:self];
[_scrollView setBounces:_scrollViewBounces];
[_scrollView setScrollEnabled:_scrollEnabled];

[self addSubview:_scrollView];

for (NSInteger i = 0; i < _numberOfViewControllers; i++ ) {
for (NSInteger i = 0; i < _numberOfViewControllers; i++ )
{
UIViewController * vc = [_delegate DY_viewControllerAtIndex:i];
if (vc) {
CGRect rect = _scrollView.bounds;
rect.origin.x = self.bounds.size.width * i;
[vc.view setFrame:rect];
[_scrollView addSubview:vc.view];
}
[_addedControllers addObject:vc ? vc : [NSNull null]];
[_scrollView addSubview:vc.view];
}
}
[_scrollView setFrame:CGRectMake(0, _slideBarHeight, self.bounds.size.width, self.bounds.size.height - _slideBarHeight)];
[_scrollView setContentSize:CGSizeMake(_scrollView.bounds.size.width * _numberOfViewControllers, 0)];

for (NSInteger i = 0; i < _numberOfViewControllers; i++ )
{
if ( [_addedControllers[i] isEqual:[NSNull null]] ) continue;
UIViewController *vc = _addedControllers[i];

CGRect rect = _scrollView.bounds;
rect.origin.x = self.bounds.size.width * i;
[vc.view setFrame:rect];
}

if ( _initializating )
{
if (_indexForDefaultItem && [_slideBarButtons count] > [_indexForDefaultItem integerValue])
{
UIButton * button = [_slideBarButtons objectAtIndex:[_indexForDefaultItem integerValue]];
[self _updateSelectedButton:button];
[_scrollView setContentOffset:CGPointMake(self.bounds.size.width * button.tag, 0) animated:NO];
}
else
{
[self _updateSelectedButton:nil];
}

[[_slideBarButtons objectAtIndex:_currentSelectedIndex] setAttributedTitle:[_delegate DY_attributedtitleForViewControllerAtIndex:_currentSelectedIndex] forState:UIControlStateNormal];
}
// 04/11/2016 - inserito questo per tentare di gestire il refresh quando ruoti, dopo l'init
else if ( _selectedButton )
{
[self _updateSelectedButton:_selectedButton];
[_scrollView setContentOffset:CGPointMake(self.bounds.size.width * _selectedButton.tag, 0) animated:NO];
}
_initializating = NO;
}

- (void)buttonClicked:(UIButton *)button {
[self updateSelectedButton:button];
- (void)buttonClicked:(UIButton *)button
{
[self _updateSelectedButton:button];
[_scrollView setContentOffset:CGPointMake(self.bounds.size.width * button.tag, 0) animated:YES];
}

- (void)updateSelectedButton:(UIButton *)button{
if (button == nil && [_slideBarButtons count] > 0) {
- (void)_updateSelectedButton:(UIButton *)button
{
if (button == nil && [_slideBarButtons count] > 0)
{
button = [_slideBarButtons firstObject];
}
if (!(_selectedButton && [_slideBarButtons indexOfObject:_selectedButton] == [button tag])) {
if (!(_selectedButton && [_slideBarButtons indexOfObject:_selectedButton] == [button tag]))
{
_selectedButton = button;
if (_delegate && [_delegate respondsToSelector:@selector(DY_didSelectButtonAtIndex:)]) {
if (_delegate && [_delegate respondsToSelector:@selector(DY_didSelectButtonAtIndex:)])
{
[self.delegate DY_didSelectButtonAtIndex:[_selectedButton tag]];
}
}
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
- (void)selectButtonWithIndex:(NSInteger)newIndex
{
if ( _initializating ) {
_indexForDefaultItem = [NSNumber numberWithInteger:newIndex];
}
else
{
for ( UIButton *mButton in _slideBarButtons ) {
if ( mButton.tag == newIndex ) {

[self buttonClicked:mButton];
// _selectedButton = mButton;
// [self setNeedsLayout];
// [self layoutIfNeeded];
return;
}
}
}
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint contentOffset = scrollView.contentOffset;

CGFloat buttonWidth = self.bounds.size.width / _numberOfViewControllers;
CGFloat sliderWidth = buttonWidth * _sliderScale;
CGRect rect = _slider.frame;
rect.origin.x = (contentOffset.x/self.bounds.size.width) * buttonWidth + (buttonWidth - sliderWidth)/2;
[_slider setFrame:rect];
[_slider setFrame:CGRectMake(rect.origin.x, _slideBarHeight-_sliderHeight-1, sliderWidth, _sliderHeight)];


CGFloat ratio = contentOffset.x/self.bounds.size.width;
NSInteger index = (NSInteger)ratio;
if (ratio - (NSInteger)ratio >= 0.5) {
if (ratio - (NSInteger)ratio >= 0.5)
{
index += 1;
}

if (index < _numberOfViewControllers && _currentBtnIndex != index) {
_currentBtnIndex = index;
for (UIButton *button in _slideBarButtons) {
if ([_slideBarButtons indexOfObject:button] == _currentBtnIndex) {
[[_slideBarButtons objectAtIndex:_currentBtnIndex] setTitleColor:_buttonSelectedColor forState:UIControlStateNormal];
} else {
[button setTitleColor:_buttonNormalColor forState:UIControlStateNormal];
}
if (index < _numberOfViewControllers && _currentSelectedIndex != index)
{
_currentSelectedIndex = index;
for ( NSInteger b = 0; b < [_slideBarButtons count]; b++)
{
[_slideBarButtons[b] setAttributedTitle:[_delegate DY_attributedtitleForViewControllerAtIndex:b] forState:UIControlStateNormal];
}
}
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[self updateSelectedButton:[_slideBarButtons objectAtIndex:_currentBtnIndex]];
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self _updateSelectedButton:[_slideBarButtons objectAtIndex:_currentSelectedIndex]];
}

@end
Loading