Stronger* 2017-06-24 03:23 采纳率: 0%
浏览 534

同样的功能用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代码

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-06 00:31
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在Swift中,我们可以使用UIViewaddSublayer方法来添加子层。以下是一个简单的例子:

    import UIKit
    
    class ViewController: UIViewController {
        
        @IBOutlet weak var imageView: UIImageView!
        
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // 创建一个新的CAReplicatorLayer实例
            let layer = CAReplicatorLayer()
    
            // 设置层的属性
            layer.bounds = CGRect(x: 0, y: 0, width: 60, height: 60)
            layer.position = self.imageView.center
            layer.backgroundColor = UIColor.clear.cgColor
            
            // 将新的层添加到视图的缩放上下文
            imageView.layer.addSublayer(layer)
            
            // 创建一个CALayer实例,并设置其属性
            let barLayer = CALayer()
            
            barLayer.bounds = CGRect(x: 0, y: 0, width: 8.0, height: 40.0)
            barLayer.position = CGPoint(x: 10, y: 75)
            barLayer.cornerRadius = 2.0
            barLayer.backgroundColor = UIColor.red.cgColor
            
            // 添加新的层到新的层上
            layer.addSublayer(barLayer)
            
            // 创建一个动画对象,并设置其属性
            let moveAnimation = CABasicAnimation(keyPath: "position.y")
            moveAnimation.toValue = barLayer.position.y - 35.0
            moveAnimation.duration = 0.5
            moveAnimation.autoreverses = true
            moveAnimation.repeatCount = Float.infinity
            barLayer.add(moveAnimation, forKey: nil)
            
            // 设置新层的样式和实例数等属性
            layer.instanceCount = 3
            layer.instanceDelay = 0.33
            layer.masksToBounds = true
            layer.instanceTransform = CATransform3DMakeTranslation(10.0, 0, 0)
        }
    }
    

    这个例子中的OC代码已经包含了所有必要的内容,包括创建CAReplicatorLayerCALayer和动画对象。你可以直接将这段代码复制粘贴到你的Objective-C项目中运行。

    评论

报告相同问题?