#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=@[@"日用百貨",@"家用電器",@"數(shù)字鄉(xiāng)鎮(zhèn)",@"綠色食品",@"女士專區(qū)",@"男士專區(qū)"];
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];
}
// 以后如果通過代碼設(shè)置子控件的位置,一般都是在layoutSubviews里面去寫
//layoutSubviews :只要父控件的frame一改變就會(huì)調(diào)用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
147A547F-8349-4E9E-8B4D-141562FA41FC.png
3F62D8B1-132C-4238-BF25-A8D633C00334.png
EF2B24C9-5094-4DE2-8BAD-9DEB4D58E2EE.png
FF34014B-F4AB-408D-9196-D1CBC5D9A5FC.png
EC47633A-FE59-459B-B4E7-3B74B06C944F.png