How is a rounded rect view with transparency done on iphone?

前端 未结 5 1710
你的背包
你的背包 2020-11-30 17:38

A lot of apps pop up a transparent view with rounded corners and an activityIndicator when running a time consuming operation.

How is this rounding done and is it po

相关标签:
5条回答
  • 2020-11-30 18:14
    view.layer.cornerRadius = radius;
    

    The hard way (that used to be required in the first iPhone SDK) is to create your own UIView subclass with drawRect: method:

     CGContextRef context = UIGraphicsGetCurrentContext();
     CGContextSetRGBFillColor(context, 0,0,0,0.75);
    
     CGContextMoveToPoint(context, rect.origin.x, rect.origin.y + radius);
     CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height - radius);
     CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, 
                    radius, M_PI, M_PI / 2, 1); //STS fixed
     CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius, 
                            rect.origin.y + rect.size.height);
     CGContextAddArc(context, rect.origin.x + rect.size.width - radius, 
                    rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
     CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + radius);
     CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, 
                    radius, 0.0f, -M_PI / 2, 1);
     CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y);
     CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + radius, radius, 
                    -M_PI / 2, M_PI, 1);
    
     CGContextFillPath(context);
    

    Note: rect in this code should be taken from [self bounds] (or whatever location you want it in), it won't make sense with rect passed to drawRect: method.

    0 讨论(0)
  • 2020-11-30 18:15

    As of iPhone SDK 3.0, you can simply use the layer's cornerRadius property. E.g.:

    view.layer.cornerRadius = 10.0;
    

    Along the same lines, you can change the view's border color and width:

    view.layer.borderColor = [[UIColor grayColor] CGColor];
    view.layer.borderWidth = 1;
    
    0 讨论(0)
  • 2020-11-30 18:24

    MBProgressHUD....
    http://www.cocoadev.com/index.pl?MBProgressHUD

    0 讨论(0)
  • 2020-11-30 18:31

    In your view do this in the drawRect method

    float radius = 5.0f;
    
    CGRect rect = self.bounds;
    CGContextRef context = UIGraphicsGetCurrentContext();   
    rect = CGRectInset(rect, 1.0f, 1.0f);
    
    CGContextBeginPath(context);
    CGContextSetGrayFillColor(context, 0.5, 0.7);
    CGContextMoveToPoint(context, CGRectGetMinX(rect) + radius, CGRectGetMinY(rect));
    CGContextAddArc(context, CGRectGetMaxX(rect) - radius, CGRectGetMinY(rect) + radius, radius, 3 * M_PI / 2, 0, 0);
    CGContextAddArc(context, CGRectGetMaxX(rect) - radius, CGRectGetMaxY(rect) - radius, radius, 0, M_PI / 2, 0);
    CGContextAddArc(context, CGRectGetMinX(rect) + radius, CGRectGetMaxY(rect) - radius, radius, M_PI / 2, M_PI, 0);
    CGContextAddArc(context, CGRectGetMinX(rect) + radius, CGRectGetMinY(rect) + radius, radius, M_PI, 3 * M_PI / 2, 0);
    
    CGContextClosePath(context);
    CGContextFillPath(context);
    

    This will make your rounded rectangle for your view. You can find a complete example in the HeadsUpUI sample provided with the SDK. HTH

    0 讨论(0)
  • 2020-11-30 18:39

    I abstracted out @lostInTransit's response into this function:

    static void ContextAddRoundedRect(CGContextRef c, CGRect rect, CGFloat radius) {
      CGFloat minX = CGRectGetMinX(rect);
      CGFloat maxX = CGRectGetMaxX(rect);
      CGFloat minY = CGRectGetMinY(rect);
      CGFloat maxY = CGRectGetMaxY(rect);
    
      CGContextMoveToPoint(c, minX + radius, minY);
      CGContextAddArcToPoint(c, maxX, minY, maxX, minY + radius, radius);
      CGContextAddArcToPoint(c, maxX, maxY, maxX - radius, maxY, radius);
      CGContextAddArcToPoint(c, minX, maxY, minX, maxY - radius, radius);
      CGContextAddArcToPoint(c, minX, minY, minX + radius, minY, radius);
    }
    

    which places the path onto the context for you to do with as you may

    slightly different CoreGraphics calls and i didn't close the path, in case you want to add that

    CGContextFillPath(c);
    
    0 讨论(0)
提交回复
热议问题