How can I remove a UIView with rounded corners from its parent view?

后端 未结 3 1311
不知归路
不知归路 2021-01-24 20:51

I\'m creating an iPad app for 3.2 and later. My app has an overlay view which has a semi-transparency that makes everything below it darker. In the middle of this view I am chop

相关标签:
3条回答
  • 2021-01-24 21:39

    If you change the cornerRadius property of a layer, you must also set clipsToBounds to YES on the associated view in order for the content to be clipped to the rounded corners.

    0 讨论(0)
  • 2021-01-24 21:48

    What you're trying to do is called masking. You can use Core Graphics to mask the current graphics context. See Apple's documentation on the subject here:

    http://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html#//apple_ref/doc/uid/TP30001066-CH212-CJBHDDBE

    0 讨论(0)
  • 2021-01-24 21:49

    Here's how I ended up getting it to work. This produces a hole with the same frame as the 'hole' UIView, cutting it out from self (UIView). This lets you see whatever is behind the 'hole' unhindered.

    - (void)drawRect:(CGRect)rect {
        CGFloat radius = self.hole.layer.cornerRadius;
        CGRect c = self.hole.frame;
        CGContextRef context = UIGraphicsGetCurrentContext();
    
            // this simply draws a path the same shape as the 'hole' view
        CGContextMoveToPoint(context, c.origin.x, c.origin.y + radius);
        CGContextAddLineToPoint(context, c.origin.x, c.origin.y + c.size.height - radius);
        CGContextAddArc(context, c.origin.x + radius, c.origin.y + c.size.height - radius, radius, M_PI_4, M_PI_2, 1);
        CGContextAddLineToPoint(context, c.origin.x + c.size.width - radius, c.origin.y + c.size.height);
        CGContextAddArc(context, c.origin.x + c.size.width - radius, c.origin.y + c.size.height - radius, radius, M_PI_2, 0.0f, 1);
        CGContextAddLineToPoint(context, c.origin.x + c.size.width, c.origin.y + radius);
        CGContextAddArc(context, c.origin.x + c.size.width - radius, c.origin.y + radius, radius, 0.0f, -M_PI_2, 1);
        CGContextAddLineToPoint(context, c.origin.x + radius, c.origin.y);
        CGContextAddArc(context, c.origin.x + radius, c.origin.y + radius, radius, -M_PI_2, M_PI, 1);
    
            // finish
        CGContextClosePath(context);
        CGContextClip(context); // this is the secret sauce
        CGContextClearRect(context, c);
    }
    
    0 讨论(0)
提交回复
热议问题