UIView面具透明。

时间:2014-03-20 21:06:08

标签: ios iphone uiview calayer mask

我使用 CAShapeLayer 创建实心圆圈(图片#1)。

现在我想用另一个较小的圆圈掩盖那个圆圈,所以它看起来像图像#2。

稍后我将通过缩小遮罩的比例来为填充(图像#3)填充动画。

我怎样才能做到这一点?

enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:3)

我不确定如何正确以下方法;最好直接使用CALayer ...

但是,如果您正在使用的视图/图层非常简单,则以下代码可能足以满足您的需要。

它基于使用内部/小圆圈的子视图 - 然后为transform上的UIView属性设置动画。

以防它有用,这里有一个指向Apple Animating Views文档的链接。

以下是代码:

@implementation ViewController
{
    UIView* _innerCircleView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView* outerCircleView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];

    UIBezierPath* bigCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:100 startAngle:0 endAngle:(M_PI * 2) clockwise:YES];
    [bigCircle closePath];
    CAShapeLayer* bigCircleLayer = [CAShapeLayer new];
    [bigCircleLayer setPath:bigCircle.CGPath];
    bigCircleLayer.fillColor = [UIColor blackColor].CGColor;
    [outerCircleView.layer addSublayer:bigCircleLayer];

    _innerCircleView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
    UIBezierPath* smallerCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(50, 50) radius:50 startAngle:0 endAngle:(M_PI * 2) clockwise:YES];
    [smallerCircle closePath];
    CAShapeLayer* smallCircleLayer = [CAShapeLayer new];
    [smallCircleLayer setPath:smallerCircle.CGPath];
    smallCircleLayer.fillColor = [UIColor whiteColor].CGColor;
    [_innerCircleView.layer addSublayer:smallCircleLayer];

    [outerCircleView addSubview:_innerCircleView];
    [self.view addSubview:outerCircleView];

    UIButton* animateButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 100, 50)];
    animateButton.backgroundColor = [UIColor blueColor];
    [animateButton setTitle:@"Animate" forState:UIControlStateNormal];
    [animateButton addTarget:self action:@selector(animateTap:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:animateButton];
}

- (void)animateTap:(id)s
{
    [UIView animateWithDuration:3.0f animations:^(){
        _innerCircleView.transform = CGAffineTransformScale(_innerCircleView.transform, 0.5, 0.5);
    }];
}

在之前快速,在模拟器之后之后

enter image description here