客戶端代碼
- 點擊小紅心祥款,發(fā)送socket給服務器,并且要傳遞房間Key給服務器,通知給哪個主播點贊,就能傳入到對應的分組socket中
- 怎么傳遞房間key扫尖,房間Key在主播界面悴品,一般一個客戶端禀综,只會產(chǎn)生一個房間,可以記錄到socket對象中
- 業(yè)務邏輯:用戶點擊小紅心,小紅心就會往上慢慢飄苔严。
- 實現(xiàn)原理:其實就是一個動畫定枷。
- 怎么實現(xiàn):用UIView做不了,因為小紅心是不規(guī)則的左右擺動届氢,慢慢上去的欠窒。
- 可以使用核心動畫(創(chuàng)建CALayer),CABasicAnimation和CAKeyframeAnimation,放在一個group組中退子。
- CABasicAnimation:漸漸顯示動畫岖妄,修改透明度
- CAKeyframeAnimation:做路徑動畫,描述小紅心的路徑寂祥,然后按照這個路徑走.
- 描述一根線衣吠,x從寬度中獲取隨機值,y值每次減減
- 動畫完成壤靶,記得移除缚俏,可以用動畫事務類,監(jiān)聽動畫是否完成,代碼一定要放在最前面
XMGLiveOverlayViewController.m
- (IBAction)clickUpvote:(id)sender {
// 發(fā)送點贊事件
[[SocketIOClient clientSocket] emit:@"upvote" with:@[[SocketIOClient clientSocket].roomKey]];
}
XMGUpVoteViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[SocketIOClient clientSocket] on:@"upvote" callback:^(NSArray * _Nonnull data, SocketAckEmitter * _Nonnull ack) {
// 監(jiān)聽到點贊,進行點贊動畫
[self setupVoteLayer];
}];
}
- (void)setupVoteLayer
{
CALayer *layer = [CALayer layer];
layer.contents = (id)[UIImage imageNamed:@"hearts (1)"].CGImage;
[self.view.layer addSublayer:layer];
layer.bounds = CGRectMake(0, 0, 30, 30);
layer.position = CGPointMake(self.view.width * 0.5, self.view.height);
[self setupAnim:layer];
}
- (void)setupAnim:(CALayer *)layer
{
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[layer removeAllAnimations];
[layer removeFromSuperlayer];
}];
// 創(chuàng)建basic動畫
CABasicAnimation *alphaAnim = [CABasicAnimation animation];
alphaAnim.keyPath = @"alpha";
alphaAnim.fromValue = @0;
alphaAnim.toValue = @1;
// 路徑動畫
CAKeyframeAnimation *pathAnim = [CAKeyframeAnimation animation];
pathAnim.keyPath = @"position";
pathAnim.path = [self animPath].CGPath;
// 創(chuàng)建動畫組
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[alphaAnim,pathAnim];
group.duration = 5;
[layer addAnimation:group forKey:nil];
[CATransaction commit];
}
- (UIBezierPath *)animPath
{
UIBezierPath *path = [UIBezierPath bezierPath];
CGFloat y = self.view.height;
CGFloat x = self.view.width * 0.5;
while (y > 0) {
x = arc4random_uniform(self.view.width - 20) + 20;
if (y == self.view.height) {
[path moveToPoint:CGPointMake(x, y)];
} else {
[path addLineToPoint:CGPointMake(x, y)];
}
y -= 20;
}
return path;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者