美文网首页
按钮 跳动和点击动画

按钮 跳动和点击动画

作者: Areyouhere | 来源:发表于2017-03-09 14:26 被阅读0次
#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
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

相关文章

网友评论

      本文标题:按钮 跳动和点击动画

      本文链接:https://www.haomeiwen.com/subject/rxiwgttx.html