objective c - Drawing a semi-transparent rectangle in iOS -
i searching answer long , tried solve myself couldn't , think best place ask. trying accomplish simple can't find way it. think missing on conceptual level. how try create semi-transparent rectangle inside class subclassing uiview:
- (void)drawrect:(cgrect)rect { cgcontextref context = uigraphicsgetcurrentcontext(); //this should white color 0.7 opacity right cgcontextsetrgbfillcolor(context, 1.0, 1.0, 1.0, 0.7); cgcontextfillrect(context, self.bounds); }
ok, makes color gray. tried same result: cgcontextsetrgbfillcolor(context, 1.0, 1.0, 1.0, 1.0);
cgcontextsetalpha(context, 0.7);
another try:
- (void)drawrect:(cgrect)rect { cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextsetrgbfillcolor(context, 1.0, 1.0, 1.0, 1.0); cgcontextfillrect(context, self.bounds); self.alpha = 0.7; }
voila! solved it. not exactly. want draw semi-transparent rectangle in context , not make whole view transparent. , example add rectangle opaque. love hear thoughts. thanks.
i guess reason why can't see transparency of rect presence of background color in view.
would try setting view background color this:
self.backgroundcolor = [uicolor clearcolor];
btw, statement, statement setting property of custom view (e.g., self.alpha = 0.7;
) should go init
method.
edit:
as pointed out @borrrden, real property set in order make trick possibly opaque
. set yes
, implies following, according apple docs:
an opaque view expected fill bounds entirely opaque content—that is, content should have alpha value of 1.0. if view opaque , either not fill bounds or contains wholly or partially transparent content, results unpredictable. should set value of property no if view or partially transparent.
setting background color clearcolor
set value of property no
collateral effect.
edit:
i not understand why overriding drawrect
in custom uiview
draw semi-transparent rectangle, maybe because not know trying do. in case, alternative approach might want using nested uiviews
or nested calayers
, assign different alpha
/opacity
of them.
if interested in rectangles varying degrees of opacity, simple nesting uiviews
in parent view , assign each of them semi-transparent background color.
Comments
Post a Comment