PresentView.m
4.82 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
140
141
//
// PresentView.m
// presentAnimation
//
// Created by 许博 on 16/7/14.
// Copyright © 2016年 许博. All rights reserved.
//
#import "PresentView.h"
#import "ShakeLabel.h"
@interface PresentView ()
{
NSTimer *timer;
NSInteger count;
NSInteger beforeNumber;
UIImageView *giftImageView;
}
@property (nonatomic,strong) UIImageView *headImageView; // 头像
//@property (nonatomic,strong) UIImageView *giftImageView; // 礼物
@property (nonatomic,strong) UILabel *nameLabel; // 送礼物者
@property (nonatomic,strong) UILabel *giftLabel; // 礼物名称
@property (nonatomic,strong) ShakeLabel *skLabel;
@property (nonatomic,strong) UIImageView *bgImageView;
@end
@implementation PresentView
- (void)didMoveToSuperview{
CGRect rect = self.frame;
rect.origin.x = 0;
[UIView animateWithDuration:0.5 animations:^{
self.frame = rect;
}];
}
#pragma mark 初始化 UI
- (void)setUI{
_bgImageView = [[UIImageView alloc] init];
_bgImageView.backgroundColor = [UIColor blackColor];
_bgImageView.alpha = 0.3;
_bgImageView.frame = self.bounds;
_bgImageView.layer.cornerRadius = self.frame.size.height / 2;
_bgImageView.layer.masksToBounds = YES;
[self addSubview:_bgImageView];
_headImageView = [[UIImageView alloc] init];
_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 = self.frame.size.height / 2;
_headImageView.layer.masksToBounds = YES;
[self addSubview:_headImageView];
giftImageView = [[UIImageView alloc] init];
// giftImageView.backgroundColor = [UIColor redColor];
giftImageView.frame = CGRectMake(self.frame.size.width - 50, self.frame.size.height - 50, 50, 50);
[self addSubview:giftImageView];
_nameLabel = [[UILabel alloc] init];
_nameLabel.frame = CGRectMake(_headImageView.frame.size.width + 5, 5, _headImageView.frame.size.width * 3, 10);
_nameLabel.textColor = [UIColor whiteColor];
_nameLabel.font = [UIFont systemFontOfSize:13];
[self addSubview:_nameLabel];
_giftLabel = [[UILabel alloc] init];
_giftLabel.frame = CGRectMake(_nameLabel.frame.origin.x, CGRectGetMaxY(_headImageView.frame) - 10 - 5, _nameLabel.frame.size.width, 10);
_giftLabel.textColor = [UIColor IVTLightRedColor];
_giftLabel.font = [UIFont systemFontOfSize:13];
[self addSubview:_giftLabel];
// 初始化动画label
_skLabel = [[ShakeLabel alloc] init];
_skLabel.frame = CGRectMake(CGRectGetWidth(self.frame) ,-10, 50, 30);
_skLabel.font = [UIFont systemFontOfSize:14];
_skLabel.borderColor = [UIColor IVTLightRedColor];
_skLabel.textColor = [UIColor IVTLightRedColor];
_skLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:_skLabel];
}
- (void)setModel:(GiftModel *)model {
_model = model;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
timer = [NSTimer timerWithTimeInterval:0.4 target:self selector:@selector(doAnimate) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] run];
[timer fire];
});
count = 0;
[giftImageView sd_setImageWithURL:[NSURL URLWithString:[model.giftImageUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
[_headImageView sd_setImageWithURL:[NSURL URLWithString:model.headImageUrl] placeholderImage:[UIImage imageNamed:@"占位头像"]];
_nameLabel.text = model.userName;
_giftLabel.text = model.giftName;
}
- (void)doAnimate{
NSLog(@"++++++----********=%ld %ld",count,_model.totalGiftNumber);
count++;
if (count -_model.totalGiftNumber >= 3) {//消失
[timer invalidate];
timer = nil;
if (self.delegate && [self.delegate respondsToSelector:@selector(stopShowView:)]) {
[self.delegate stopShowView:self];
}
}
else{
if (beforeNumber != _model.totalGiftNumber) {
count = _model.totalGiftNumber;
_skLabel.text = [NSString stringWithFormat:@"×%ld",(long)_model.totalGiftNumber];
[_skLabel startAnimWithDuration:0.4];
}
beforeNumber = _model.totalGiftNumber;
}
// if (_model.totalGiftNumber - beforeNumber >=10) {
// count = _model.totalGiftNumber;
// }
// if (count <= _model.totalGiftNumber) {
// count = _model.totalGiftNumber;
// _skLabel.text = [NSString stringWithFormat:@"%ld",(long)count];
// [_skLabel startAnimWithDuration:0.4];
// }
// else if (count == _model.totalGiftNumber + 5){
// [timer invalidate];
// timer = nil;
// if (_block) {
// self.block();
// }
// }
}
@end