Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

iphone - can a particular rect of UIview have different alpha..?

I wan't a imageview which is semi-transparent in other parts and completely transparent in the middle .. see this image.. enter image description here

i have managed to do it but it is not the best of ways.. can I only have a rect inside yellow lines transparent ...?

EDIT: Is it possible to change the alpha of a certain rect of a view..?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Place a custom UIView over the view showing the image, size and position it to exactly overlap. In the custom view's drawRect method

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGRect rBounds = self.bounds;

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Fill background with 80% white
    CGContextSetFillColorWithColor(context, [[[UIColor whiteColor] colorWithAlphaComponent:0.8] CGColor]);
    CGContextFillRect(context, rBounds);

    // Draw the window 'frame'
    CGContextSetStrokeColorWithColor(context, [[UIColor orangeColor] CGColor]);
    CGContextSetLineWidth(context, 10);
    CGContextStrokeRect(context, self.maskRect);

    // make the window transparent
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextFillRect(context, self.maskRect);
}

where maskRect is the rectangle to be shown transparent. Do ensure that the background color of the custom view is set to 'clearColor'

You can write any additional logic required to change the maskRect and call 'setNeedsDisplay' on this view.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...