【问题标题】:CALayer - CABasicAnimation not scaling around center/anchorPointCALayer - CABasicAnimation 不围绕中心/锚点缩放
【发布时间】:2012-07-15 21:56:46
【问题描述】:

使用下面的代码,我一直试图让我的 CALayer 围绕其中心进行缩放 - 但它按左/上 (0,0) 缩放。我看到了这个问题:Anchor Point in CALayer 并尝试设置锚点 - 但在我设置它之前它是 [.5, .5] 并且设置没有区别。还尝试设置contentsGravity

设置是我在self.view.layer 中添加了一个CAShapeLayer,我在其中进行了一些绘图,然后添加了CABasicAnimation。动画运行良好 - 但它向顶部/左侧缩放 - 而不是视图的中心。

已经修复了几个小时 - 它一定很简单,但我不明白。

shapeLayer.anchorPoint = CGPointMake(.5,.5);
shapeLayer.contentsGravity = @"center";

printf("anchorPoint %f  %f\n", shapeLayer.anchorPoint.x, shapeLayer.anchorPoint.y);

CABasicAnimation *animation =
[CABasicAnimation animationWithKeyPath:@"transform.scale"];

animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animation.toValue =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];

[animation setDuration:1.0];
animation.fillMode=kCAFillModeForwards;
animation.removedOnCompletion=NO;
[shapeLayer addAnimation:animation forKey:@"zoom"];

【问题讨论】:

    标签: ios core-animation


    【解决方案1】:

    你所在层的bounds是什么?

    我敢打赌它的大小为零;尝试将其大小设置为与您的形状相同的大小。

    CAShapeLayer 将形状绘制为一种覆盖,独立于您在普通CALayer 中使用的contents(和boundscontentsGravity 等)。这可能有点混乱。

    【讨论】:

    • 我遇到了类似的问题,就是没有设置图层的框架。然后锚定了。
    • 我面临同样的问题,但还不能解决..如果我将边界应用于 CAShape 层,那么它的位置会完全改变,但如果我不应用边界,它就会开始从VC的左上角动画:(
    【解决方案2】:

    还要确保在创建视图后添加动画。如果您尝试在 viewDidLoad 中添加动画,它可能无法正常工作。尝试将其添加到 viewDidAppear。

    我只知道 Swift,但这里有一个例子:

    override func viewDidAppear (animated: Bool) {
        addPulsation(yourButton)
    }
    
    func addPulsation (button: UIButton) {
        let scaleAnimation:CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
        scaleAnimation.duration = 1.0
        scaleAnimation.repeatCount = 100
        scaleAnimation.autoreverses = true
        scaleAnimation.fromValue = 1.05;
        scaleAnimation.toValue = 0.95;
        button.layer.addAnimation(scaleAnimation, forKey: "scale")
    }
    

    【讨论】:

      【解决方案3】:

      这是一个旧线程,但万一这对任何人都有帮助。

      这是一个坐标空间问题。您只需要设置绘图空间和路径的空间。这段代码对我有用...

      ...只需将其放在动画创建方法的开头(在父视图的方法内)...

      // add animation to remap (without affecting other elements)
          let animationLayer = CALayer()
          layer?.addSublayer(animationLayer). // (don't use optional for iOS)
      
      // set the layer origin to the center of the view (or any center point for the animation)
          animationLayer.bounds.origin .x = -self.bounds.size.width / 2.0
          animationLayer.bounds.origin .y = -self.bounds.height / 2.0
      
      // make image layer with circle around the zero point
          let imageLayer = CAShapeLayer()
          animationLayer.addSublayer(imageLayer)
      
          let shapeBounds = CGRect(x: -bounds.size.width/2.0,
                                   y:-bounds.size.height/2.0,
                                   width: bounds.size.width,
                                   height: bounds.size.height)
      
          imageLayer.path = CGPath(ellipseIn: shapeBounds, transform: nil)
          imageLayer.fillColor = fillColour
      
      
          // **** apply your animations to imageLayer here ****
      

      此代码在 iOS 和 MacOS 上保持不变 - 除了 MacOS 所需的可选层。

      (顺便说一句,您需要在动画完成后删除动画层!)

      【讨论】:

        【解决方案4】:

        我花了几个小时来了解如何根据中心进行操作,但找不到任何适用于 OSX 的解决方案。我决定使用 CATransform3DMakeTranslation 移动图层并结合两种变换。

        我的代码:

        NSView *viewToTransform = self.view;
        [viewToTransform setWantsLayer:YES];
        
        viewToTransform.layer.sublayerTransform = CATransform3DConcat(CATransform3DMakeScale(-1.0f, -1.0f, 1.0f), CATransform3DMakeTranslation(viewToTransform.bounds.size.width, viewToTransform.bounds.size.height, 0));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-03-12
          • 1970-01-01
          • 2012-12-01
          • 1970-01-01
          • 2012-12-10
          • 1970-01-01
          • 1970-01-01
          • 2013-11-15
          相关资源
          最近更新 更多