//
// FWEffectBar.m
// FWMeituApp
//
// Created by ForrestWoo on 15-9-23.
// Copyright (c) 2015年 ForrestWoo co,.ltd. All rights reserved.
//
#import "FWEffectBar.h"
#import "FWEffectBarItem.h"
@interface FWEffectBar ()
@property (nonatomic) CGFloat itemWidth;
@end
@implementation FWEffectBar
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
}
return self;
}
- (id)init {
return [self initWithFrame:CGRectZero];
}
- (void)layoutSubviews {
CGSize frameSize = self.frame.size;
CGFloat minimumContentHeight = [self minimumContentHeight];
[self setItemWidth:roundf((frameSize.width - [self contentEdgeInsets].left -
[self contentEdgeInsets].right) / [[self items] count])];
NSInteger index = 0;
// Layout items
for (FWEffectBarItem *item in [self items]) {
CGFloat itemHeight = [item itemHeight];
if (!itemHeight) {
itemHeight = frameSize.height;
}
[item setFrame:CGRectMake(self.contentEdgeInsets.left + (index * self.itemWidth),
roundf(frameSize.height - itemHeight) - self.contentEdgeInsets.top,
self.itemWidth, itemHeight - self.contentEdgeInsets.bottom)];
[item setNeedsDisplay];
index++;
}
}
#pragma mark - Configuration
- (void)setItemWidth:(CGFloat)itemWidth {
if (itemWidth > 0) {
_itemWidth = itemWidth;
}
}
- (void)setItems:(NSArray *)items {
for (FWEffectBarItem *item in _items) {
[item removeFromSuperview];
}
_items = [items copy];
for (FWEffectBarItem *item in _items) {
[item addTarget:self action:@selector(tabBarItemWasSelected:) forControlEvents:UIControlEventTouchDown];
[self addSubview:item];
}
}
- (void)setHeight:(CGFloat)height {
[self setFrame:CGRectMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame),
CGRectGetWidth(self.frame), height)];
}
- (CGFloat)minimumContentHeight {
CGFloat minimumTabBarContentHeight = CGRectGetHeight([self frame]);
for (FWEffectBarItem *item in [self items]) {
CGFloat itemHeight = [item itemHeight];
if (itemHeight && (itemHeight < minimumTabBarContentHeight)) {
minimumTabBarContentHeight = itemHeight;
}
}