首先当然是引入该文件 再初始化一下嘛
#import "TestView.h"
@property (nonatomic, strong) TestView *TestView;
在需要的view上面直接使用 需要传入几个值
/**
一些需要的值
@param titles 标题数组
@param Ncolor 正常背景色
@param Scolor 隐藏背景色
@param Lcolor 滚动线背景色
@param index 默认位置
*/
_TestView = [[TestView alloc]initWithFrame:CGRectMake(0, 550, SCREEN_WIDTH, 48)];
NSArray *title = @[@"豆奶",@"饼干",@"冰糖",@"豆浆",@"饼干",@"冰糖",@"豆浆"];
[_TestView MakesegTitle:title titlenormalColcor:[UIColor grayColor] titleseletedColor:[UIColor orangeColor] lineColor:[UIColor orangeColor] selectedSegmentIndex:3];
_TestView.Block = ^(NSString *itemname, NSInteger index) {
NSLog(@"%@--%ld",itemname,index);
};
[self.view addSubview:_TestView];
Block回调返回点击位置做其他事件处理
这里还有一个方法是外部控件 比如配合使用的UIscrollowView 可以在适当地方调用该方法改变UISegmentedControl显示位置
[_TestView selectedSegmentIndex:seg.selectedSegmentIndex];
看看方法吧:.h文件
#import <UIKit/UIKit.h>
#import "UIView+Extension.h"//扩展文件
typedef void (^clickblock)(NSString *itemname,NSInteger index);
@interface TestView : UIView
@property(nonatomic, copy)clickblock Block;
/**
一些需要的值
@param titles 标题数组
@param Ncolor 正常背景色
@param Scolor 隐藏背景色
@param Lcolor 滚动线背景色
@param index 默认位置
*/
-(void)MakesegTitle:(NSArray*)titles titlenormalColcor:(UIColor *)Ncolor titleseletedColor:(UIColor*)Scolor lineColor:(UIColor*)Lcolor selectedSegmentIndex:(NSInteger)index;
//外部控件调用这个控制位置
-(void)selectedSegmentIndex:(NSInteger )index;
@end
.m文件
#import "TestView.h"
@interface TestView()
@property (retain,nonatomic) UISegmentedControl *segControl;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, assign) NSInteger count;
@end
@implementation TestView
- (instancetype)initWithFrame:(CGRect)frame{
if (self == [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
self.frame = frame;
}
return self;
}
-(void)MakesegTitle:(NSArray *)titles titlenormalColcor:(UIColor *)Ncolor titleseletedColor:(UIColor *)Scolor lineColor:(UIColor *)Lcolor selectedSegmentIndex:(NSInteger)index{
self.count = titles.count;
_segControl = [[UISegmentedControl alloc]initWithItems:titles];
_segControl.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-4);
_segControl.tintColor = [UIColor whiteColor];
[self addSubview:_segControl];
_segControl.selectedSegmentIndex = index;
//设置Segment的字体
NSDictionary *dic1 = @{
//1.设置字体样式:例如黑体,和字体大小
NSFontAttributeName:[UIFont fontWithName:@"Arial-BoldMT" size:20],
//2.字体颜色
NSForegroundColorAttributeName:Ncolor
};
[_segControl setTitleTextAttributes:dic1 forState:UIControlStateNormal];
//设置Segment的字体
NSDictionary *dic2 = @{
//1.设置字体样式:例如黑体,和字体大小
NSFontAttributeName:[UIFont fontWithName:@"Arial-BoldMT" size:20],
//2.字体颜色
NSForegroundColorAttributeName:Lcolor
};
[_segControl setTitleTextAttributes:dic2 forState:UIControlStateSelected];
[_segControl addTarget:self action:@selector(segclick:) forControlEvents:UIControlEventValueChanged];
UIView *lineView = [[UIView alloc]init];
lineView.frame = CGRectMake(index * self.frame.size.width/self.count, self.frame.size.height-4, self.frame.size.width/self.count, 2);
lineView.backgroundColor = Lcolor;
self.lineView = lineView;
[self addSubview:self.lineView];
}
-(void)segclick:(UISegmentedControl *)seg{
NSLog(@"第%ld个",seg.selectedSegmentIndex);
[UIView animateWithDuration:0.3 animations:^{
self.lineView.x = seg.selectedSegmentIndex*self.frame.size.width/self.count;
}];
if (_Block) {
self.Block(@"预留位置", seg.selectedSegmentIndex);
}
}
-(void)selectedSegmentIndex:(NSInteger)index{
NSLog(@"反向点击:%ld",index);
_segControl.selectedSegmentIndex = index;
[UIView animateWithDuration:0.3 animations:^{
self.lineView.x = index*self.frame.size.width/self.count;
}];
}
@end
UIview的扩展文件
扩展文件.png
#import <UIKit/UIKit.h>
@interface UIView (Extension)
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGSize size;
@property (nonatomic, assign) CGPoint origin;
@property (nonatomic, assign) CGFloat centerX;
@property (nonatomic, assign) CGFloat centerY;
/**
* 9.上 < Shortcut for frame.origin.y
*/
@property (nonatomic) CGFloat top;
/**
* 10.下 < Shortcut for frame.origin.y + frame.size.height
*/
@property (nonatomic) CGFloat bottom;
/**
* 11.左 < Shortcut for frame.origin.x.
*/
@property (nonatomic) CGFloat left;
/**
* 12.右 < Shortcut for frame.origin.x + frame.size.width
*/
@property (nonatomic) CGFloat right;
- (void)addTarget:(id)target action:(SEL)action;
@end
#import "UIView+Extension.h"
@implementation UIView (Extension)
- (void)setX:(CGFloat)x {
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
- (CGFloat)x {
return self.frame.origin.x;
}
- (void)setY:(CGFloat)y {
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
- (CGFloat)y {
return self.frame.origin.y;
}
- (void)setWidth:(CGFloat)width {
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
- (CGFloat)width {
return self.frame.size.width;
}
- (void)setHeight:(CGFloat)height {
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
- (CGFloat)height {
return self.frame.size.height;
}
- (void)setSize:(CGSize)size {
// self.width = size.width;
// self.height = size.height;
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
- (CGSize)size {
return self.frame.size;
}
- (void)setOrigin:(CGPoint)origin {
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
}
- (CGPoint)origin {
return self.frame.origin;
}
- (void)setCenterX:(CGFloat)centerX {
CGPoint center = self.center;
center.x = centerX;
self.center = center;
}
- (CGFloat)centerX {
return self.center.x;
}
- (void)setCenterY:(CGFloat)centerY {
CGPoint center = self.center;
center.y = centerY;
self.center = center;
}
- (CGFloat)centerY {
return self.center.y;
}
- (CGFloat)left {
return self.frame.origin.x;
}
- (void)setLeft:(CGFloat)x {
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
- (CGFloat)top {
return self.frame.origin.y;
}
- (void)setTop:(CGFloat)y {
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
- (CGFloat)right {
return self.frame.origin.x + self.frame.size.width;
}
- (void)setRight:(CGFloat)right {
CGRect frame = self.frame;
frame.origin.x = right - frame.size.width;
self.frame = frame;
}
- (CGFloat)bottom {
return self.frame.origin.y + self.frame.size.height;
}
- (void)setBottom:(CGFloat)bottom {
CGRect frame = self.frame;
frame.origin.y = bottom - frame.size.height;
self.frame = frame;
}
- (void)addTarget:(id)target action:(SEL)action;
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:target
action:action];
self.userInteractionEnabled = YES;
[self addGestureRecognizer:tap];
}
@end
网友评论