123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- //
- // DGThumbUpButton.m
- // DGThumbUpButton
- //
- // Created by Desgard_Duan on 16/6/9.
- // Copyright © 2016年 Desgard_Duan. All rights reserved.
- //
- #import "DGThumbUpButton.h"
- #import "DGExplodeAnimationView.h"
- @interface DGThumbUpButton()
- @property (strong, nonatomic) DGExplodeAnimationView *explodeAnimationView;
- @property (copy, nonatomic) void(^upInsideBlock)(BOOL selected);
- @end
- @implementation DGThumbUpButton
- - (void)awakeFromNib
- {
- [super awakeFromNib];
- [self setImage: [UIImage imageNamed: @"pond_praise_no"] forState: UIControlStateNormal];
- // _isSelected = NO;
- // if (_isSelected) {
- // [self setImage: [UIImage imageNamed: @"pond_praise_yes"] forState: UIControlStateNormal];
- // } else {
- // [self setImage: [UIImage imageNamed: @"pond_praise_no"] forState: UIControlStateNormal];
- // }
-
- [self addTarget: self
- action: @selector(clickButtonPress)
- forControlEvents: UIControlEventTouchUpInside];
-
- // self.backgroundColor = [UIColor redColor];
-
- [self insertSubview: self.explodeAnimationView atIndex: 0];
- }
- - (void)setIsSelected:(BOOL)isSelected {
- _isSelected = isSelected;
- if (isSelected) {
- [self setImage: [UIImage imageNamed: @"pond_praise_yes"] forState: UIControlStateNormal];
- } else {
- [self setImage: [UIImage imageNamed: @"pond_praise_no"] forState: UIControlStateNormal];
- }
- }
- #pragma mark - Freedom Initial
- - (instancetype) initWithFrame: (CGRect)frame isPress: (BOOL)press type: (DGThumbUpButtonType)type {
- self = [super initWithFrame: frame];
- self.isSelected = press;
- if (self.isSelected) {
- [self setImage: [UIImage imageNamed: @"pond_praise_yes"] forState: UIControlStateNormal];
- } else {
- [self setImage: [UIImage imageNamed: @"pond_praise_no"] forState: UIControlStateNormal];
- }
-
- [self addTarget: self
- action: @selector(clickButtonPress)
- forControlEvents: UIControlEventTouchUpInside];
-
- [self insertSubview: self.explodeAnimationView atIndex: 0];
-
- return self;
- }
- - (instancetype) initWithFrame: (CGRect)frame isPress: (BOOL)press {
- self = [self initWithFrame: frame
- isPress: press
- type: DGThumbUpExplosionType];
- return self;
- }
- #pragma mark - Override
- - (instancetype) initWithFrame: (CGRect) frame {
- self = [self initWithFrame: frame
- isPress: NO
- type: DGThumbUpExplosionType];
- return self;
- }
- - (instancetype) init {
- self = [self initWithFrame: CGRectMake(0, 0, 30, 30)
- isPress: NO
- type: DGThumbUpExplosionType];
- return self;
- }
- - (void) layoutSubviews {
- [super layoutSubviews];
- }
- #pragma mark - Methods
- - (void) clickButtonPress{
- if (self.isSelected) {
- [self popInsideWithDuration: 0.5];
- }
- else {
- [self popOutsideWithDuration: 0.5];
- if(self.isShowAnimation){
- [self.explodeAnimationView animate];
- }
- };
- }
- - (void) popOutsideWithDuration: (NSTimeInterval) duringTime {
- __weak typeof(self) weakSelf = self;
- self.transform = CGAffineTransformIdentity;
-
- [UIView animateKeyframesWithDuration: duringTime delay: 0 options: 0 animations: ^{
- [weakSelf setImage: [UIImage imageNamed: @"pond_praise_yes"] forState: UIControlStateNormal];
- [UIView addKeyframeWithRelativeStartTime: 0
- relativeDuration: 1 / 3.0
- animations: ^{
- typeof(self) strongSelf = weakSelf;
- strongSelf.transform = CGAffineTransformMakeRotation(-30 * (M_PI / 180));
- }];
- [UIView addKeyframeWithRelativeStartTime: 0
- relativeDuration: 1 / 3.0
- animations: ^{
- typeof(self) strongSelf = weakSelf;
- strongSelf.transform = CGAffineTransformMakeScale(1.5, 1.5);
- }];
- [UIView addKeyframeWithRelativeStartTime: 1 / 3.0
- relativeDuration: 1 / 3.0
- animations: ^{
- typeof(self) strongSelf = weakSelf;
- strongSelf.transform = CGAffineTransformMakeScale(0.8, 0.8);
- }];
- [UIView addKeyframeWithRelativeStartTime: 2 / 3.0
- relativeDuration: 1 / 3.0
- animations: ^{
- typeof(self) strongSelf = weakSelf;
- strongSelf.transform = CGAffineTransformMakeScale(1.0, 1.0);
- }];
- } completion: ^(BOOL finished) {
- self.isSelected = YES;
- if(self.upInsideBlock){
- self.upInsideBlock(self.isSelected);
- }
- }];
- }
- - (void) popInsideWithDuration: (NSTimeInterval) duringTime {
- __weak typeof(self) weakSelf = self;
- self.transform = CGAffineTransformIdentity;
-
- [UIView animateKeyframesWithDuration: duringTime delay: 0 options: 0 animations: ^{
- [weakSelf setImage: [UIImage imageNamed: @"pond_praise_no"] forState: UIControlStateNormal];
- [UIView addKeyframeWithRelativeStartTime: 0
- relativeDuration: 1 / 2.0
- animations: ^{
- typeof(self) strongSelf = weakSelf;
- strongSelf.transform = CGAffineTransformMakeScale(0.8, 0.8);
- }];
- [UIView addKeyframeWithRelativeStartTime: 1 / 2.0
- relativeDuration: 1 / 2.0
- animations: ^{
- typeof(self) strongSelf = weakSelf;
- strongSelf.transform = CGAffineTransformMakeScale(1.0, 1.0);
- }];
- } completion: ^(BOOL finished) {
- self.isSelected = NO;
- if(self.upInsideBlock){
- self.upInsideBlock(self.isSelected);
- }
- }];
- }
- #pragma mark - Lazy Init
- - (DGExplodeAnimationView *) explodeAnimationView {
- if (!_explodeAnimationView) {
- _explodeAnimationView = [[DGExplodeAnimationView alloc] initWithFrame: self.bounds];
- }
- return _explodeAnimationView;
- }
- -(void)setActionUpInside:(void (^)(BOOL))action{
- [self setUpInsideBlock:action];
- }
- @end
|