iOS_第3方类库_側滑选项卡SlideSwitchView
1、在主控制器中创建一个【SlideSwitchView】的对象实例,并用成员变量记住,如_slideSwitchView,并加入到self.view
2、设置【_slideSwitchView】的4个属性:
tabItemNormalColor:选项卡正常时的颜色
tabItemSelectedColor:选项卡选中时的颜色
shadowImage:盖在选项卡上面的一张图片
slideSwitchViewDelegate:设置代理为当前控制器,用于监听滑动时,切换控制器
3、调用【_slideSwitchView】的buildUI方法
4、遵守协议,并实现代理方法;如:
顶部选项卡的个数
每一个选项卡相应的控制器
//
// SlideSwitchView.h
// 多控制器选项卡切换效果
//
// Created by beyond on 14-10-18.
// Copyright (c) 2014年 beyond. All rights reserved.
// #import <UIKit/UIKit.h> @protocol SlideSwitchViewDelegate;
@interface SlideSwitchView : UIView<UIScrollViewDelegate>
{
UIScrollView *_rootScrollView; //主视图
UIScrollView *_topScrollView; //顶部页签视图
UIImageView *_line; CGFloat _userContentOffsetX;
BOOL _isLeftScroll; //是否左滑动
BOOL _isRootScroll; //是否主视图滑动
BOOL _isBuildUI; //是否建立了ui NSInteger _userSelectedChannelID; //点击button选择名字ID UIImageView *_shadowImageView;
UIImage *_shadowImage; UIColor *_tabItemNormalColor; //正常时tab文字颜色
UIColor *_tabItemSelectedColor; //选中时tab文字颜色
UIImage *_tabItemNormalBackgroundImage; //正常时tab的背景
UIImage *_tabItemSelectedBackgroundImage; //选中时tab的背景
NSMutableArray *_viewArray; //主视图的子视图数组 UIButton *_rigthSideButton; //右側button __weak id<SlideSwitchViewDelegate> _slideSwitchViewDelegate;
} @property (nonatomic, strong) IBOutlet UIScrollView *rootScrollView;
@property (nonatomic, strong) IBOutlet UIScrollView *topScrollView;
@property (nonatomic, assign) CGFloat userContentOffsetX;
@property (nonatomic, assign) NSInteger userSelectedChannelID;
@property (nonatomic, assign) NSInteger scrollViewSelectedChannelID;
@property (nonatomic, weak) IBOutlet id<SlideSwitchViewDelegate> slideSwitchViewDelegate;
@property (nonatomic, strong) UIColor *tabItemNormalColor;
@property (nonatomic, strong) UIColor *tabItemSelectedColor;
@property (nonatomic, strong) UIImage *tabItemNormalBackgroundImage;
@property (nonatomic, strong) UIImage *tabItemSelectedBackgroundImage;
@property (nonatomic, strong) UIImage *shadowImage;
@property (nonatomic, strong) NSMutableArray *viewArray;
@property (nonatomic, strong) IBOutlet UIButton *rigthSideButton; - (void)setHideTopView:(BOOL)hide; - (void) setScrollViewSelectedWithIndex:(NSInteger)index; /*!
* @method 创建子视图UI
* @abstract
* @discussion
* @param
* @result
*/
- (void)buildUI; /*!
* @method 通过16进制计算颜色
* @abstract
* @discussion
* @param 16机制
* @result 颜色对象
*/
+ (UIColor *)colorFromHexRGB:(NSString *)inColorString; @end @protocol SlideSwitchViewDelegate <NSObject> @required /*!
* @method 顶部tab个数
* @abstract
* @discussion
* @param 本控件
* @result tab个数
*/
- (NSUInteger)numberOfTab:(SlideSwitchView *)view; /*!
* @method 每一个tab所属的viewController
* @abstract
* @discussion
* @param tab索引
* @result viewController
*/
- (UIViewController *)slideSwitchView:(SlideSwitchView *)view viewOfTab:(NSUInteger)number; @optional /*!
* @method 滑动左边界时传递手势
* @abstract
* @discussion
* @param 手势
* @result
*/
- (void)slideSwitchView:(SlideSwitchView *)view panLeftEdge:(UIPanGestureRecognizer*) panParam; /*!
* @method 滑动右边界时传递手势
* @abstract
* @discussion
* @param 手势
* @result
*/
- (void)slideSwitchView:(SlideSwitchView *)view panRightEdge:(UIPanGestureRecognizer*) panParam; /*!
* @method 点击tab
* @abstract
* @discussion
* @param tab索引
* @result
*/
- (void)slideSwitchView:(SlideSwitchView *)view didselectTab:(NSUInteger)number; - (void)scrollViewDidScroll:(SlideSwitchView *)view didselectTab:(NSUInteger)number; @end
<span style="color:#000000;">//
// SlideSwitchView.m
// 多控制器选项卡切换效果
//
// Created by beyond on 14-10-18.
// Copyright (c) 2014年 beyond. All rights reserved.
// #import "SlideSwitchView.h" #define UIColorFromIntRBG(RED, GREEN, BLUE) [UIColor colorWithRed:RED/255.0 green:GREEN/255.0 blue:BLUE/255.0 alpha:1.0] @interface SlideSwitchView ()
{
BOOL doSomething;
// 開始滚动时的offset的x
int startOffsetX;
}
@end
static const CGFloat kHeightOfTopScrollView = 44.0f;
static const CGFloat kWidthOfButtonMargin = 16.0f;
static const CGFloat kFontSizeOfTabButton = 17.0f;
static const NSUInteger kTagOfRightSideButton = 999; @implementation SlideSwitchView #pragma mark - 初始化參数 - (void)initValues
{ //创建顶部可滑动的tab
_topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, kHeightOfTopScrollView)];
_topScrollView.delegate = self;
_topScrollView.backgroundColor = UIColorFromIntRBG(240, 240, 240);
_topScrollView.pagingEnabled = NO;
_topScrollView.showsHorizontalScrollIndicator = NO;
_topScrollView.showsVerticalScrollIndicator = NO;
_topScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_topScrollView.scrollEnabled = NO; [self addSubview:_topScrollView];
_userSelectedChannelID = 100; _line = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_topScrollView.frame), self.bounds.size.width, 1)];
UIColor *fromColor = UIColorFromIntRBG(212, 212, 212);
// 工具方法,从颜色 生成 图片
UIImage *image = [self imageWithColor:fromColor size:_line.frame.size];
[_line setImage:image];
[self addSubview:_line]; //创建主滚动视图
_rootScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kHeightOfTopScrollView + 1, self.bounds.size.width, self.bounds.size.height - kHeightOfTopScrollView)];
_rootScrollView.delegate = self;
_rootScrollView.pagingEnabled = YES;
_rootScrollView.userInteractionEnabled = YES;
_rootScrollView.bounces = NO;
_rootScrollView.showsHorizontalScrollIndicator = NO;
_rootScrollView.showsVerticalScrollIndicator = NO;
_rootScrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
_userContentOffsetX = 0;
[_rootScrollView.panGestureRecognizer addTarget:self action:@selector(scrollHandlePan:)];
[self addSubview:_rootScrollView]; _viewArray = [[NSMutableArray alloc] init]; _isBuildUI = NO; } - (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self initValues];
}
return self;
} - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initValues];
}
return self;
} #pragma mark getter/setter - (void)setRigthSideButton:(UIButton *)rigthSideButton
{
UIButton *button = (UIButton *)[self viewWithTag:kTagOfRightSideButton];
[button removeFromSuperview];
rigthSideButton.tag = kTagOfRightSideButton;
_rigthSideButton = rigthSideButton;
[self addSubview:_rigthSideButton]; } #pragma mark - 创建控件 //当横竖屏切换时可通过此方法调整布局
- (void)layoutSubviews
{
//创建完子视图UI才须要调整布局
if (_isBuildUI) { //更新主视图的总宽度
_rootScrollView.contentSize = CGSizeMake(self.bounds.size.width * [_viewArray count], 0); //更新主视图各个子视图的宽度
for (int i = 0; i < [_viewArray count]; i++) {
UIViewController *listVC = _viewArray[i];
listVC.view.frame = CGRectMake(0+_rootScrollView.bounds.size.width*i, 0,
_rootScrollView.bounds.size.width, _rootScrollView.bounds.size.height);
} //滚动到选中的视图
[_rootScrollView setContentOffset:CGPointMake((_userSelectedChannelID - 100)*self.bounds.size.width, 0) animated:NO]; // 调整顶部滚动视图选中按钮位置
UIButton *button = (UIButton *)[_topScrollView viewWithTag:_userSelectedChannelID];
[self adjustScrollViewContentX:button];
}
} /*!
* @method 创建子视图UI
* @abstract
* @discussion
* @param
* @result
*/
- (void)buildUI
{
NSUInteger number = [self.slideSwitchViewDelegate numberOfTab:self];
for (int i=0; i<number; i++) {
UIViewController *vc = [self.slideSwitchViewDelegate slideSwitchView:self viewOfTab:i];
[_viewArray addObject:vc];
[_rootScrollView addSubview:vc.view];
}
[self createNameButtonsWithNumber:number]; //选中第一个view
if (self.slideSwitchViewDelegate && [self.slideSwitchViewDelegate respondsToSelector:@selector(slideSwitchView:didselectTab:)]) {
[self.slideSwitchViewDelegate slideSwitchView:self didselectTab:_userSelectedChannelID - 100];
} _isBuildUI = YES; //创建完子视图UI才须要调整布局
[self setNeedsLayout];
} /*!
* @method 初始化顶部tab的各个按钮
* @abstract
* @discussion
* @param
* @result
*/
- (void)createNameButtonsWithNumber:(int)number
{ _shadowImageView = [[UIImageView alloc] init];
[_shadowImageView setImage:_shadowImage];
[_topScrollView addSubview:_shadowImageView]; //顶部tabbar的总长度
CGFloat topScrollViewContentWidth = kWidthOfButtonMargin; CGRect rect = [UIScreen mainScreen].applicationFrame;
int width = rect.size.width / number; //每一个tab偏移量
CGFloat xOffset = 0;
for (int i = 0; i < [_viewArray count]; i++) {
UIViewController *vc = _viewArray[i];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; //累计每一个tab文字的长度
topScrollViewContentWidth += kWidthOfButtonMargin + width;
//设置按钮尺寸
[button setFrame:CGRectMake(xOffset, 0, width, kHeightOfTopScrollView)];
//计算下一个tab的x偏移量
xOffset += width; [button setTag:i+100];
if (i == 0) {
_shadowImageView.frame = CGRectMake(0, 0, width, _shadowImage.size.height);
button.selected = YES;
}
[button setTitle:vc.title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:kFontSizeOfTabButton];
[button setTitleColor:self.tabItemNormalColor forState:UIControlStateNormal];
[button setTitleColor:self.tabItemSelectedColor forState:UIControlStateSelected];
[button setBackgroundImage:self.tabItemNormalBackgroundImage forState:UIControlStateNormal];
[button setBackgroundImage:self.tabItemSelectedBackgroundImage forState:UIControlStateSelected];
[button addTarget:self action:@selector(selectNameButton:) forControlEvents:UIControlEventTouchUpInside];
[_topScrollView addSubview:button]; if (i < _viewArray.count) { UIView *line = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(button.frame), 0, 0.5, _topScrollView.frame.size.height - 2)];
line.backgroundColor = UIColorFromIntRBG(212, 212, 212); [_topScrollView addSubview:line]; } } //设置顶部滚动视图的内容总尺寸
_topScrollView.contentSize = CGSizeMake(topScrollViewContentWidth, kHeightOfTopScrollView);
} /*!
* @method 初始化顶部tab的各个按钮
* @abstract
* @discussion
* @param
* @result
*/
- (void)createNameButtons
{ _shadowImageView = [[UIImageView alloc] init];
[_shadowImageView setImage:_shadowImage];
[_topScrollView addSubview:_shadowImageView]; //顶部tabbar的总长度
CGFloat topScrollViewContentWidth = kWidthOfButtonMargin;
//每一个tab偏移量
CGFloat xOffset = kWidthOfButtonMargin;
for (int i = 0; i < [_viewArray count]; i++) {
UIViewController *vc = _viewArray[i];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGSize textSize = [vc.title sizeWithFont:[UIFont systemFontOfSize:kFontSizeOfTabButton]
constrainedToSize:CGSizeMake(_topScrollView.bounds.size.width, kHeightOfTopScrollView)
lineBreakMode:NSLineBreakByTruncatingTail];
//累计每一个tab文字的长度
topScrollViewContentWidth += kWidthOfButtonMargin+textSize.width;
//设置按钮尺寸
[button setFrame:CGRectMake(xOffset,0,
textSize.width, kHeightOfTopScrollView)];
//计算下一个tab的x偏移量
xOffset += textSize.width + kWidthOfButtonMargin; [button setTag:i+100];
if (i == 0) {
_shadowImageView.frame = CGRectMake(kWidthOfButtonMargin, 0, textSize.width, _shadowImage.size.height);
button.selected = YES;
}
[button setTitle:vc.title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:kFontSizeOfTabButton];
[button setTitleColor:self.tabItemNormalColor forState:UIControlStateNormal];
[button setTitleColor:self.tabItemSelectedColor forState:UIControlStateSelected];
[button setBackgroundImage:self.tabItemNormalBackgroundImage forState:UIControlStateNormal];
[button setBackgroundImage:self.tabItemSelectedBackgroundImage forState:UIControlStateSelected];
[button addTarget:self action:@selector(selectNameButton:) forControlEvents:UIControlEventTouchUpInside];
[_topScrollView addSubview:button];
} //设置顶部滚动视图的内容总尺寸
_topScrollView.contentSize = CGSizeMake(topScrollViewContentWidth, kHeightOfTopScrollView);
} #pragma mark - 顶部滚动视图逻辑方法 /*!
* @method 选中tab时间
* @abstract
* @discussion
* @param 按钮
* @result
*/
- (void)selectNameButton:(UIButton *)sender
{
//假设点击的tab文字显示不全,调整滚动视图x坐标使用使tab文字显示全
[self adjustScrollViewContentX:sender]; //假设更换按钮
if (sender.tag != _userSelectedChannelID) {
//取之前的按钮
UIButton *lastButton = (UIButton *)[_topScrollView viewWithTag:_userSelectedChannelID];
lastButton.selected = NO;
//赋值按钮ID
_userSelectedChannelID = sender.tag;
} //按钮选中状态
if (!sender.selected) {
sender.selected = YES; [UIView animateWithDuration:0.25 animations:^{ [_shadowImageView setFrame:CGRectMake(sender.frame.origin.x, 0, sender.frame.size.width, _shadowImage.size.height)]; } completion:^(BOOL finished) {
if (finished) {
//设置新页出现 (不用动画)
if (!_isRootScroll) {
[_rootScrollView setContentOffset:CGPointMake((sender.tag - 100)*self.bounds.size.width, 0) animated:NO];
}
_isRootScroll = NO; if (self.slideSwitchViewDelegate && [self.slideSwitchViewDelegate respondsToSelector:@selector(slideSwitchView:didselectTab:)]) {
[self.slideSwitchViewDelegate slideSwitchView:self didselectTab:_userSelectedChannelID - 100];
} }
}]; }
//反复点击选中按钮
else { }
} - (void) setScrollViewSelectedWithIndex:(NSInteger)index
{
_userSelectedChannelID = index + 100; //设置新页出现 [_rootScrollView setContentOffset:CGPointMake(index * self.bounds.size.width, 0) animated:YES];
_isRootScroll = NO; if (self.slideSwitchViewDelegate && [self.slideSwitchViewDelegate respondsToSelector:@selector(slideSwitchView:didselectTab:)]) {
[self.slideSwitchViewDelegate slideSwitchView:self didselectTab:_userSelectedChannelID - 100];
} } /*!
* @method 调整顶部滚动视图x位置
* @abstract
* @discussion
* @param
* @result
*/
- (void)adjustScrollViewContentX:(UIButton *)sender
{
//假设 当前显示的最后一个tab文字超出右边界
if (sender.frame.origin.x - _topScrollView.contentOffset.x > self.bounds.size.width - (sender.bounds.size.width)) {
//向左滚动视图,显示完整tab文字
[_topScrollView setContentOffset:CGPointMake(sender.frame.origin.x - (_topScrollView.bounds.size.width- (sender.bounds.size.width)), 0) animated:YES];
} //假设 (tab的文字坐标 - 当前滚动视图左边界所在整个视图的x坐标) < 按钮的隔间 。代表tab文字已超出边界
if (sender.frame.origin.x - _topScrollView.contentOffset.x < 0) {
//向右滚动视图(tab文字的x坐标 - 按钮间隔 = 新的滚动视图左边界在整个视图的x坐标)。使文字显示完整
[_topScrollView setContentOffset:CGPointMake(sender.frame.origin.x, 0) animated:YES];
}
} #pragma mark - 主视图逻辑方法 //滚动视图開始时
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{ startOffsetX = scrollView.contentOffset.x;
// 一開始在最左边时,临时是不做不论什么事情;由于当它向右划时,不需做不论什么事;仅当它向左划时,才须要切换tabItem的状态
if (startOffsetX == 0) {
doSomething = NO;
}
// 一開始在最左边时,临时是不做不论什么事情;由于当它向右划时,不需做不论什么事;仅当它向左划时,才须要切换tabItem的状态
// 960
if (startOffsetX == _rootScrollView.contentSize.width - _rootScrollView.bounds.size.width) {
doSomething = NO;
}
//********************************** if (scrollView == _rootScrollView) {
_userContentOffsetX = scrollView.contentOffset.x;
}
} //滚动视图进行中
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == _rootScrollView) {
//推断用户是否左滚动还是右滚动
if (_userContentOffsetX < scrollView.contentOffset.x) {
_isLeftScroll = YES;
}
else {
_isLeftScroll = NO;
} }
// **************************
// 假设最開始在最左边,可是是往左滑,仍须要调整顶部的tabItem状态
if(startOffsetX == 0 && _isLeftScroll){
doSomething = YES;
}
// 假设最開始在最右边,可是是往右滑,也须要调整顶部的tabItem状态
if(startOffsetX == _rootScrollView.contentSize.width - _rootScrollView.bounds.size.width && !_isLeftScroll){
doSomething = YES;
} } //滚动视图 结束滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{ if (!doSomething) {
return;
} //**********************************
if (scrollView == _rootScrollView) {
_isRootScroll = YES;
//调整顶部滑条按钮状态
int tag = (int)scrollView.contentOffset.x/self.bounds.size.width +100; if (self.slideSwitchViewDelegate && [self.slideSwitchViewDelegate respondsToSelector:@selector(slideSwitchView:didselectTab:)]) {
[self.slideSwitchViewDelegate slideSwitchView:self didselectTab:tag - 100];
} UIButton *button = (UIButton *)[_topScrollView viewWithTag:tag];
// 顶部的tabItem按钮点击
[self selectNameButton:button];
}
} //传递滑动事件给下一层
-(void)scrollHandlePan:(UIPanGestureRecognizer*) panParam
{ // BXLog(@"_userSelectedChannelID ===== %f", _rootScrollView.contentOffset.x); //当滑道左边界时。传递滑动事件给代理
if(_rootScrollView.contentOffset.x <= 0) {
if (self.slideSwitchViewDelegate
&& [self.slideSwitchViewDelegate respondsToSelector:@selector(slideSwitchView:panLeftEdge:)]) {
[self.slideSwitchViewDelegate slideSwitchView:self panLeftEdge:panParam];
}
} else if(_rootScrollView.contentOffset.x >= _rootScrollView.contentSize.width - _rootScrollView.bounds.size.width) {
if (self.slideSwitchViewDelegate
&& [self.slideSwitchViewDelegate respondsToSelector:@selector(slideSwitchView:panRightEdge:)]) {
[self.slideSwitchViewDelegate slideSwitchView:self panRightEdge:panParam];
}
}
} - (void) setHideTopView:(BOOL)hide
{
_line.hidden = hide;
_topScrollView.hidden = hide;
_rootScrollView.frame = CGRectMake(0, 0, _rootScrollView.frame.size.width, self.bounds.size.height);
} #pragma mark - 工具方法 /*!
* @method 通过16进制计算颜色
* @abstract
* @discussion
* @param 16机制
* @result 颜色对象
*/
+ (UIColor *)colorFromHexRGB:(NSString *)inColorString
{
UIColor *result = nil;
unsigned int colorCode = 0;
unsigned char redByte, greenByte, blueByte; if (nil != inColorString)
{
NSScanner *scanner = [NSScanner scannerWithString:inColorString];
(void) [scanner scanHexInt:&colorCode]; // ignore error
}
redByte = (unsigned char) (colorCode >> 16);
greenByte = (unsigned char) (colorCode >> 8);
blueByte = (unsigned char) (colorCode); // masks off high bits
result = [UIColor
colorWithRed: (float)redByte / 0xff
green: (float)greenByte/ 0xff
blue: (float)blueByte / 0xff
alpha:1.0];
return result;
}
#pragma mark - 工具方法
// 从颜色 转成 图片
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
//Create a context of the appropriate size
UIGraphicsBeginImageContext(size);
CGContextRef currentContext = UIGraphicsGetCurrentContext(); //Build a rect of appropriate size at origin 0,0
CGRect fillRect = CGRectMake(0, 0, size.width, size.height); //Set the fill color
CGContextSetFillColorWithColor(currentContext, color.CGColor); //Fill the color
CGContextFillRect(currentContext, fillRect); //Snap the picture and close the context
UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return colorImage;
}
@end
</span>
//
// ViewController.m
// 多控制器选项卡切换效果
//
// Created by beyond on 14-10-18.
// Copyright (c) 2014年 beyond. All rights reserved.
// #import "ViewController.h"
// 1
#import "SlideSwitchView.h"
// 3
#import "Tab1Ctrl.h"
#import "Tab2Ctrl.h"
#import "Tab3Ctrl.h"
#import "Tab4Ctrl.h"
#define IS_IOS7 [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] #define UIColorFromIntRBG(RED, GREEN, BLUE) [UIColor colorWithRed:RED/255.0 green:GREEN/255.0 blue:BLUE/255.0 alpha:1.0] @interface ViewController ()<SlideSwitchViewDelegate>
{
// 4
Tab1Ctrl *_tab1VC;
Tab2Ctrl *_tab2VC;
Tab3Ctrl *_tab3VC;
Tab4Ctrl *_tab4VC; // 5
SlideSwitchView *_slideSwitchView;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
if (IS_IOS7) {
self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars = NO;
self.modalPresentationCapturesStatusBarAppearance = NO;
}
// 2创建全部的tab相应的控制器们
[self setupChildCtrollers]; // 5创建Slide总的view
[self setupSlideSwitchView];
} // 2创建全部的tab相应的控制器们
- (void) setupChildCtrollers
{
_tab1VC = [[Tab1Ctrl alloc] init];
_tab1VC.title = @"小学同学";
// _tab1VC.delegate = self; _tab2VC = [[Tab2Ctrl alloc] init];
_tab2VC.title = @"初中同学";
// _tab2VC.delegate = self; _tab3VC = [[Tab3Ctrl alloc] init];
_tab3VC.title = @"高中同学";
// _tab3VC.delegate = self; _tab4VC = [[Tab4Ctrl alloc] init];
_tab4VC.title = @"大学同学";
// _tab4VC.delegate = self;
} // 5创建Slide总的view
- (void)setupSlideSwitchView
{
_slideSwitchView = [[SlideSwitchView alloc] initWithFrame:CGRectMake(0, 20, 320,480 )];
// 加入到控制器的view
[self.view addSubview:_slideSwitchView]; // tabItem正常的 和 选中时的颜色
_slideSwitchView.tabItemNormalColor = UIColorFromRGB(0x666666);
_slideSwitchView.tabItemSelectedColor = UIColorFromIntRBG(51, 125, 231); UIImage *shadowImage = [[UIImage imageNamed:@"bottom_line.png"]
stretchableImageWithLeftCapWidth:30.0f topCapHeight:0.0f];
_slideSwitchView.shadowImage = shadowImage;
// 固定写法,代理,用于监听滑动事件
_slideSwitchView.slideSwitchViewDelegate = self;
// 固定写法,创建子视图UI
[_slideSwitchView buildUI];
} #pragma mark - 数据源和代理方法 // 顶部tab个数
- (NSUInteger)numberOfTab:(SlideSwitchView *)view
{
return 4;
} // 每一个tab所属的viewController
- (UIViewController *)slideSwitchView:(SlideSwitchView *)view viewOfTab:(NSUInteger)number
{
if (number == 0) {
return _tab1VC;
} else if (number == 1) {
return _tab2VC;
} else if (number == 2) {
return _tab3VC;
} else {
return _tab4VC;
}
} #pragma mark - 可选实现
// 滑动左边界时传递手势
- (void)slideSwitchView:(SlideSwitchView *)view panLeftEdge:(UIPanGestureRecognizer*) panParam{}
// 滑动右边界时传递手势
- (void)slideSwitchView:(SlideSwitchView *)view panRightEdge:(UIPanGestureRecognizer*) panParam{}
// 点击tab
- (void)slideSwitchView:(SlideSwitchView *)view didselectTab:(NSUInteger)number{}
@end
//
// Tab1Ctrl.m
// 多控制器选项卡切换效果
//
// Created by beyond on 14-10-18.
// Copyright (c) 2014年 beyond. All rights reserved.
// #import "Tab1Ctrl.h" @interface Tab1Ctrl () @end @implementation Tab1Ctrl - (void)viewDidLoad {
[super viewDidLoad]; CGRect rect = CGRectMake(0, 0, 320, 416);
UIImageView *imageView = [[UIImageView alloc]initWithFrame:rect];
imageView.image = [UIImage imageNamed:@"1.png"];
imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:imageView];
} @end
iOS_第3方类库_側滑选项卡SlideSwitchView的更多相关文章
- iOS_第3方类库MBprogressHUD
1,将下载好的第3方类库MBprogressHUD源代码包增加到project(事实上就是一个.h和.m文件) 2,进入project的Build Phases,将源代码包里面的所有.m文件所有加入到 ...
- iOS_第3方类库_BlurAlertView_GPUImage
最终效果图: 先加入GPUImage.framework 导入BlurAlertView的类声明和类实现 // // BlurAlertView.h // 特效弹出框 // // Created by ...
- slidingmenu+fragment实现经常使用的側滑效果(包含Fragment状态的保存)
一.需求 关于fragment的问题,一直想写一篇博客了.应该当初自己也是对这玩意一点都不熟悉到如今也大概知道个日常的使用的地步. 一个側滑的导航栏,内有4个条目.每个选项点击进入相应的界面,每个界面 ...
- Android 高仿QQ5.2双向側滑菜单DrawerLayout实现源代码
Android 高仿QQ5.2双向側滑菜单DrawerLayout实现源代码 左右側滑效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a ...
- 側滑回退的layout(相似IOS側滑回退到上一个activity)
用过apple的同学应该都知道,大多数IOS应用都支持側滑回退,就不具体说明了,直接上图: 作为使用ios的android开发人员来说,我是特别喜欢这个功能的.既然这样,那就在android上也实现这 ...
- 高仿QQ6.0側滑菜单之滑动优化(二)
好了,昨天已经实现了高仿QQ6.0的側滑大致框架.如有兴趣.能够去看下仿QQ6.0側滑之ViewDragHelper的使用(一) 可是之前的实现.仅仅是简单的能够显示和隐藏左側的菜单,可是特别生硬,并 ...
- Android 实现形态各异的双向側滑菜单 自己定义控件来袭
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39670935.本文出自:[张鸿洋的博客] 1.概述 关于自己定义控件側滑已经写了 ...
- Android学习之仿QQ側滑功能的实现
如今项目越来越多的应用了滑动删除的功能,Android本来遵循的是长按删除,IOS定制的是滑动删除,不可否认滑动删除确实在客户体验上要好一点,所以看了非常多关于仿QQ滑动删除的样例,还是感觉代码家的A ...
- 高仿QQ6.0之側滑删除
前两天已经完毕了高仿QQ6.0側滑和优化,今天来看下側滑删除的实现吧,假设有兴趣,能够去看下之前的两篇,仿QQ6.0側滑之ViewDragHelper的使用(一)和高仿QQ6.0側滑菜单之滑动优化(二 ...
随机推荐
- 【codeforces 733F】Drivers Dissatisfaction
[题目链接]:http://codeforces.com/problemset/problem/733/F [题意] 给你n个点m条边; 让你从中选出n-1条边; 形成一个生成树; (即让n个点都联通 ...
- java--web学习总结<转>
http://www.cnblogs.com/xdp-gacl/p/3729033.html
- Linux内核源码情景分析-wait()、schedule()
父进程执行wait4,并调用schedule切换到子进程: wait4(child, NULL, 0, NULL); 像其它系统调用一样.wait4()在内核中的入口是sys_wait4().代码例如 ...
- Remove Duplicates from Sorted List II 解答(有个比較特殊的case leetcode OJ没有覆盖)
昨天被考了一道数据结构题,当时的实现比較一般.回来翻看leetcode,果然是上面的题.遂解之. accept之后翻看discuss别人的解法.发现非常多能够accept的代码都过不了我设计的一个ca ...
- Android实现天气预报温度/气温折线趋势图
Android实现天气预报温度/气温折线趋势图 天气预报的APP应用中,难免会遇到绘制天气温度/气温,等关于数据趋势的折线或者曲线图,这类关于气温/温度的折线图,通常会有两条线.一条是高温线,一 ...
- html屏蔽鼠标右键
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue 父子组件传值以及方法调用,平行组件之间传值以及方法调用大全
vue项目经常需要组件间的传值以及方法调用,具体场景就不说了,都知道.基本上所有的传值都可以用vuex状态管理来实现,只要在组件内监听vuex就好. vue常用的传值方式以及方法有: 1. 父值传子( ...
- mybatis的sql中字段两种映射(映射到实体)方式
mybatis的xml配置文件中,字段映射的两种方式: 1.resultMap标签中将数据库的字段与实体类中的字段对应: <resultMap id="BaseResultMap&qu ...
- 3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)
3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称.它相当于是对每个数据块应用三次DES加密算法.由于计 ...
- 线程状态与tcb、线程的生命周期
struct tcb { u32_t status; struct reg_context thread_context; void *stack; struct thread_info thread ...