iOS开发 商城中商品详情 ,评价,商品页面继承框架
2017-12-28 15:17
821 查看
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 * _containe
1ec90
rItemView;
}
@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);
}
}
// 设置文字内容
- (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 ()<UIScrollViewDelegate>
@property (strong,
nonatomic) UIScrollView *scrollerView;
@property (nonatomic,strong)
SectionView *headerBtn;
@end
@implementation FirstViewController
//设置滑动视图
- (UIScrollView *)scrollerView{
if (!_scrollerView) {
_scrollerView = [[UIScrollView
alloc] initWithFrame:CGRectZero];
_scrollerView.frame =
self.view.bounds;
_scrollerView.showsVerticalScrollIndicator =
NO;
_scrollerView.showsHorizontalScrollIndicator =
NO;
_scrollerView.pagingEnabled =
YES;
_scrollerView.bounces =
NO;
_scrollerView.delegate =
self;
[self.view
addSubview:_scrollerView];
}
return
_scrollerView;
}
- (void)viewDidLoad {
[super
viewDidLoad];
self.view.backgroundColor = [UIColor
whiteColor];
self.scrollerView.backgroundColor
= self.view.backgroundColor;
[self
setUPButton];
[self
addChildViewContrlllers];
[self
changeChildViewController];
self.scrollerView.contentSize
= CGSizeMake(self.view.frame.size.width
* self.childViewControllers.count,
0);
}
//设置顶部筛选按钮
- (void)setUPButton{
NSArray *titles =
@[@"商品",@"详情",@"评价"];
_headerBtn = [[SectionView
alloc] initWithFrame:CGRectMake(self.view.frame.size.width/3,24,self.view.frame.size.width/3,40)];
_headerBtn.backgroundColor = [UIColor
whiteColor];
for (int i =
0; i < 3; i++) {
[_headerBtn
adSectionItemWithtitle:titles[i] font:15];
}
self.navigationItem.titleView =
_headerBtn;
__block
FirstViewController * ctl = self;
_headerBtn.sectionViewClick = ^(int index)
{
[UIView
animateWithDuration:0.2
animations:^{
[ctl contentOffSet:index];
}];
};
}
//滑动切换子视图控制器
- (void)changeChildViewController{
NSInteger index =
_scrollerView.contentOffset.x /
_scrollerView.frame.size.width;
UIViewController *childVc =
self.childViewControllers[index];
if (childVc.view.superview)
return; //判断添加就不用再添加了
childVc.view.frame =
CGRectMake(index * _scrollerView.frame.size.width,
0, _scrollerView.frame.size.width,
_scrollerView.frame.size.height);
[_scrollerView
addSubview:childVc.view];
}
//获取点击或者滑动偏移量
- (void)contentOffSet:(NSInteger)index{
CGPoint offset =
_scrollerView.contentOffset;
offset.x =
_scrollerView.frame.size.width * index;
[_scrollerView
setContentOffset:offset animated:YES];
}
//添加子视图控制器
- (void)addChildViewContrlllers{
SecondViewController *secVC = [[SecondViewController
alloc]
init];
[self
addChildViewController:secVC];
ThirdViewController *thirdVC = [[ThirdViewController
alloc]
init];
[self
addChildViewController:thirdVC];
FourthViewController *fourthVC = [[FourthViewController
alloc]
init];
[self
addChildViewController:fourthVC];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
_headerBtn.selectItemIndex = index;
[self
contentOffSet:index];
[self
changeChildViewController];
}
//实现滑动视图的代理方法
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
[self
changeChildViewController];
}
@end
滑条框架.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 * _containe
1ec90
rItemView;
}
@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);
}
}
// 设置文字内容
- (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 ()<UIScrollViewDelegate>
@property (strong,
nonatomic) UIScrollView *scrollerView;
@property (nonatomic,strong)
SectionView *headerBtn;
@end
@implementation FirstViewController
//设置滑动视图
- (UIScrollView *)scrollerView{
if (!_scrollerView) {
_scrollerView = [[UIScrollView
alloc] initWithFrame:CGRectZero];
_scrollerView.frame =
self.view.bounds;
_scrollerView.showsVerticalScrollIndicator =
NO;
_scrollerView.showsHorizontalScrollIndicator =
NO;
_scrollerView.pagingEnabled =
YES;
_scrollerView.bounces =
NO;
_scrollerView.delegate =
self;
[self.view
addSubview:_scrollerView];
}
return
_scrollerView;
}
- (void)viewDidLoad {
[super
viewDidLoad];
self.view.backgroundColor = [UIColor
whiteColor];
self.scrollerView.backgroundColor
= self.view.backgroundColor;
[self
setUPButton];
[self
addChildViewContrlllers];
[self
changeChildViewController];
self.scrollerView.contentSize
= CGSizeMake(self.view.frame.size.width
* self.childViewControllers.count,
0);
}
//设置顶部筛选按钮
- (void)setUPButton{
NSArray *titles =
@[@"商品",@"详情",@"评价"];
_headerBtn = [[SectionView
alloc] initWithFrame:CGRectMake(self.view.frame.size.width/3,24,self.view.frame.size.width/3,40)];
_headerBtn.backgroundColor = [UIColor
whiteColor];
for (int i =
0; i < 3; i++) {
[_headerBtn
adSectionItemWithtitle:titles[i] font:15];
}
self.navigationItem.titleView =
_headerBtn;
__block
FirstViewController * ctl = self;
_headerBtn.sectionViewClick = ^(int index)
{
[UIView
animateWithDuration:0.2
animations:^{
[ctl contentOffSet:index];
}];
};
}
//滑动切换子视图控制器
- (void)changeChildViewController{
NSInteger index =
_scrollerView.contentOffset.x /
_scrollerView.frame.size.width;
UIViewController *childVc =
self.childViewControllers[index];
if (childVc.view.superview)
return; //判断添加就不用再添加了
childVc.view.frame =
CGRectMake(index * _scrollerView.frame.size.width,
0, _scrollerView.frame.size.width,
_scrollerView.frame.size.height);
[_scrollerView
addSubview:childVc.view];
}
//获取点击或者滑动偏移量
- (void)contentOffSet:(NSInteger)index{
CGPoint offset =
_scrollerView.contentOffset;
offset.x =
_scrollerView.frame.size.width * index;
[_scrollerView
setContentOffset:offset animated:YES];
}
//添加子视图控制器
- (void)addChildViewContrlllers{
SecondViewController *secVC = [[SecondViewController
alloc]
init];
[self
addChildViewController:secVC];
ThirdViewController *thirdVC = [[ThirdViewController
alloc]
init];
[self
addChildViewController:thirdVC];
FourthViewController *fourthVC = [[FourthViewController
alloc]
init];
[self
addChildViewController:fourthVC];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
_headerBtn.selectItemIndex = index;
[self
contentOffSet:index];
[self
changeChildViewController];
}
//实现滑动视图的代理方法
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
[self
changeChildViewController];
}
@end
相关文章推荐
- 【商城开发四】Android商品详情全部评价(下拉刷新+上滑加载更多)
- iOS项目开发--实现类似淘宝详情页面
- B2Ctt商城09 商品详情页面
- iOS 开发商品详情页中的banner中点击查看图片
- iOS 开发商品详情页中的banner中点击查看图片
- iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
- ios开发之商城类软件 - 框架构思
- ios开发之--仿购物类详情页面数量添加小功能
- iOS 开发商品详情页中的banner中点击查看图片
- 【商城开发三】Android 仿淘宝商品详情页下拉足迹修改版
- ios 类似淘宝商品详情页面的效果
- iOS开发 仿淘宝,京东商品详情3D动画
- ios仿淘宝商品详情页面粘贴商品规格弹出模板
- Ecshop模板开发(十一):商品详情页显示和添加评价
- iOS 走近商城 APP(三 WKWebView 商品规格选择框架封装)
- 带你逐步深入了解SSM框架——淘淘商城项目之商品详情页面实现
- iOS开发进阶 - 日志输出框架CocoaLumberjack与XcodeColors插件的简单使用(swift版)
- android快速开发框架--快速实现 页面 加载中 加载失败 无数据等状态以及下拉刷新和自动加载
- iOS开发 - WebView的Hybrid框架设计
- iOS新闻客户端开发教程8-加载更多和新闻详情