【问题标题】:How to Make CABasicAnimation invisible outside of superLayer bounds?如何使 CABasicAnimation 在 superLayer 边界之外不可见?
【发布时间】:2013-03-28 23:49:31
【问题描述】:

我正在尝试将一个三角形从 UIView 边界移动到 UIView 区域。

我正在使用 UIBezierPath。

虽然三角形在该区域之外,但我希望它不可见。 我只希望 UIView 内的三角形部分可见。

不幸的是,它在整个动画中都可见(在边界之外和内部)。

这是我的代码:

UIBezierPath *triangle = [UIBezierPath bezierPath];
[triangle moveToPoint:CGPointMake(X, Y)];
[triangle addLineToPoint:CGPointMake(X - (width*0.5), Y)];
[triangle addLineToPoint:CGPointMake(X, Y + (width*0.5))];
[triangle closePath];
CAShapeLayer *triLayer = [CAShapeLayer layer];
triLayer.frame = testView.bounds;
triLayer.path = triangle.CGPath;
triLayer.strokeColor = [[UIColor redColor] CGColor];
triLayer.fillColor = [[UIColor yellowColor] CGColor];

[testView.layer addSublayer:triLayer];

CABasicAnimation *triangleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
triangleAnimation.duration=0.5;
triangleAnimation.repeatCount=1;
triangleAnimation.autoreverses=NO;

triangleAnimation.fromValue=[NSNumber numberWithFloat:(width*0.5)];
triangleAnimation.toValue=[NSNumber numberWithFloat:0];
triangleAnimation.removedOnCompletion = NO;
triangleAnimation.fillMode = kCAFillModeForwards;

[triLayer addAnimation:triangleAnimation forKey:@"animateLayer"];

【问题讨论】:

    标签: ios uiview uibezierpath cabasicanimation


    【解决方案1】:

    如果您有对超级视图本身的引用,您可以将其上的clipsToBounds 属性设置为YES。否则,您可以使用CALayermasksToBounds 属性来做同样的事情。

    【讨论】:

      【解决方案2】:

      使用

      testView.layer.masksToBounds = YES;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多