直播点赞,上升的动画-- CAKeyFrameAnimation
//
// ViewController.m
// DMHeartFlyAnimation
//
// Created by Rick on 16/3/9.
// Copyright © 2016年 Rick. All rights reserved.
//
#import "ViewController.h"
#import "DMHeartFlyView.h"
@interface ViewController ()
{
CGFloat _heartSize;
NSTimer *_burstTimer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_heartSize = 36;
self.view.backgroundColor = [UIColor lightGrayColor];
self.view.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showTheLove)];
[self.view addGestureRecognizer:tapGesture];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)];
longPressGesture.minimumPressDuration = 0.2;
[self.view addGestureRecognizer:longPressGesture];
}
//点击
-(void)showTheLove{
DMHeartFlyView* heart = [[DMHeartFlyView alloc]initWithFrame:CGRectMake(0, 0, _heartSize, _heartSize)];
[self.view addSubview:heart];
CGPoint fountainSource = CGPointMake(20 + _heartSize/2.0, self.view.bounds.size.height - _heartSize/2.0 - 10);
heart.center = fountainSource;//创建的地方
[heart animateInView:self.view];
}
//长按
-(void)longPressGesture:(UILongPressGestureRecognizer *)longPressGesture{
switch (longPressGesture.state) {
case UIGestureRecognizerStateBegan:
_burstTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(showTheLove) userInfo:nil repeats:YES];
break;
case UIGestureRecognizerStateEnded:
[_burstTimer invalidate];
_burstTimer = nil;
break;
default:
break;
}
}
@end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// DMHeartFlyView.h
// DMHeartFlyAnimation
//
// Created by Rick on 16/3/9.
// Copyright © 2016年 Rick. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DMHeartFlyView : UIView
-(void)animateInView:(UIView *)view;
@end
//
// DMHeartFlyView.m
// DMHeartFlyAnimation
//
// Created by Rick on 16/3/9.
// Copyright © 2016年 Rick. All rights reserved.
//
#define DMRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define DMRGBAColor(r, g, b ,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
#define DMRandColor DMRGBColor(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))
#import "DMHeartFlyView.h"
//@interface DMHeartTheme ()
//
//@end
//
//@implementation DMHeartTheme
//
//@end
@interface DMHeartFlyView ()
@property(nonatomic,strong) UIColor *strokeColor;
@property(nonatomic,strong) UIColor *fillColor;
@end
@implementation DMHeartFlyView
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
_strokeColor = [UIColor whiteColor];
_fillColor = DMRandColor;
self.backgroundColor = [UIColor clearColor];
self.layer.anchorPoint = CGPointMake(0.5, 1);
}
return self;
}
static CGFloat PI = M_PI;
-(void)animateInView:(UIView *)view{
NSTimeInterval totalAnimationDuration = 6;
CGFloat heartSize = CGRectGetWidth(self.bounds);
CGFloat heartCenterX = self.center.x;
CGFloat viewHeight = CGRectGetHeight(view.bounds);
//Pre-Animation setup
self.transform = CGAffineTransformMakeScale(0, 0);
self.alpha = 0;
//Bloom弹簧效果
[UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.6 initialSpringVelocity:0.8 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.transform = CGAffineTransformIdentity;
self.alpha = 0.9;
} completion:NULL];
NSInteger i = arc4random_uniform(2);
NSInteger rotationDirection = 1- (2*i);// -1 OR 1
NSInteger rotationFraction = arc4random_uniform(10);
[UIView animateWithDuration:totalAnimationDuration animations:^{
self.transform = CGAffineTransformMakeRotation(rotationDirection * PI/(16 + rotationFraction*0.2));
} completion:NULL];
//上升路径
UIBezierPath *heartTravelPath = [UIBezierPath bezierPath];
[heartTravelPath moveToPoint:self.center];
//random end point
CGPoint endPoint = CGPointMake(heartCenterX + (rotationDirection) * arc4random_uniform(2*heartSize), viewHeight/6.0 + arc4random_uniform(viewHeight/4.0));
//random Control Points
NSInteger j = arc4random_uniform(2);
NSInteger travelDirection = 1- (2*j);// -1 OR 1
//randomize x and y for control points
CGFloat xDelta = (heartSize/2.0 + arc4random_uniform(2*heartSize)) * travelDirection;
CGFloat yDelta = MAX(endPoint.y ,MAX(arc4random_uniform(8*heartSize), heartSize));
CGPoint controlPoint1 = CGPointMake(heartCenterX + xDelta, viewHeight - yDelta);
CGPoint controlPoint2 = CGPointMake(heartCenterX - 2*xDelta, yDelta);
//上升的曲线
[heartTravelPath addCurveToPoint:endPoint controlPoint1:controlPoint1 controlPoint2:controlPoint2];
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyFrameAnimation.path = heartTravelPath.CGPath;
keyFrameAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
keyFrameAnimation.duration = totalAnimationDuration + endPoint.y/viewHeight;
[self.layer addAnimation:keyFrameAnimation forKey:@"positionOnPath"];
//Alpha & remove from superview
[UIView animateWithDuration:totalAnimationDuration animations:^{
self.alpha = 0.0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
-(void)drawRect:(CGRect)rect{
// UIImage *heartImage = [UIImage imageNamed:@"heart"];
// UIImage *heartImageBorder = [UIImage imageNamed:@"heartBorder"];
//
// //Draw background image (mimics border)
// UIGraphicsBeginImageContextWithOptions(heartImageBorder.size, NO, 0.0f);
// [_strokeColor setFill];
// CGRect bounds = CGRectMake(0, 0, heartImageBorder.size.width, heartImageBorder.size.height);
// UIRectFill(bounds);
// [heartImageBorder drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
// heartImageBorder = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
//
// //Draw foreground heart image
// UIGraphicsBeginImageContextWithOptions(heartImage.size, NO, 0.0f);
// [_fillColor setFill];
// CGRect bounds1 = CGRectMake(0, 0, heartImage.size.width, heartImage.size.height);
// UIRectFill(bounds1);
// [heartImage drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
// heartImage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
[self drawHeartInRect:rect];
}
//画心
-(void)drawHeartInRect:(CGRect)rect{
[_strokeColor setStroke];
[_fillColor setFill];
CGFloat drawingPadding = 4.0;
CGFloat curveRadius = floor((CGRectGetWidth(rect) - 2*drawingPadding) / 4.0);
//Creat path
UIBezierPath *heartPath = [UIBezierPath bezierPath];
//1.Start at bottom heart tip// floor()向下取整
CGPoint tipLocation = CGPointMake(floor(CGRectGetWidth(rect) / 2.0), CGRectGetHeight(rect) - drawingPadding);
[heartPath moveToPoint:tipLocation];//起点
//2.Move to top left start of curve
CGPoint topLeftCurveStart = CGPointMake(drawingPadding, floor(CGRectGetHeight(rect) / 2.4));
[heartPath addQuadCurveToPoint:topLeftCurveStart controlPoint:CGPointMake(topLeftCurveStart.x, topLeftCurveStart.y + curveRadius)];////画二元曲线,一般和moveToPoint配合使用
//3.Create top left curve
[heartPath addArcWithCenter:CGPointMake(topLeftCurveStart.x + curveRadius, topLeftCurveStart.y) radius:curveRadius startAngle:PI endAngle:0 clockwise:YES];
//4.Create top right curve
CGPoint topRightCurveStart = CGPointMake(topLeftCurveStart.x + 2*curveRadius, topLeftCurveStart.y);
[heartPath addArcWithCenter:CGPointMake(topRightCurveStart.x + curveRadius, topRightCurveStart.y) radius:curveRadius startAngle:PI endAngle:0 clockwise:YES];
//5.Final curve to bottom heart tip
CGPoint topRightCurveEnd = CGPointMake(topLeftCurveStart.x + 4*curveRadius, topRightCurveStart.y);
[heartPath addQuadCurveToPoint:tipLocation controlPoint:CGPointMake(topRightCurveEnd.x, topRightCurveEnd.y + curveRadius)];
[heartPath fill];
heartPath.lineWidth = 1;
heartPath.lineCapStyle = kCGLineCapRound;
heartPath.lineJoinStyle = kCGLineCapRound;
[heartPath stroke];
}
@end
直播点赞,上升的动画-- CAKeyFrameAnimation的更多相关文章
- iOS:核心动画之关键帧动画CAKeyframeAnimation
CAKeyframeAnimation——关键帧动画 关键帧动画,也是CAPropertyAnimation的子类,与CABasicAnimation的区别是: –CABasicAnimation只能 ...
- 放yy直播点赞动画
最近在做直播相关的东西,这个动画是IOS先撸出来的,后来android这边要模仿,大部分直播应用都有很炫酷的点赞动画,所以也没什么好稀奇的.如果有现成的轮子了,就没必要自己再造了,后来参照了程序亦非猿 ...
- iOS直播点赞动画,iOS直播心型点赞动画
https://github.com/songxing10000/LikeAnimation-PraiseAnimation
- 核心动画 - CAKeyframeAnimation 简单应用
核心动画: 登录按钮的抖动效果: CAKeyframeAnimation * kfAnimation = [CAKeyframeAnimation animationWithKeyPath:@&quo ...
- 从QQ音乐开发,探讨如何利用腾讯云SDK在直播中加入视频动画
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯游戏云发表于云+社区专栏 看着精彩的德甲赛事,突然裁判一声口哨,球赛断掉了,屏幕开始自动播放"吃麦趣鸡盒,看德甲比赛&q ...
- iOS动画:CAKeyframeAnimation
网络中Core Animation类的继承关系图 属性简介 @interface CAKeyframeAnimation : CAPropertyAnimation /* 提供关键帧数据的 ...
- Android 开发技术周报 Issue#273
新闻/News Android 11有新玩法:双击手机背部截屏/进入多任务界面 Android 11 DP2证实了类似AirDrop的附近文件分享功能 谷歌发布Camera Go:即使入门机也能有出色 ...
- 核心动画(CAKeyframeAnimation)
Main.storyboard ViewController.m // // ViewController.m // 8A02.核心动画 - CAKeyframeAnimation // // ...
- 核心动画(CAKeyframeAnimation,CABasicAnimation)
一,核心动画常用的三种例子 view的核心动画其体现就是把view按照指定好的路径进行运动,针对的是view的整体. [view.layer addAnimation:动画路径 forKey:@“绑定 ...
随机推荐
- [更新中]【fit-flow使用总结】djang开发中git flow使用总结
djang开发中git flow使用总结 初次接触可以先看看此链接上关于git flow的东西http://danielkummer.github.io/git-flow-cheatsheet/ind ...
- 2016.8.17服务器端数据库用户导入导出方法 expdp和impdp
EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用. IMP只适用于EXP导出的 ...
- Diag:Diagonal matrices and diagonals of a matrix
Diag:Diagonal matrices and diagonals of a matrix Syntax X = diag(v,k) X = diag(v) v = diag(X,k) v = ...
- [mpm_winnt:error] [pid 28120:tid 15980] (OS 10038)在一个非套接字上尝试了一个操作。 : AH00332: winnt_accept: getsockname error on listening socket, is IPv6 available?
解决办法一: 可能是安装了某些程序修改了Winsock,使用netsh winsock reset 命令修复Winsock重启计算机即可! 解决办法二: 在httpd.conf文件中添加 Win32D ...
- CentOS和Ubuntu系统下安装vsftp(助推大数据部署搭建)
不多说,直接上干货! 同时,声明,我这里安装的vsftp,仅仅只为我的大数据着想,关于网上的复杂安装,那是服务和运维那块.我不多牵扯,也不多赘述. 一.CentOS系统里安装vsftp 第一步:使用y ...
- 关联查询 join的使用
#!/usr/bin/env python import sqlalchemy from sqlalchemy import create_engine from sqlalchemy.ext.dec ...
- session和cookie个字消除的方法
session消除的方法就是: session_destroy(); cookie消除的方法就是setcookie()函数的时间设为当前时间即可 if(isset($_COOKIE['adminId' ...
- Struts2框架02 消息传递
1 消息传递 浏览器和服务器之间的数据传递 2 服务器项浏览器发送数据 2.1 在控制器类中增加一个属性,该属性的值就是服务器需要向浏览器发送的数据 2.2 为该属性增加 get 方法 2.3 在处理 ...
- C++获取电脑上连接的多个摄像头名称与编号
#include<iostream>#include "strmif.h"#include <initguid.h>#include<vector&g ...
- Python Matplotlib.plot Update image Questions
1. 最近在测试一款设备,采集了一些设备后需要一帧一帧显示图像,经常使用Python,所以选用了Matplotlib进行图像操作 数据结构: timesatamp polar_distance hor ...