ios选项卡按钮

效果图:230453_Pcs7_1186234.gif

第三方 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


转载于:https://my.oschina.net/zhaodacai/blog/646979

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值