CYC-爱心暖暖的小动画

一个看着暖暖的心形小动画 简单操作 就能实现 送给另一个她一个小惊喜吧

一个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"];


}

心儿 会不停的 跳动 扑通~扑通~
这里写图片描述
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值