CAAnimation动画系列示例代码第二篇---CAReplicatorLayer

CAReplicatorLayer简单使用

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setReplicatorLayer];
}
- (void)setReplicatorLayer{
    CAReplicatorLayer *rLayer = [[CAReplicatorLayer alloc] init];![在这里插入图片描述](https://img-blog.csdnimg.cn/20190102150348885.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3QzMDM1,size_16,color_FFFFFF,t_70)
    rLayer.frame = (CGRect){{0, 100}, {500, 80}};
    rLayer.backgroundColor = [UIColor greenColor].CGColor;
    CALayer *redLayer = [[CALayer alloc] init];
    redLayer.backgroundColor = [UIColor whiteColor].CGColor;
    redLayer.frame = (CGRect){{0, 10}, {60, 60}};
    
    NSInteger instanceCount = 5;
    rLayer.instanceCount = instanceCount;
    // 70 为间隔 + layer的width
    rLayer.instanceTransform = CATransform3DMakeTranslation(70, 0, 0);
    // 控制颜色变化快慢
    CGFloat offsetStep = -1.0 / instanceCount;
    rLayer.instanceBlueOffset = offsetStep;
    rLayer.instanceGreenOffset = offsetStep;
    
    [rLayer addSublayer:redLayer];
   // 嵌套layer
    CAReplicatorLayer *outerReplicatorLayer = [[CAReplicatorLayer alloc] init];
    
    [outerReplicatorLayer addSublayer:rLayer];
    
    outerReplicatorLayer.instanceCount = instanceCount;
    outerReplicatorLayer.instanceTransform = CATransform3DMakeTranslation(0, 70, 0);
    outerReplicatorLayer.instanceRedOffset = offsetStep;
    
    [self.view.layer addSublayer:outerReplicatorLayer];
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里提供一个简单的红包雨功能的object-c实现示例: 首先,我们需要准备红包图片的资源,用UIImageView来展示红包。 ```objective-c // 初始化红包图片视图 UIImageView *redPacketImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -50, 50, 50)]; redPacketImageView.image = [UIImage imageNamed:@"redPacket.png"]; [self.view addSubview:redPacketImageView]; ``` 接下来,我们需要实现红包的下落动画,让它从上面飘落到下面。 ```objective-c // 定义红包下落动画 CABasicAnimation *fallingAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"]; fallingAnimation.fromValue = [NSNumber numberWithFloat:-50]; fallingAnimation.toValue = [NSNumber numberWithFloat:self.view.bounds.size.height + 50]; fallingAnimation.duration = 5.0; fallingAnimation.delegate = self; // 添加红包下落动画 [redPacketImageView.layer addAnimation:fallingAnimation forKey:@"fallingAnimation"]; ``` 我们同时需要处理多个红包的下落逻辑,可以使用NSTimer定时器来实现。 ```objective-c // 定义红包定时器 NSTimer *redPacketTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(addRedPacket) userInfo:nil repeats:YES]; // 添加红包方法 - (void)addRedPacket { // 初始化红包图片视图 UIImageView *redPacketImageView = [[UIImageView alloc] initWithFrame:CGRectMake(arc4random_uniform((uint32_t)self.view.bounds.size.width - 50), -50, 50, 50)]; redPacketImageView.image = [UIImage imageNamed:@"redPacket.png"]; [self.view addSubview:redPacketImageView]; // 定义红包下落动画 CABasicAnimation *fallingAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"]; fallingAnimation.fromValue = [NSNumber numberWithFloat:-50]; fallingAnimation.toValue = [NSNumber numberWithFloat:self.view.bounds.size.height + 50]; fallingAnimation.duration = 5.0; fallingAnimation.delegate = self; // 添加红包下落动画 [redPacketImageView.layer addAnimation:fallingAnimation forKey:@"fallingAnimation"]; } ``` 最后,我们需要处理红包落地后的逻辑,让用户可以获得奖励。 ```objective-c // 红包下落动画结束后回调方法 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { // 判断是否是红包下落动画 if ([redPacketImageView.layer animationForKey:@"fallingAnimation"] == anim) { // 移除红包图片视图 [redPacketImageView removeFromSuperview]; // 弹出获得奖励提示框 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"恭喜您获得奖励!" message:@"获得了10元现金红包!" preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]]; [self presentViewController:alertController animated:YES completion:nil]; } } ``` 这样就实现了一个简单的红包雨功能。当用户进入红包雨页面后,红包会不断从上面飘落到下面,并且用户可以获取奖励。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值