在开发中经常有需求要求创建一排按钮,下面我们就开发这一个组件,实现方式如下:
WHC_ButtonGroupView.h头文件如下:
//
// buttonGroupView.h
// buttonGroupView
//
// Created by 吴海超 on 14-6-6.
// Copyright (c) 2014年 buttonGroupView. All rights reserved.
//
/*
NOTE:获取组按钮默认创建实现方式如下:
+(WHC_ButtonGroupParam *)getDefaultParamWithTitleArr:(NSArray*)titleArr{
WHC_ButtonGroupParam * groupParam = [WHC_ButtonGroupParam new];
groupParam.normalBackColor = [UIColor grayColor];
groupParam.selectedBackColor = [UIColor blackColor];
groupParam.markColor = [UIColor clearColor];
groupParam.segmentLineY = 0.0;
groupParam.segmentLineColor = [UIColor blackColor];
groupParam.normalTextColor = [UIColor whiteColor];
groupParam.selectedTextColor = [UIColor grayColor];
groupParam.titles = titleArr;
groupParam.images = nil;
groupParam.scroll = NO;
groupParam.headLine = YES;
groupParam.footLine = YES;
groupParam.firstSegmentLine = YES;
groupParam.markViewDown = NO;
groupParam.fontSize = 16.0;
groupParam.markPading = 5.0;
return groupParam;
}
*/
#import <UIKit/UIKit.h>
@class WHC_ButtonGroupView;
@protocol WHC_ButtonGroupViewDelegate<NSObject>
@required
- (void)whcButtonGroup:(WHC_ButtonGroupView*)whcBtnView index:(NSInteger)index;
@end
@interface WHC_ButtonGroupParam : NSObject
@property BOOL scroll; //是否支持滚动
@property BOOL footLine; //是否显示底部线
@property BOOL headLine; //是否显示顶部线
@property BOOL markViewDown; //是否下标记
@property BOOL firstSegmentLine; //首部分割线
@property int currIndex; //当前下标
@property CGFloat fontSize; //字体大小
@property CGFloat btnHeight; //按钮高度
@property CGFloat btnWidth; //按钮宽度
@property CGFloat markPading; //游标和按钮的间隙
@property CGFloat segmentLineY; //分隔线Y坐标
@property CGFloat scrollWidth; //滚条宽度
@property UIColor *normalBackColor; //默认按钮背景颜色
@property UIColor *selectedBackColor; //选中按钮背景颜色
@property UIColor *normalTextColor; //默认状态按钮文字颜色
@property UIColor *selectedTextColor; //选中状态按钮文字颜色
@property UIColor *segmentLineColor; //分隔线颜色
@property UIColor *markColor; //移动游标颜色
@property NSArray *titles; //按钮标题
@property NSArray *images; //按钮图片
+ (WHC_ButtonGroupParam *)getDefaultParamWithTitleArr:(NSArray*)titleArr; //获取默认参数
@end
@interface WHC_ButtonGroupView : UIView
@property id<WHC_ButtonGroupViewDelegate> delegate;
- (void)changeSelectedStateButtonWithIndex:(NSInteger)index; //改变指定按钮的状态
- (BOOL)deleteButtonWithIndex:(NSInteger)index withTitle:(NSString*)title; //删除指定按钮
- (BOOL)addButtonWithIndex:(NSInteger)index withTitle:(NSString*)title; //添加按钮到指定位子
- (void)switchUIOrientationWithIsHori:(BOOL)isHori withPading:(CGFloat)pad; //转换屏幕处理
- (id)initWithFrame:(CGRect)frame withParam:(WHC_ButtonGroupParam*)param; //初始化
- (void)gotoOtherBtnOptionWithIndex:(NSInteger)index; //切换到其他按钮响应
- (void)modifyButtonTitleForIndex:(NSInteger)index withTitle:(NSString*)title; //修改指定按钮标题
@end
WHC_ButtonGroupView.m源文件如下:
//
// buttonGroupView.m
// bhtrader
//
// Created by apple on 14-6-6.
// Copyright (c) 2014年 InvestGu. All rights reserved.
//
#import "WHC_ButtonGroupView.h"
#define kLineWidth (1.0)
#define KANMATION_TIME 0.2
@implementation WHC_ButtonGroupParam
//返回默认样式
+(WHC_ButtonGroupParam *)getDefaultParamWithTitleArr:(NSArray*)titleArr{
WHC_ButtonGroupParam * groupParam = [WHC_ButtonGroupParam new];
groupParam.normalBackColor = [UIColor grayColor];
groupParam