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

前端 未结 5 1728
你的背包
你的背包 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.

提交回复
热议问题