iOS开发商城时会遇到 商品 详情 评论 三个视图筛选的页面, 这里封装了一个框架,可以使随意切换子视图界面,继承简单,也封装了滑条框架.效果图如下:
滑条框架.h文件
#import <UIKit/UIKit.h>
@interface SectionView : UIView//
@property(nonatomic,copy)void (^sectionViewClick)(int index);
//添加一个Secion按钮
- (void)adSectionItemWithtitle:(NSString *)title font:(CGFloat)font;
//添加一个Secion按钮
- (void)adSectionItemWithtitle:(NSString *)title img:(NSString *)imgName selectImg:(NSString *)selectImgName font:(CGFloat)font;
// 设置选中的按钮
@property(nonatomic,assign)NSInteger selectItemIndex;
//提供一个方法给设置文字
- (void)setTitleContent:(NSString *)title index:(int)index;
@end
滑条框架.m文件
#import "SectionView.h"
#define kSectionTitleFont 16
// 正常时候的文字颜色
#define kSectionItemNormalColor [UIColor colorWithRed:126/255.0 green:127/255.0 blue:126/255.0 alpha:1]
#define kSectionItemSelectColor [UIColor redColor]
#define kinditorWidth 80
@interface SectionView ()
{
UIButton * _currentSelectBtn;
// 指示器
UIView * _inditorView;
// 底部分割显示
UIView * _seperateView;
// 装按键
UIView * _containerItemView;
}
@end
@implementation SectionView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_containerItemView = [[UIView alloc] init];
_containerItemView.frame = self.bounds;
[self addSubview:_containerItemView];
// 指示器
_inditorView = [[UIView alloc]init];
_inditorView.backgroundColor = kSectionItemSelectColor;
_inditorView.bounds = CGRectMake(0, 0,kinditorWidth ,2);
[self addSubview:_inditorView];
//底部分割线
// _seperateView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-0.5, frame.size.width,0.8)];
// _seperateView.backgroundColor = [UIColor redColor];
// [self addSubview:_seperateView];
}
return self;
}
//添加一个按钮
- (void)adSectionItemWithtitle:(NSString *)title font:(CGFloat)font{
UIButton * item = [UIButton buttonWithType:UIButtonTypeCustom];
// 文字
[item setTitle:title forState:UIControlStateNormal];
[item setTitleColor:kSectionItemNormalColor forState:UIControlStateNormal];
[item setTitleColor:kSectionItemSelectColor forState:UIControlStateSelected];
item.titleLabel.numberOfLines = 0;
item.titleLabel.textAlignment = NSTextAlignmentCenter;
item.titleLabel.font = [UIFont systemFontOfSize:font];
item.adjustsImageWhenHighlighted = NO;
[item addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchDown];
[_containerItemView addSubview:item];
// 调节位置
[self adjusetItemFrame];
}
- (void)adSectionItemWithtitle:(NSString *)title img:(NSString *)imgName selectImg:(NSString *)selectImgName font:(CGFloat)font{
UIButton * item = [UIButton buttonWithType:UIButtonTypeCustom];
[item setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
[item setImage:[UIImage imageNamed:selectImgName] forState:UIControlStateSelected];
// 文字
[item setTitle:title forState:UIControlStateNormal];
[item setTitleColor:kSectionItemNormalColor forState:UIControlStateNormal];
[item setTitleColor:kSectionItemSelectColor forState:UIControlStateSelected];
item.titleLabel.numberOfLines = 0;
item.titleLabel.textAlignment = NSTextAlignmentCenter;
item.titleLabel.font = [UIFont systemFontOfSize:font];
item.adjustsImageWhenHighlighted = NO;
[item addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchDown];
[_containerItemView addSubview:item];
// 调节位置
[self adjusetItemFrame];
}
// 按键
- (void)clickItem:(UIButton *)item{
_currentSelectBtn.selected = NO;
item.selected = YES;
_currentSelectBtn = item;
[self setInditorCenter:item];
if(_sectionViewClick){
_sectionViewClick((int)item.tag -10);
}
}
// 调节位置
- (void)adjusetItemFrame{
NSInteger itemCount = _containerItemView.subviews.count ;
CGFloat itemWidth = self.frame.size.width / (itemCount);
CGFloat itemHeight = self.frame.size.height;
for (NSInteger i =0; i <itemCount; i++) {
UIButton * btn = (UIButton *)_containerItemView.subviews[i];
btn.frame = CGRectMake(i * (itemWidth+1), 0, itemWidth, itemHeight);
if(i ==0){
btn.selected = YES;
_currentSelectBtn = btn;
//默认只是第一个
_inditorView.center = CGPointMake(_currentSelectBtn.center.x, self.frame.size.height - 0.75);
}
btn.tag = i + 10;
}
_inditorView.bounds = CGRectMake(0, 0,self.frame.size.width /itemCount -20 , 1.2);
}
- (void)setInditorCenter:(UIButton*)item{
[UIView animateWithDuration:0.2 animations:^{
_inditorView.center = CGPointMake(item.center.x, self.frame.size.height - 0.75);
}];
}
// 设置当前选中那个按钮
- (void)setSelectItemIndex:(int)selectItemIndex{
if(selectItemIndex >=_containerItemView.subviews.count )return;
UIButton * item = _containerItemView.subviews[selectItemIndex];
[self selectItemIndex:item];
}
- (void)selectItemIndex:(UIButton *)item
{
// 1.让当前的item取消选中
_currentSelectBtn.selected = NO;
// 2.让新的item选中
item.selected = YES;
// 3.让新的item变为当前选中
_currentSelectBtn= item;
[self setInditorCenter:item];
// 4.调用block
if(_sectionViewClick){
_sectionViewClick((int)item.tag -10);
}
}
n>// 4.调用block
if(_sectionViewClick){
_sectionViewClick((int)item.tag -10);
}
}
// 设置文字内容
- (void)setTitleContent:(NSString *)title index:(int)index{
if(index>=_containerItemView.subviews.count)return;
UIButton * item = _containerItemView.subviews[index];
if([title containsString:@"\n"]){
NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:title];
NSRange sep = [title rangeOfString:@"\n"];
NSRange range = NSMakeRange(sep.location +1,title.length - sep.location -1);
[str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:11] range:range];
item.titleLabel.attributedText = str;
}
[item setTitle:title forState:UIControlStateNormal];
}
@end
最后是父视图控制器,只需要拷贝.m文件里面代码即可,里面的子视图可以自己随意修改
//
// FirstViewController.m
// 商品详情
//
// Created by HandsomeC on 2017/12/28.
// Copyright © 2017年 赵发生. All rights reserved.
//
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h"
#import "SectionView.h"
@interface FirstViewController