PeriscopeHeartAnimation
2024-09-06 08:52:59
//
// 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
最新文章
- 用Appium去操作移动设备上的chrome
- 程序员定制的中州韵(rime)windows版(小狼毫)微软双拼输入法
- python练习程序(c100经典例17)
- uva 10056
- Asp.Net使用异步性能就提升吗
- mac IntelliJ Idea添加schema和dtd约束提示
- python 基于numpy的线性代数运算
- 12.13java过关测试
- MVVM Light 笔记
- IOS设计模式第十篇之命令行设计模式
- 洛谷P1841重要的城市
- Linux tar This does not look like a tar archive
- Tesseract-OCR-03-图片文字识别
- Access导入Sql 2008 R2 错误 0xc020801c
- AspectJ 是什么
- POJ1006Biorhythms——中国剩余定理
- BZOJ——1622: [Usaco2008 Open]Word Power 名字的能量
- [Python3网络爬虫开发实战] 1.7.3-Appium的安装
- java中TCP传输协议
- Nginx Cache-Control
热门文章
- 基于EF的一个简单实战型分层架构
- 【单条记录锁】select single for update
- viewPager作为RecyclerView的itemView出现的显示问题
- Python 定义常量
- MySQL 锁的监控及处理
- Linux:Apache服务器的搭建
- JVM 类的加载机制
- Codeforces Round #594 (Div. 2)
- SpringCloud学习笔记(二、SpringCloud Config)
- Python 生成器 (generator) &; 迭代器 (iterator)