PresentView.m
4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// PresentView.m
// presentAnimation
//
// Created by 许博 on 16/7/14.
// Copyright © 2016年 许博. All rights reserved.
//
#import "PresentView.h"
#import "UIImageView+WebCache.h"
@interface PresentView ()
@property (nonatomic,strong) UIImageView *bgImageView;
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,copy) void(^completeBlock)(BOOL finished,NSInteger finishCount,NSInteger finishGiftCount); // 新增了回调参数 finishCount, 用来记录动画结束时累加数量,将来在3秒内,还能继续累加
@end
@implementation PresentView
// 根据礼物个数播放动画
- (void)animateWithCompleteBlock:(completeBlock)completed
{
[UIView animateWithDuration:0.3 animations:^{
self.frame = CGRectMake(0, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
} completion:^(BOOL finished) {
[self shakeNumberLabel];
}];
self.completeBlock = completed;
}
- (void)shakeNumberLabel{
if (self.giftCount == 10) {
_animCount += _giftCount;
} else {
_animCount ++;
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hidePresendView) object:nil];//可以取消成功。
[self performSelector:@selector(hidePresendView) withObject:nil afterDelay:2];
self.skLabel.text = [NSString stringWithFormat:@"X %ld",_animCount];
[self.skLabel startAnimWithDuration:0.3];
}
- (void)hidePresendView
{
[UIView animateWithDuration:0.30 delay:2 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.frame = CGRectMake(0, self.frame.origin.y - 20, self.frame.size.width, self.frame.size.height);
self.alpha = 0;
} completion:^(BOOL finished) {
[self reset];
if (self.completeBlock) {
self.completeBlock(finished,_animCount,_finishGiftCount);
}
_finished = finished;
[self removeFromSuperview];
}];
}
// 重置
- (void)reset {
self.frame = _originFrame;
self.alpha = 1;
self.animCount = 0;
self.skLabel.text = @"";
}
- (instancetype)init {
if (self = [super init]) {
_originFrame = self.frame;
[self setUI];
}
return self;
}
#pragma mark 布局 UI
- (void)layoutSubviews {
[super layoutSubviews];
_headImageView.frame = CGRectMake(0, 0, self.frame.size.height, self.frame.size.height);
_headImageView.layer.borderWidth = 1;
_headImageView.layer.borderColor = [UIColor cyanColor].CGColor;
_headImageView.layer.cornerRadius = _headImageView.frame.size.height / 2;
_headImageView.layer.masksToBounds = YES;
_giftImageView.frame = CGRectMake(self.frame.size.width - 30, self.frame.size.height - 50, 50, 50);
_nameLabel.frame = CGRectMake(_headImageView.frame.size.width + 5, 5, _headImageView.frame.size.width * 3, 10);
_giftLabel.frame = CGRectMake(_nameLabel.frame.origin.x, CGRectGetMaxY(_headImageView.frame) - 10 - 5, _nameLabel.frame.size.width, 10);
_bgImageView.frame = self.bounds;
_bgImageView.layer.cornerRadius = self.frame.size.height / 2;
_bgImageView.layer.masksToBounds = YES;
_skLabel.frame = CGRectMake(CGRectGetMaxX(self.frame) + 5,-20, 70, 30);
}
#pragma mark 初始化 UI
- (void)setUI {
_bgImageView = [[UIImageView alloc] init];
_bgImageView.backgroundColor = [UIColor blackColor];
_bgImageView.alpha = 0.3;
_headImageView = [[UIImageView alloc] init];
_giftImageView = [[UIImageView alloc] init];
_nameLabel = [[UILabel alloc] init];
_giftLabel = [[UILabel alloc] init];
_nameLabel.textColor = [UIColor whiteColor];
_nameLabel.font = [UIFont systemFontOfSize:13];
_giftLabel.textColor = [UIColor yellowColor];
_giftLabel.font = [UIFont systemFontOfSize:13];
// 初始化动画label
_skLabel = [[ShakeLabel alloc] init];
_skLabel.font = [UIFont systemFontOfSize:16];
_skLabel.borderColor = [UIColor redColor];
_skLabel.textColor = [UIColor redColor];
_skLabel.textAlignment = NSTextAlignmentCenter;
_animCount = 0;
[self addSubview:_bgImageView];
[self addSubview:_headImageView];
[self addSubview:_giftImageView];
[self addSubview:_nameLabel];
[self addSubview:_giftLabel];
[self addSubview:_skLabel];
}
- (void)setModel:(GiftModel *)model {
_model = model;
[_giftImageView sd_setImageWithURL:[NSURL URLWithString:[model.giftImage stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
[_headImageView sd_setImageWithURL:[NSURL URLWithString:model.headImage]];
_nameLabel.text = [model.name base64DecodedToString];
_giftLabel.text = [NSString stringWithFormat:@"送了%@",model.giftName];
_giftCount = model.giftCount;
}
@end