封裝的水平選擇移動頭

第一步:封裝了一個UIViewide

#import <UIKit/UIKit.h>

@protocol HorizontalMenuProtocol <NSObject>

@optional

@required

- (void)getTag:(NSInteger)tag;

@end

@interface QHHorizontaMenuView : UIView

@property (strong, nonatomic) NSArray *menuArray;
@property (strong, nonatomic) UIFont *font;
@property (strong, nonatomic) UIColor *titleColorDefault;
@property (strong, nonatomic) UIColor *titleColorSelected;
@property (strong, nonatomic) UIColor *markLineColor;

@property (assign, nonatomic) id<HorizontalMenuProtocol>delegate;

- (void)setNameWithArray:(NSArray *)menuArray;

@end
#import "QHHorizontaMenuView.h"

@interface QHHorizontaMenuView()

@property (assign, nonatomic) CGFloat space;
@end

@implementation QHHorizontaMenuView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    self.backgroundColor = [UIColor whiteColor];
    // Drawing code
}

- (void)setNameWithArray:(NSArray *)menuArray {
    _menuArray = menuArray;
    CGFloat space = (self.frame.size.width)/[_menuArray count];
    for (int i = 0; i<[menuArray count]; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundColor:[UIColor whiteColor]];
        button.frame = CGRectMake(space*i, 0, space, self.frame.size.height);
        button.tag = i;
        if (button.tag == 0) {
            button.enabled = NO;
        }
        
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[menuArray objectAtIndex:i]];
        [str addAttributes:@{NSFontAttributeName:_font?_font:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:_titleColorDefault?_titleColorDefault:UIColorFromRGB(0x969699)} range:NSMakeRange(0, [str length])];
        [button setAttributedTitle:str forState:UIControlStateNormal];
        
        NSMutableAttributedString *strSele = [[NSMutableAttributedString alloc] initWithString:[menuArray objectAtIndex:i]];
        [strSele addAttributes:@{NSFontAttributeName:_font?_font:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:_titleColorSelected?_titleColorSelected:UIColorFromRGB(0x19191a)} range:NSMakeRange(0, [strSele length])];
        [button setAttributedTitle:strSele forState:UIControlStateDisabled];
        
        [button addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    
    UIView *markLine =  [[UIView alloc] initWithFrame:CGRectMake(((Main_Screen_Width/_menuArray.count-100)/2), self.frame.size.height-4, 100, 2)];
    markLine.tag = 999;
    markLine.backgroundColor = _markLineColor?_markLineColor:UIColorFromRGB(0x19191a);
    [self addSubview:markLine];
}

- (void)btnClicked:(UIButton *)sender {
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            UIButton *suButton = (UIButton *)subView;
            if (suButton.tag == sender.tag) {
                [suButton setEnabled:NO];
            }else {
                [suButton setEnabled:YES];
            }
        }
    }
    
//    CGFloat space = (self.frame.size.width)/[_menuArray count];
    UIView *view = [self viewWithTag:999];
    [UIView animateWithDuration:0.2f animations:^{
        CGRect markFrame = view.frame;
        markFrame.origin.x = sender.tag*(Main_Screen_Width/_menuArray.count)+((Main_Screen_Width/_menuArray.count-100)/2);
        view.frame = markFrame;
    }];
    
    if ([self.delegate respondsToSelector:@selector(getTag:)]) {
        [self.delegate getTag:sender.tag];
    }
}

@end

第二步:用法ui

QHHorizontaMenuView *menu = [[QHHorizontaMenuView alloc] init];
    menu.frame = CGRectMake(0, 0, Main_Screen_Width, 50);
    [_baseView addSubview:menu];
    
    NSArray *menuArray = @[QHLocalizedString(@"返還中", nil), QHLocalizedString(@"已返還", nil)];
    [menu setNameWithArray:menuArray];
    menu.delegate = self;

實現協議atom

- (void)getTag:(NSInteger)tag {
    
}
相關文章
相關標籤/搜索