同样的功能用swift写可以实现用oc写就不行,代码基本一致
    let lay = CAReplicatorLayer()
    lay.bounds = CGRect(x: 0, y: 0, width: 60, height: 60)
    lay.position = view.center
    lay.backgroundColor = UIColor.clear.cgColor
    view.layer.addSublayer(lay)


    let bar = CALayer()
    bar.bounds = CGRect(x: 0, y: 0, width: 8.0, height: 40.0)
    bar.position = CGPoint(x: 10, y: 75)
    bar.cornerRadius = 2.0
    bar.backgroundColor = UIColor.red.cgColor
    lay.addSublayer(bar)


    let move = CABasicAnimation(keyPath: "position.y")
    move.toValue = bar.position.y - 35.0
    move.duration = 0.5
    move.autoreverses = true
    move.repeatCount = Float.infinity
    bar.add(move, forKey: nil)
    lay.instanceCount = 3
    lay.instanceDelay = 0.33
    lay.masksToBounds = true
    lay.instanceTransform = CATransform3DMakeTranslation(10.0, 0, 0)

            以上是swift代码


             CAReplicatorLayer * lay = [CAReplicatorLayer layer];

lay.bounds = CGRectMake(0, 0, 60, 60);

lay.position = self.view.center;

lay.backgroundColor = [UIColor clearColor].CGColor;

[self.view.layer addSublayer:lay];

CALayer * bar = [CALayer layer];

bar.bounds = CGRectMake(0, 0, 8, 40);

bar.position = CGPointMake(10, 75);

bar.cornerRadius = 2.0;

bar.backgroundColor = [UIColor redColor].CGColor;

[lay addSublayer:bar];

CABasicAnimation * move = [CABasicAnimation animationWithKeyPath:@"path"];
move.fromValue = @(10);
move.toValue = @(40);
move.delegate = self;
move.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
move.toValue = [NSValue valueWithCGPoint:CGPointMake(0 ,40)];
move.duration = 0.5;
move.timingFunction = [CAMediaTimingFunction  functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//自己返回
move.autoreverses = YES;
move.repeatCount = 100;
[bar addAnimation:move forKey:@"path"];
lay.instanceCount = 3;
lay.instanceDelay = 0.33;
lay.masksToBounds = YES;
lay.instanceTransform = CATransform3DMakeTranslation(10.0, 0, 0);

以上是oc代码

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐