//
// 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

最新文章

  1. 用Appium去操作移动设备上的chrome
  2. 程序员定制的中州韵(rime)windows版(小狼毫)微软双拼输入法
  3. python练习程序(c100经典例17)
  4. uva 10056
  5. Asp.Net使用异步性能就提升吗
  6. mac IntelliJ Idea添加schema和dtd约束提示
  7. python 基于numpy的线性代数运算
  8. 12.13java过关测试
  9. MVVM Light 笔记
  10. IOS设计模式第十篇之命令行设计模式
  11. 洛谷P1841重要的城市
  12. Linux tar This does not look like a tar archive
  13. Tesseract-OCR-03-图片文字识别
  14. Access导入Sql 2008 R2 错误 0xc020801c
  15. AspectJ 是什么
  16. POJ1006Biorhythms——中国剩余定理
  17. BZOJ——1622: [Usaco2008 Open]Word Power 名字的能量
  18. [Python3网络爬虫开发实战] 1.7.3-Appium的安装
  19. java中TCP传输协议
  20. Nginx Cache-Control

热门文章

  1. 基于EF的一个简单实战型分层架构
  2. 【单条记录锁】select single for update
  3. viewPager作为RecyclerView的itemView出现的显示问题
  4. Python 定义常量
  5. MySQL 锁的监控及处理
  6. Linux:Apache服务器的搭建
  7. JVM 类的加载机制
  8. Codeforces Round #594 (Div. 2)
  9. SpringCloud学习笔记(二、SpringCloud Config)
  10. Python 生成器 (generator) & 迭代器 (iterator)