我正在尝试将一个三角形从 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"];