一个看着暖暖的心形小动画 简单操作 就能实现 送给另一个她一个小惊喜吧
一个UIView 用于布局
#import "XinView.h"
@implementation XinView
- (instancetype) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
开始触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self];
CALayer *caLayer = [CALayer layer];
caLayer.frame = CGRectMake(touchPoint.x - 1, touchPoint.y - 1, 10, 10);
// 边框宽度
caLayer.borderWidth = 0.2;
// 圆 5.0 相当于一个圆
caLayer.cornerRadius = 5.0;
// 给一个颜色
caLayer.borderColor = [UIColor redColor].CGColor;
[self.layer addSublayer:caLayer];
[self scaleBegin:caLayer];
}
移动 (点击鼠标左键拖动)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self];
CALayer *caLayer = [CALayer layer];
caLayer.frame = CGRectMake(touchPoint.x - 1, touchPoint.y - 1, 10, 10);
// 边框宽度
caLayer.borderWidth = 0.2;
// 圆 5.0 相当于一个圆
caLayer.cornerRadius = 5.0;
// 给一个颜色
caLayer.borderColor = [UIColor redColor].CGColor;
[self.layer addSublayer:caLayer];
[self scaleBegin:caLayer];
}
- (void)scaleBegin:(CALayer *)layer
{
// 最大比例 能扩大 多少倍
const float maxScale = 120.0;
/**
*
struct CATransform3D
{
CGFloat m11(x缩放), m12(y切变), m13(旋转), m14();
CGFloat m21(x切变), m22(y缩放), m23(), m24();
CGFloat m31(旋转), m32( ), m33(), m34(透视效果,要操作的这个对象要有旋转的角度,否则没有效果。正直/负值都有意义);
CGFloat m41(x平移), m42(y平移), m43(z平移), m44();
};
*/
if (layer.transform.m11 < maxScale) {
if (layer.transform.m11 == 1.0) {
// 1.1 1.1 1.0 点击后 圆可以扩大
[layer setTransform:CATransform3DMakeScale(1.1, 1.1, 1.0)];
}else {
// 1.1 1.1 1.0 点击后 圆可以扩大
[layer setTransform:CATransform3DScale(layer.transform, 1.1, 1.1, 1.0)];
}
// 圆放大的速度
[self performSelector:_cmd withObject:layer afterDelay:0.05];
} else {
[layer removeFromSuperlayer];
}
}
@end
回到控制器中
#import "ViewController.h"
#import "XinView.h"
@interface ViewController ()
@end
@implementation ViewController
布局
- (void)viewDidLoad {
XinView *xinV = [[XinView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:xinV];
[self herat];
[super viewDidLoad];
}
// 添加方法
- (void)herat
{
UIImageView *xinImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
xinImageView.image = [UIImage imageNamed:@"3.jpg"];
[self.view addSubview:xinImageView];
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"transform.scale";
// 缩放动画 范围在 0.8 ~1.0
animation.fromValue = [NSNumber numberWithFloat:0.8];
animation.toValue = [NSNumber numberWithFloat:1.0];
// 执行在0.5秒内
animation.duration = 0.5f;
// 组合动画
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
// 0.5秒内
animationGroup.duration = 0.5f;
// 是否重播
animationGroup.autoreverses = YES;
// 循环的次数
animationGroup.repeatCount = NSNotFound;
[animationGroup setAnimations:[NSArray arrayWithObjects:animation , nil]];
// 将上述两个动画编组
[xinImageView.layer addAnimation:animationGroup forKey:@"transform.scale"];
}
心儿 会不停的 跳动 扑通~扑通~