iOS 实现一个签到样式

可以随意更改每行显示的个数,并且自适应占满指定区域。

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
@class BackView;
@interface ButtonView : UIView
/**  传入按钮名名字数组  */
@property (nonatomic, strong) NSArray *arrName;
/**  累计的签到数据  */
@property (nonatomic, strong) NSMutableArray *selArr;
/**  是否已签到过  */
@property (nonatomic, assign) BOOL isAdd;
@end


@interface BackView : UIView
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UILabel *label2;
@property (nonatomic, strong) UIButton *button;
-(void)viewAddViewFromname:(NSString *)name;
@end

NS_ASSUME_NONNULL_END
#import "ButtonView.h"

@interface ButtonView ()
@property (nonatomic, strong) NSMutableArray  *arrview;
@end


@implementation ButtonView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {

    }
    return self;
}

- (void)setArrName:(NSArray *)arrName
{
    _arrName = arrName;
    _isAdd = NO;
   self.arrview = [self buttonFromArr:_arrName count:4 lastBuQi:YES];
}

/**
 在指定区域内绘制button组

 @param arrName 按钮显示的名字
 @param count 单行按钮数量
 @param lastBuQi 最后一个是否补齐 也就是让视图范围填满
 @return 返回所有的button组
 */
- (NSMutableArray *)buttonFromArr:(NSArray *)arrName  count:(NSInteger)count lastBuQi:(BOOL)lastBuQi
{
    NSMutableArray *arr = [NSMutableArray array];
    if (count <= 0)
    {
        return arr;
    }
    // 每行个数
    NSInteger yu = arrName.count % count;
    // 要显示的行数
    NSInteger hang = arrName.count / count;
    if ( yu > 0)  { hang += 1; }
    
    CGFloat screnWidth = CGRectGetWidth(self.frame);
    CGFloat screnheight = CGRectGetHeight(self.frame);
    
    // 顶底间距
    CGFloat topBotomSpace = 10;
    // 左右间距
    CGFloat leftRightSpace = 10;
    
    // 横向间距
    CGFloat wSpace = 15;
    // 纵向间距
    CGFloat hSpace = 10;
    
    CGFloat x = 0;
    CGFloat y = topBotomSpace;
    
    CGFloat width = (screnWidth - (count - 1) * wSpace - leftRightSpace * 2) / count;
    CGFloat height = (screnheight - topBotomSpace * ( hang- 1 ) - hSpace * 2) / hang;

    for (NSInteger i = 0; i < arrName.count; i++)
    {
        if (x == 0)
        {
            x += leftRightSpace;
        }
        else
        {
            x += wSpace;
        }
        
        if (lastBuQi == YES)
        {
            // 最后一个补齐机制
            if (i == arrName.count - 1)
            {
                if (x + width + leftRightSpace + 1 < screnWidth)
                {
                    width = screnWidth - x - leftRightSpace;
                }
            }
        }
        BackView *view = [[BackView alloc]init];
        view.frame = CGRectMake(x, y, width, height);
        x += width;
        
        // +1 是因为平均分配可能存在小数
        if ( x + leftRightSpace + 1 >= screnWidth)
        {
            x = 0;
            y = y + height + hSpace;
        }
        
        view.layer.borderWidth = 1;
        view.layer.borderColor = [UIColor grayColor].CGColor;
        [view viewAddViewFromname:arrName[i]];
        [view.button addTarget:self action:@selector(clickSender:) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:view];
        [arr addObject:view];
    }
    return arr;
}

/**  点击方法  */
- (void)clickSender:(UIButton *)sender
{
    NSInteger index = [self.arrview indexOfObject:sender.superview];
    
    if (_isAdd == NO)
    {
        if (index == self.selArr.count)
        {
            [self.selArr addObject:self.arrName[index]];
             _isAdd = YES;
        }
        self.selArr = _selArr;
    }
}

/**  刷新  */
- (void)setSelArr:(NSMutableArray *)selArr
{
    _selArr = selArr;
    for (int i = 0; i< self.arrview.count; i++)
    {
        BackView *back  = self.arrview[i];
        if (_selArr.count-1 >= i)
        {
            back.button.selected = YES;
            back.button.userInteractionEnabled = NO;
            back.label2.text = @"✨领奖✨";
        }
        else
        {
            back.button.selected = NO;
            back.button.userInteractionEnabled = YES;
            back.label2.text = @"✨";
        }
    }
}

- (NSMutableArray *)arrview{
    if (!_arrview) {
        _arrview = [NSMutableArray array];
    }
    return _arrview;
}

@end


@implementation BackView

-(void)viewAddViewFromname:(NSString *)name
{
    UILabel *label = [UILabel new];
    label.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height/5 * 3);
    
    label.font = [UIFont systemFontOfSize:18];
    label.text = name;
    [label setTextAlignment:(NSTextAlignmentCenter)];
    [self addSubview:label];
    
    self.label =label;
    
    UILabel *label2 = [UILabel new];
    label2.frame = CGRectMake(0, self.frame.size.height/5 * 3, self.frame.size.width, self.frame.size.height/5 * 2);
    label2.text = @"领奖";
    label2.font = [UIFont systemFontOfSize:17];
    
    label2.layer.cornerRadius = 10;
    label2.layer.masksToBounds = YES;
    [label2 setTextAlignment:(NSTextAlignmentCenter)];
    [self addSubview:label2];
    
    self.label2 = label2;
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = self.bounds;
    [button setTitle:@"" forState:(UIControlStateNormal)];
    [button setTitle:@"✔" forState:(UIControlStateSelected)];
    button.selected = NO;
  
    [self addSubview:button];
    self.button = button;
}

@end

 

// 调用
#import "ViewController.h"
#import "ButtonView.h"
@interface ViewController ()


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *arr = @[@"第一天",@"第二天",@"第三天",@"第四天",@"第五天",@"第六天",@"第七天"];
    NSArray *selArr = @[@"第一天",@"第二天",@"第三天",@"第四天"];
    ButtonView *view = [[ButtonView alloc]initWithFrame:CGRectMake(0, 88, CGRectGetWidth([UIScreen mainScreen].bounds), 200)];
    view.arrName = arr;
    view.selArr = selArr.mutableCopy;
    [self.view addSubview:view];
}

@end

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值