I've been able to get pretty far with what I've been wanting to accomplish, and that's to replicate iOS's built in circular photo cropper for the built in contacts app. However, I'm stuck at trying to get my CAShapeLayers
made correctly. I'm trying to make a transparent 320 px diameter circle and the rest of the view filled with an 0.9 alpha black background. The circle and rectangle are in the right place, but, the circle is not completely transparent like I need it to be.
I'm lost as to how to fix this. I appreciate your help! Code and screenshot:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([navigationController.viewControllers count] == 3)
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = screenRect.size.height;
UIView *plCropOverlay = [[[viewController.view.subviews objectAtIndex:1]subviews] objectAtIndex:0];
plCropOverlay.hidden = YES;
CAShapeLayer *circleLayer = [CAShapeLayer layer];
if (screenHeight == 568)
{
[circleLayer setPosition:CGPointMake(0.0f,124.0f)];
}
else
{
[circleLayer setPosition:CGPointMake(0.0f,80.0f)];
}
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:
CGRectMake(0.0f, 0.0f, 320.0f, 320.0f)];
[circleLayer setPath:[path CGPath]];
[circleLayer setFillColor:[[UIColor whiteColor] CGColor]];
circleLayer.opacity = 0.7f;
// Set to 0.7f to show for screenshot purposes; setting to 0.0 would make it invisible and blend in with the below rectangleLayer.
CAShapeLayer *rectangleLayer = [CAShapeLayer layer];
UIBezierPath *path2 = [UIBezierPath bezierPathWithRect:CGRectMake(0.0f, 0.0f, 320.0f, screenHeight - 72)];
[rectangleLayer setPath:[path2 CGPath]];
[rectangleLayer setFillColor:[[UIColor blackColor] CGColor]];
[rectangleLayer setOpacity:0.9f];
[rectangleLayer addSublayer:circleLayer];
[[viewController.view layer] addSublayer:rectangleLayer];
UILabel *moveLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 320, 50)];
[moveLabel setText:@"Move and Scale"];
[moveLabel setTextAlignment:NSTextAlignmentCenter];
[moveLabel setTextColor:[UIColor whiteColor]];
[viewController.view addSubview:moveLabel];
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…