在iphone上如何做一个圆滑的透明度视图? [英] How is a rounded rect view with transparency done on iphone?

查看:103
本文介绍了在iphone上如何做一个圆滑的透明度视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多应用程序在运行耗时的操作时会弹出一个带有圆角和activityIndi​​cator的透明视图。

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

如何完成此舍入操作,做它只是使用Interface Builder(因为有很多地方,我想使用像这样)?或者,我应该使用具有圆润的矩形或可拉伸图像的imageview吗?我需要自己绘制背景吗?

How is this rounding done and is it possible to do it just using Interface Builder (as there are lots of places I'd like to use something like this)? Or, should I use an imageview with a rounded rect or stretchable image? Do I need to draw the background myself?

到目前为止,我已经设法通过在Interface Builder中设置alphaValue来获得类似透明度的基本视图,但它不会具有圆角,并且透明度似乎适用于所有子视图(我不想让文本和activityindicator是透明的,但即使我设置alphaValue在IB,它似乎被忽略)。

So far, I have managed to get a basic view with similar transparency by setting the alphaValue in Interface Builder however it doesn't have rounded corners, and also the transparency seems to apply to all subviews (I don't want the text and activityindicator to be transparent, however even though I set the alphaValue on those in IB it seems to get ignored).

推荐答案

view.layer.cornerRadius = radius;

困难的方式(以前在第一个iPhone SDK需要)是创建自己的方法:

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);

注意: rect [self bounds] (或任何你想要的位置),它不会有意义 rect 传递给 drawRect:方法。

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.

这篇关于在iphone上如何做一个圆滑的透明度视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆