diff --git a/Class/DYSlideView.h b/Class/DYSlideView.h index 1e05ccd..abd6d9b 100644 --- a/Class/DYSlideView.h +++ b/Class/DYSlideView.h @@ -13,7 +13,7 @@ @required - (NSInteger)DY_numberOfViewControllersInSlideView; -- (NSString *)DY_titleForViewControllerAtIndex:(NSInteger)index; +- (NSAttributedString *)DY_attributedtitleForViewControllerAtIndex:(NSInteger)index; - (UIViewController *)DY_viewControllerAtIndex:(NSInteger)index; @optional @@ -27,6 +27,7 @@ @property (nonatomic, weak) id delegate; @property (strong, nonatomic) NSNumber *indexForDefaultItem; +@property (nonatomic, assign, readonly) NSInteger currentSelectedIndex; @property (strong, nonatomic) UIColor *slideBarColor; @property (nonatomic) CGFloat slideBarHeight; @@ -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 diff --git a/Class/DYSlideView.m b/Class/DYSlideView.m index 3008abc..33bfe77 100644 --- a/Class/DYSlideView.m +++ b/Class/DYSlideView.m @@ -8,21 +8,28 @@ #import "DYSlideView.h" -@interface DYSlideView () { +@interface DYSlideView () +{ 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; @@ -31,113 +38,117 @@ - (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]; @@ -145,64 +156,124 @@ - (void)addScrollView { [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 diff --git a/DYSlideView.podspec b/DYSlideView.podspec index 15bdb24..c7e3bde 100644 --- a/DYSlideView.podspec +++ b/DYSlideView.podspec @@ -1,14 +1,14 @@ Pod::Spec.new do |s| s.name = 'DYSlideView' - s.version = '0.0.9' + s.version = '0.0.16' s.summary = 'An iOS tabbed slide view.' - s.homepage = 'https://github.com/Dwarven/DYSlideView' + s.homepage = 'https://github.com/tarokker/DYSlideView' s.ios.deployment_target = '7.0' s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { 'Dwarven' => 'prison.yang@gmail.com' } s.social_media_url = "https://twitter.com/DwarvenYang" - s.source = { :git => 'https://github.com/Dwarven/DYSlideView.git', :tag => s.version } + s.source = { :git => 'https://github.com/tarokker/DYSlideView.git', :tag => s.version } s.source_files = 'Class/*.{h,m}' end diff --git a/DYSlideView.xcodeproj/project.pbxproj b/DYSlideView.xcodeproj/project.pbxproj index 6c82db8..9b8e0f0 100644 --- a/DYSlideView.xcodeproj/project.pbxproj +++ b/DYSlideView.xcodeproj/project.pbxproj @@ -420,6 +420,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.Dwarven.DYSlideView; PRODUCT_NAME = "$(TARGET_NAME)"; + TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; }; @@ -432,6 +433,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.Dwarven.DYSlideView; PRODUCT_NAME = "$(TARGET_NAME)"; + TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; }; diff --git a/DYSlideView/ViewController.m b/DYSlideView/ViewController.m index 38bc8d6..d60f08a 100644 --- a/DYSlideView/ViewController.m +++ b/DYSlideView/ViewController.m @@ -10,7 +10,9 @@ #import "DYSlideView.h" @interface ViewController () - +{ + DYSlideView *slideView; +} @end @implementation ViewController @@ -18,19 +20,17 @@ @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; - DYSlideView *slideView = [[DYSlideView alloc] init]; + slideView = [[DYSlideView alloc] init]; + [slideView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight]; [slideView setFrame:self.view.bounds]; + slideView.separatorColor = [UIColor yellowColor]; slideView.slideBarColor = [UIColor lightGrayColor]; slideView.slideBarHeight = 50; slideView.sliderColor = [UIColor redColor]; slideView.sliderHeight = 2; - slideView.sliderScale = 0.6; - - slideView.buttonNormalColor = [UIColor yellowColor]; - slideView.buttonSelectedColor = [UIColor blackColor]; - slideView.buttonTitleFont = [UIFont boldSystemFontOfSize:16.f]; +// slideView.sliderScale = 0.6; slideView.scrollViewBounces = YES; @@ -38,23 +38,30 @@ - (void)viewDidLoad { slideView.delegate = self; [self.view addSubview:slideView]; - } - (NSInteger)DY_numberOfViewControllersInSlideView { return 4; } -- (NSString *)DY_titleForViewControllerAtIndex:(NSInteger)index{ - return [NSString stringWithFormat:@"Tab%li",index]; +- (NSAttributedString *)DY_attributedtitleForViewControllerAtIndex:(NSInteger)index +{ + return [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"Tab%li",index] attributes:nil]; } - (UIViewController *)DY_viewControllerAtIndex:(NSInteger)index { switch (index) { case 0: - return [self vcWithBackgroundColor:[UIColor whiteColor]]; - break; - + { + UIViewController *newctl = [self vcWithBackgroundColor:[UIColor whiteColor]]; + UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; + [btn setBackgroundColor:[UIColor blueColor]]; + [btn setTitle:@"Go 2" forState:UIControlStateNormal]; + [btn addTarget:self action:@selector(didClickButtonGo2:) forControlEvents:UIControlEventTouchUpInside]; + [btn setFrame:CGRectMake(50, 50, 150, 50)]; + [newctl.view addSubview:btn]; + return newctl; + } case 1: return [self vcWithBackgroundColor:[UIColor grayColor]]; break; @@ -83,4 +90,8 @@ - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } +- (void)didClickButtonGo2:(id)sender +{ + [slideView selectButtonWithIndex:1]; +} @end