1.定义协议,定义方法,定义属性
#import <UIKit/UIKit.h>
#define kTitleViewH 50
@protocol MyOrderTopViewDelegate <NSObject>
@optional
-(void)didSelectedIndex:(NSInteger)tag;
@end
@interface MyOrderTitleView : UIView
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray*)titles;
@property(nonatomic,weak) id<MyOrderTopViewDelegate> delegate;
@end
2.判断代理是否实现
#import "MyOrderTitleView.h"
#import "HWDConstant.h"
#import "UIViewExt.h"
@interface MyOrderTitleView ()
/** 标签栏底部的红色指示器 */
@property (nonatomic, weak) UIView *indicatorView;
/** 当前选中的按钮 */
@property (nonatomic, weak) UIButton *selectedButton;
/** 顶部的所有标签 */
@property (nonatomic, strong) NSArray *titlesViewArr;
@end
@implementation MyOrderTitleView
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray*)titles{
self=[super initWithFrame:frame];
if (self) {
self.titlesViewArr=titles;
[self setupTitlesView:titles];
}
return self;
}
- (void)setupTitlesView:(NSArray*)titles
{
//标签栏整体
self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1.0];
//下面的指示器view
UIView *indicatorView = [[UIView alloc] init];
self.indicatorView=indicatorView;
indicatorView.backgroundColor = DTButtonColor;
indicatorView.height = 2;
indicatorView.tag = -1;
indicatorView.y = kTitleViewH-1;
[self addSubview:indicatorView];
// 内部子视图空间
CGFloat width = KScreenWidth / titles.count;
CGFloat height = 50;
for (NSInteger i = 0; i<titles.count; i++) {
UIButton *button = [[UIButton alloc] init];
button.tag = i;
button.height = height;
button.width = width;
button.x = i * width;
[button setTitleEdgeInsets:UIEdgeInsetsMake(5, 0, 0, 0)];
[button setTitle:titles[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[button setTitleColor:DTButtonColor forState:UIControlStateDisabled];
button.titleLabel.font = [UIFont boldSystemFontOfSize:15];
[button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
// 默认点击了第一个按钮
if (i == 0) {
button.enabled = NO;
self.selectedButton = button;
// 让按钮内部的label根据文字内容来计算尺寸
[button.titleLabel sizeToFit];
indicatorView.width = width-30;
indicatorView.centerX = button.centerX;
}
}
}
- (void)titleClick:(UIButton *)button
{
// 修改按钮状态
self.selectedButton.enabled = YES;
button.enabled = NO;
self.selectedButton = button;
// 动画
[UIView animateWithDuration:0.25 animations:^{
self.indicatorView.width = KScreenWidth/self.titlesViewArr.count-30;
self.indicatorView.centerX = button.centerX;
}];
// 滚动
// CGPoint offset = self.contentView.contentOffset;
// offset.x = button.tag * self.contentView.width;
// [self.contentView setContentOffset:offset animated:YES];
if ([self.delegate respondsToSelector:@selector(didSelectedIndex:)]) {
[self.delegate didSelectedIndex:button.tag];
}
}
@end
3.遵守协议,成为代理,实现方法
#import "MyOrderViewController.h"
#import "HWDConstant.h"
#import "MyOrderTitleView.h"
@interface MyOrderViewController ()<MyOrderTopViewDelegate>
@end
@implementation MyOrderViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor= DefaultColor;
MyOrderTitleView * orderTitleView=[[MyOrderTitleView alloc] initWithFrame:CGRectMake(0, 64, KScreenWidth, kTitleViewH) titles:@[@"全部订单",@"未完成",@"已完成"]];
orderTitleView.delegate=self;
[self.view addSubview:orderTitleView];
}
- (void)didSelectedIndex:(NSInteger)tag{
NSLog(@"%ld",(long)tag);
}
@end