//
// ViewController.m
// PeriscopeHeartAnimation
//
// Created by ldj on 4/28/15.
// Copyright (c) 2015 ldj. All rights reserved.
// #import "ViewController.h" #define kScreenWidth [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addHeart)];
[self.view addGestureRecognizer:tap];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} - (void)addHeart {
UIImageView *heartImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth / 2.0 - 14, kScreenHeight - 100, 28, 26)]; heartImageView.image = [UIImage imageNamed:@"heart"];
heartImageView.transform = CGAffineTransformMakeScale(0, 0);
[self.view addSubview:heartImageView]; CGFloat duration = 5 + (arc4random() % 5 - 2);
[UIView animateWithDuration:0.3 animations:^{
heartImageView.transform = CGAffineTransformMakeScale(1, 1);
heartImageView.transform = CGAffineTransformMakeRotation(-0.01 * (arc4random() % 20));
}];
[UIView animateWithDuration:duration animations:^{
heartImageView.alpha = 0;
}];
CAKeyframeAnimation *animation = [self createAnimation:heartImageView.frame];
animation.duration = duration;
[heartImageView.layer addAnimation:animation forKey:@"position"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((duration + 0.5) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[heartImageView removeFromSuperview];
});
} - (CAKeyframeAnimation *)createAnimation:(CGRect)frame {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef path = CGPathCreateMutable(); int height = -100 + arc4random() % 40 - 20;
int xOffset = frame.origin.x;
int yOffset = frame.origin.y;
int waveWidth = 50;
CGPoint p1 = CGPointMake(xOffset, height * 0 + yOffset);
CGPoint p2 = CGPointMake(xOffset, height * 1 + yOffset);
CGPoint p3 = CGPointMake(xOffset, height * 2 + yOffset);
CGPoint p4 = CGPointMake(xOffset, height * 2 + yOffset); CGPathMoveToPoint(path, NULL, p1.x,p1.y); if (arc4random() % 2) {
CGPathAddQuadCurveToPoint(path, NULL, p1.x - arc4random() % waveWidth, p1.y + height / 2.0, p2.x, p2.y);
CGPathAddQuadCurveToPoint(path, NULL, p2.x + arc4random() % waveWidth, p2.y + height / 2.0, p3.x, p3.y);
CGPathAddQuadCurveToPoint(path, NULL, p3.x - arc4random() % waveWidth, p3.y + height / 2.0, p4.x, p4.y);
} else {
CGPathAddQuadCurveToPoint(path, NULL, p1.x + arc4random() % waveWidth, p1.y + height / 2.0, p2.x, p2.y);
CGPathAddQuadCurveToPoint(path, NULL, p2.x - arc4random() % waveWidth, p2.y + height / 2.0, p3.x, p3.y);
CGPathAddQuadCurveToPoint(path, NULL, p3.x + arc4random() % waveWidth, p3.y + height / 2.0, p4.x, p4.y);
}
animation.path = path;
animation.calculationMode = kCAAnimationCubicPaced;
CGPathRelease(path);
return animation;
} @end

PeriscopeHeartAnimation的更多相关文章

随机推荐

  1. Python实现图片的base64编码

    import base64 if __name__ == "__main__": dir='image.jpg' basef=open(dir.split('.')[0]+'_ba ...

  2. [browser navigator 之plugins] 写了一个检测游览器插件

    检测IE插件 function hasIEPlugin(name){ try{ new ActiveXObject(name); return true; }catch(ex){ return fal ...

  3. docker in docker 的启动方式

    --privileged  -v /var/run/docker.sock:/var/run/docker.sock  -v $(which docker):/bin/docker 启动容器时添加上面 ...

  4. elasticsearch安装与使用

    一.windows10上安装elasticsearch Elasticsearch 需要 Java环境,在安装Elasticsearch之前先安装好JDK. 本文安装jdk1.8,es6.3.2为例. ...

  5. linux系统修改用户密码报错

    版权声明:本文为博主原创文章,支持原创,转载请附上原文出处链接和本声明. 本文地址:https://www.cnblogs.com/wannengachao/p/12069113.html 1.设置新 ...

  6. Leetcode 1239. 串联字符串的最大长度

    地址 https://leetcode-cn.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/s ...

  7. C缺陷与陷阱

    导读 词法"陷阱" 语法"陷阱" 语义"陷阱" 链接 库函数 预处理器 可移植性缺陷

  8. Global AI Bootcamp 2019 宁波站活动总结

    2019年12月14日,由微软MVP技术社区发起的Global AI Bootcamp 2019盛会在全球60多个国家130个城市点燃.在大中华区,本次活动由全国众多Azure专家及微软MVP技术社区 ...

  9. zabbix--完整安装攻略

    zabbix:是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管理员快速定位/解 ...

  10. 从零开始的微信小程序入门教程(一)

    之前说要和同事一起开发个微信小程序项目,现在也在界面设计,功能定位等需求上开始实施了.所以在还未正式写项目前,打算在空闲时间学习下小程序.本意是在学习过程中结合实践整理出一个较为入门且不是很厚的教程, ...