运用seg实现选项卡 效果类似于
代码如下
#define kVheadHeight 40
#define kVLineRedMarkHeight 2
#define kVLineRedMarkWidth 20
#define kVLineRedMarkOriginX (((SCREEN_WIDTH/2)-kVLineRedMarkWidth)/2)
#define kVLineRedMarkOriginY (kVheadHeight-2)
@property (nonatomic, strong) UIView *vHeadBg;
@property (nonatomic, strong) UISegmentedControl *segChoose;
@property (nonatomic, strong) UIView *vlineCenterSep;
@property (nonatomic, strong) UIView *vLineRedMark;
//基本控件封装成一句话了 UIKitFactory
_vHeadBg = [UIKitFactory createViewWithFrame:CGRectMake(0,0, SCREEN_WIDTH, 40) backgroundColor:Color_White superView:self.view];
_segChoose = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
_segChoose.backgroundColor = Color_White;
[_segChoose setTintColor:Color_White];
_segChoose.layer.borderColor = Color_White.CGColor;
NSArray *arrTitle = @[@"推荐",@"关注"];
for (int i = 0; i < arrTitle.count; i ++) {
[_segChoose insertSegmentWithTitle:arrTitle[i] atIndex:i animated:NO];
}
[_segChoose setTitleTextAttributes:@{NSFontAttributeName:Font_Moments_SegTitle,NSForegroundColorAttributeName:Color_Moments_SegNormal} forState:UIControlStateNormal];
[_segChoose setTitleTextAttributes:@{NSFontAttributeName:Font_Moments_SegTitle,NSForegroundColorAttributeName:Color_Moments_ColorRed} forState:UIControlStateSelected];
[_segChoose addTarget:self action:@selector(segmentedValueChanged:
) forControlEvents:UIControlEventValueChanged];
[_vHeadBg addSubview:_segChoose];
_segChoose.selectedSegmentIndex = 0;
_vLineRedMark = [UIKitFactory createViewWithFrame:CGRectMake(kVLineRedMarkOriginX,kVLineRedMarkOriginY, kVLineRedMarkWidth, kVLineRedMarkHeight) backgroundColor:Color_Moments_ColorRed superView:_vHeadBg];
_vlineCenterSep = [UIKitFactory createViewWithFrame:CGRectMake(0, 0, 0.5, 20) backgroundColor:Color_LineColor superView:_vHeadBg];
_vlineCenterSep.center = _vHeadBg.center;
[UIKitFactory createViewWithFrame:CGRectMake(0,kVheadHeight-0.5, SCREEN_WIDTH, 0.5) backgroundColor:Color_LineColor superView:_vHeadBg];
seg 事件处理
- (void)segmentedValueChanged:(UISegmentedControl *)segChoose{
[UIView animateWithDuration:0.3f animations:^{
CGRect rect = _vLineRedMark.frame;
rect.origin.x = segChoose.selectedSegmentIndex *(SCREEN_WIDTH/2) + kVLineRedMarkOriginX;
_vLineRedMark.frame = rect;
}];
//刷新数据
[_tbvMomentsMain.mj_header beginRefreshing];
}