In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use a line of code in iOS to achieve UIView hollowing out effect. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
First, train of thought
Our ultimate goal is to encapsulate an interface that is called in a way similar to the maskView attribute, which can easily hollowed out a UIView.
Note: originView is used to refer to the view that needs to be effective, and maskView is used to refer to the view that acts as a mask.
At present, it seems that we can start from two directions:
Modify the drawing process of the mask modify the maskView itself
The first way is to redraw the view of originView when setting this property, and then subtract the area of maskView as you draw.
The second way is that when you get the maskView, you first deal with the maskView itself and reverse the mask range. Then do the mask effect, because the mask area has been opposite, so the result is the opposite, to achieve the purpose of hollowing out.
Mode 2 seems to be more reliable, and finally, it can be implemented by calling UIView's setMaskView:, and some of the features of the original mask can be retained. For example, when you modify the frame of maskView, the mask position of originView will change accordingly.
II. Realization
Generating the opposite mask map can be divided into three steps. Assuming that the maskView you get at the beginning is as follows, let's take a look at the changes in the mask diagram at each step of the conversion process.
Note: for a more intuitive effect, the transparent part of the picture is represented by a gray-and-white grid (the same below).
1. Convert maskView to UIImage
UIGraphicsBeginImageContextWithOptions (self.bounds.size, NO, [UIScreen mainScreen] .scale); CGContextTranslateCTM (UIGraphicsGetCurrentContext (), view.frame.origin.x, view.frame.origin.y); [view.layer renderInContext:UIGraphicsGetCurrentContext ()]; UIImage * image = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext ()
In this step, you get the image image corresponding to maskView. At this point, the size of the mask image is synchronized to the size of originView.
2. Set
UIImage is converted to CGContextRef with only alpha channel
CGImageRef originalMaskImage = [image CGImage]; float width = CGImageGetWidth (originalMaskImage); float height = CGImageGetHeight (originalMaskImage); int strideLength = ROUND_UP (width * 1,4); unsigned char * alphaData = calloc (strideLength * height, sizeof (unsigned char)); CGContextRef alphaOnlyContext = CGBitmapContextCreate (alphaData, width, height, 8, strideLength, NULL, kCGImageAlphaOnly); CGContextDrawImage (alphaOnlyContext, CGRectMake (0,0, width, height), originalMaskImage)
At this time, the corresponding image of the alphaOnlyContext is as follows, leaving only the alpha channel.
3. Set
Traversal conversion of alpha value in CGContextRef
For (int y = 0; y < height; y +) {for (int x = 0; x < width; x +) {unsigned char val = alphaData [y*strideLength + x]; val = 255-val; alphaData [y*strideLength + x] = val;}} CGImageRef alphaMaskImage = CGBitmapContextCreateImage (alphaOnlyContext); UIImage * result = [UIImage imageWithCGImage:alphaMaskImage]
After conversion, the result image obtained is:
Therefore, we can use result to do mask happily.
III. Use
We can encapsulate the above steps into a method, which can be implemented with category.
@ interface UIView (MFSubtractMask)-(void) setSubtractMaskView: (UIView *) view;- (UIView *) subtractMaskView;@end
This makes it very convenient to call, and one line of code is done:
View.subtractMaskView = maskView
IV. Limitations
1. SubtractMaskView will not refresh automatically
We know that when the content of UIView's maskView is dynamically modified, it will be reflected in UIView in real time. In this project, however, the subtractMaskView property generates a completely new image as a mask because the view is not refreshed in real time based on the contents of the subtractMaskView. If you need to update, you must manually call the setSubtractMaskView: method to regenerate the mask map.
2. SetSubtractMaskView: should not be called frequently
SetSubtractMaskView: in essence, it is the process of generating a new mask image, which involves traversing the pixels of the image, which is time-consuming and should not be called frequently.
On "how to use a line of code in iOS to achieve UIView hollowing out effect" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.