//
//  AddressItemView.m
//  Open
//
//  Created by 雷俊博 on 2019/12/13.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "AddressItemView.h"
#import "UIView+frameAdjust.h"

static  CGFloat  const  HYBarItemMargin = 20;

@interface AddressItemView ()

@property (nonatomic,strong) NSMutableArray * btnArray;



@end

@implementation AddressItemView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView.frame = self.bounds;
        [self addSubview:self.scrollView];
    }
    return self;
}

- (void)layoutSubviews{
   
    [super layoutSubviews];
    
    for (NSInteger i = 0; i <= self.btnArray.count - 1 ; i++) {
        
        UIView * view = self.btnArray[i];
        if (i == 0) {
            view.x = HYBarItemMargin;
        }
        if (i > 0) {
            UIView * preView = self.btnArray[i - 1];
            view.x = HYBarItemMargin  + preView.maxX;
        }
      
    }
}

#pragma mark - getters and setters
- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectZero];
    }
    return _scrollView;
}


- (NSMutableArray *)btnArray{
    
    CGFloat contentW = 0.0;
    
    NSMutableArray * mArray  = [NSMutableArray array];
    for (UIView * view in self.scrollView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [mArray addObject:view];
            contentW = contentW + view.bounds.size.width + HYBarItemMargin;
        }
    }
    _btnArray = mArray;
    self.scrollView.contentSize = CGSizeMake(contentW, self.bounds.size.height);
    return _btnArray;
}

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

@end
