[英]UIView mask transparent.
I use CAShapeLayer to create filled circle (image #1). 我使用CAShapeLayer创建实心圆(图像#1)。
Now I would like to mask that circle with another smaller circle, so it looks like image #2. 现在,我想用另一个较小的圆遮住该圆,因此它看起来像图像2。
Later I will animate filling (image #3) the circle by reducing scale of the mask. 稍后,我将通过减小蒙版的比例为圆形填充动画(图像#3)。
How can I achieve this? 我该如何实现?
I'm not sure how correct the following approach is; 我不确定以下方法的正确性 ; it might be better to work with the
CALayer
s directly... 直接与
CALayer
一起使用可能会更好...
However, if the views/layers you're working with are quite simple, the following code might be good enough for what you need. 但是,如果您使用的视图/图层非常简单,则以下代码可能足以满足您的需求。
It's based around using a subview for the inner/smaller circle - and then animating the transform
property on UIView
. 它基于对内部/较小圆圈使用子视图,然后对
UIView
的transform
属性设置动画。
Just in case it's useful, here's a link to Apple's doc on Animating Views . 万一有用,这里有一个指向Apple的Animating Views文档的链接。
Here's the code: 这是代码:
@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);
}];
}
And a quick before and after from the simulator: 以及从模拟器之前和之后的快速操作:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.