如何使CABasicAnimation在superLayer界限之外不可见?

时间:2013-03-28 23:49:32

标签: ios uiview uibezierpath cabasicanimation

我正试图将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"];

2 个答案:

答案 0 :(得分:1)

如果您对superview本身有引用,则可以将clipsToBounds属性设置为YES。否则,您可以使用CALayer的{​​{1}}属性执行相同的操作。

答案 1 :(得分:1)

使用

testView.layer.masksToBounds = YES;