ios选项卡按钮

效果图:ios选项卡按钮_第1张图片

第三方 purelayout    pop

h文件

//
//  YLOptionsView.h
//  YangLand
//
//  Created by point on 16/3/24.
//  Copyright © 2016年 赵大财. All rights reserved.
//

#import "BaseView.h"

@class YLOptionsView;

@protocol YLOptionsViewDelegate <NSObject>

@optional
- (void)YLOptionsView:(YLOptionsView *)optionsView didSelectRowAtIndexPath:(NSInteger)indexPath;

@end

@interface YLOptionsView : BaseView

@property (nonatomic, weak) id<YLOptionsViewDelegate> delegate;

- (instancetype)initWithOptions:(NSArray *)arr selected:(NSString *)selectStr;

@end


m 文件

//
//  YLOptionsView.m
//  YangLand
//
//  Created by point on 16/3/24.
//  Copyright © 2016年 赵大财. All rights reserved.
//

#import "YLOptionsView.h"

static const CGFloat lineLackWidth = 20; //缺少的宽度

@interface YLOptionsView ()
@property (strong,nonatomic) UIView *lineView;
@end

@implementation YLOptionsView

- (instancetype)initWithOptions:(NSArray *)arr selected:(NSString *)selectStr {
    self = [super init];
    if (self) {
        CGFloat width = YLScreenW/arr.count;
        int i = 0;
        int selectedIndex = 0;
        for (NSString *title in arr) {
            if([selectStr isEqualToString:arr[i]]) {
                selectedIndex = i;
            }
            UILabel *label = [UILabel newAutoLayoutView];
            [self addSubview:label];
            [label autoSetDimension:ALDimensionWidth toSize:width];
            [label autoSetDimension:ALDimensionHeight toSize:44];
            label.backgroundColor = [UIColor whiteColor];
            [label autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:i*width];
            [label autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
            label.text = title;
            label.textAlignment = NSTextAlignmentCenter;
            label.tag = 100+i;
            WS(weakSelf);
            [label addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
                CGFloat toValue = (label.tag-100)*width+(width/2);
                POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
                anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
                anim.duration = 0.1;
                anim.toValue = [NSValue valueWithCGPoint:CGPointMake(toValue, 42)];
                [weakSelf.lineView pop_addAnimation:anim forKey:nil];
                if ([weakSelf.delegate respondsToSelector:@selector(YLOptionsView: didSelectRowAtIndexPath:)]) {
                    [weakSelf.delegate YLOptionsView:weakSelf didSelectRowAtIndexPath:i];
                }
            }];
            i++;
        }
        self.lineView = [UIView newAutoLayoutView];
        [self addSubview: self.lineView];
        [ self.lineView autoSetDimensionsToSize:CGSizeMake(width-lineLackWidth, 2)];
        self.lineView.backgroundColor = [UIColor redColor];
        CGFloat edgeLeft = selectedIndex*width+lineLackWidth/2;
        [ self.lineView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:edgeLeft];
        [ self.lineView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];
    }
    return self;
}
@end


你可能感兴趣的:(ios选项卡按钮)