#import "WatchViewController.h"
#import "StickButton.h"
@interface WatchViewController ()
@property (nonatomic ,assign) int btnIndex;
@property (nonatomic ,retain) NSMutableArray *itemButtons;
@property (nonatomic ,retain) NSTimer *timer;
@end
@implementation WatchViewController
- (NSMutableArray*)itemButtons
{
if (!_itemButtons)
{
_itemButtons = [NSMutableArray array];
}
return _itemButtons;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self setUpAllBtn];
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}
- (void)timeChange
{
if (_btnIndex == self.itemButtons.count)
{
[_timer invalidate];
return;
}
UIButton *button = self.itemButtons[_btnIndex];
[self setUpOneButtonAnimate:button];
_btnIndex ++;
}
- (void)setUpOneButtonAnimate:(UIButton *)button
{
[UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
button.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
}];
}
- (void)setUpAllBtn
{
int cols = 3;
int col = 0;
int row = 0;
CGFloat x = 0;
CGFloat y = 0;
CGFloat wh = 100;
CGFloat margin = ([UIScreen mainScreen].bounds.size.width - cols * wh)/(cols + 1);
CGFloat oriY = 300;
NSArray*titleArr=@[@"日用百货",@"家用电器",@"数字乡镇",@"绿色食品",@"女士专区",@"男士专区"];
for (int i=0; i<titleArr.count;i++)
{
StickButton *button = [StickButton buttonWithType:UIButtonTypeCustom];
col = i % cols;
row = i / cols;
x = margin + col * (margin + wh);
y = row * (margin + wh) + oriY;
// button.backgroundColor = [UIColor redColor];
button.frame = CGRectMake(x, y, wh, wh);
[button setImage:[UIImage imageNamed:titleArr[i]] forState:UIControlStateNormal];
[button setTitle:titleArr[i] forState:UIControlStateNormal];
button.transform = CGAffineTransformMakeTranslation(0, self.view.bounds.size.height);
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
[self.itemButtons addObject:button];
[self.view addSubview:button];
}
}
- (void)buttonClick:(UIButton *)button
{
[UIView animateWithDuration:0.5 animations:^{
button.transform = CGAffineTransformMakeScale(1.2, 1.2);
}];
}
自定义按钮
#import "StickButton.h"
@interface StickButton ()
@end
@implementation StickButton
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
[self setUp];
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self setUp];
}
- (void)setUp
{
self.imageView.contentMode = UIViewContentModeCenter;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.font = [UIFont systemFontOfSize:15];
}
// 以后如果通过代码设置子控件的位置,一般都是在layoutSubviews里面去写
//layoutSubviews :只要父控件的frame一改变就会调用layoutSubviews,重新布局子控件
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat W = self.bounds.size.width;
CGFloat imageH = self.bounds.size.height * 0.8;
self.imageView.frame = CGRectMake(0, 0, W, imageH);
CGFloat labelH = self.bounds.size.height - imageH;
self.titleLabel.frame = CGRectMake(0, imageH, W, labelH);
}
- (void)setHighlighted:(BOOL)highlighted
{
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[UIView animateWithDuration:0.5 animations:^{
self.transform = CGAffineTransformMakeScale(2.0, 2.0);
self.alpha = 0.0;
}];
}
@end