“This is my 28th day of participating in the First Challenge 2022. For details: First Challenge 2022.”
preface
Action buttons often require view rounded corners, such as the registration button on a registration page.
I iOS set view cornerRadius property invalid solution
1.1 Solution Procedure
_numberlab. clipsToBounds = YES; 2, Set [self.numberLab layoutIfNeeded]; Then you’ll execute the cornerRadius
[self.view layoutIfNeeded] [self.view layoutIfNeeded] [self.view layoutIfNeeded]] [self.view layoutIfNeeded] I can get the frame and set the rounded corners
- (void)layoutSubviews{
[super layoutSubviews];
[self.numberLab layoutIfNeeded];
[self.contentView bringSubviewToFront:self.numberLab];
self.numberLab.layer.cornerRadius =self.numberLab.height*0.5;
}
Copy the code
3, try to set up _numberLab. Layer. MasksToBounds = YES;
1.2 What is the masksToBounds attribute? What does it do
- MasksToBounds specifies whether the child layer is clipped when it exceeds the parent layer. YES indicates clipping and NO indicates NO. When using the layer.cornerRadius property, masksToBounds should be set to YES to ensure that all corners are rounded. However, this method is inefficient and the easiest to implement.
- MasksToBounds is different from clipsToBounds. The former refers to whether the child layer is clipped when it exceeds the parent layer (masksToBounds is a CALayer attribute). The latter refers to whether a child view is clipsToBounds when it exceeds the parent view (clipsToBounds is a UIView property).
1.3 Set only the rounded corners at the top
usage
- (void)layoutSubviews {
[super layoutSubviews];
[self.titleV layoutIfNeeded];
[self.titleV setCornerOnTop:kAdjustRatio(20)];
}
Copy the code
SetCornerOnTop: Implementation of the method
#pragma mark - Corner Radius
- (void)setCornerOnTop:(CGFloat)radius {
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
- (void)setCornerOnBottom:(CGFloat)radius {
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
- (void)setCornerOnLeft:(CGFloat)radius {
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft)
cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
- (void)setCornerOnRight:(CGFloat)radius {
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight)
cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
- (void)setCornerOnTopLeft:(CGFloat)radius {
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:UIRectCornerTopLeft
cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
- (void)setCornerOnTopRight:(CGFloat)radius {
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:UIRectCornerTopRight
cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
- (void)setCornerOnBottomLeft:(CGFloat)radius {
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:UIRectCornerBottomLeft
cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
- (void)setCornerOnBottomRight:(CGFloat)radius {
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:UIRectCornerBottomRight
cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
- (void)setAllCorner:(CGFloat)radius {
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
cornerRadius:radius];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
- (void)setCornerRadius:(CGFloat)radius {
[self.layer setCornerRadius:radius];
[self.layer setMasksToBounds:YES];
}
Copy the code
II Add glow Shadow (Shadow)
IOS removes the black line at the top of the TabBar and adds a glowing shadow
Blog.csdn.net/z929118967/…
III How to optimize scrolling loading of UITableView to prevent stutters?
Table view (UITableView) is mainly used to list and display data items. If there is a large amount of data, the table will need the same number of cell views to display, and the creation and initialization of a large number of cells will cause memory stress, affecting the interface fluency, so the optimization of table view loading is very important. The scroll optimization of UITableView mainly lies in the following two aspects:
- 1) Reduce the amount of computation in the cellForRowAtIndexPath proxy (cell content calculation).
- 2) Reduce the amount of computation (cell height calculation) in the heightForRowAtIndexPath agent.
3.1 Reduce the amount of computation in cellForRowAtIndexPath proxy:
- (1) Some basic data needed in each cell should be calculated in advance and taken out directly when the proxy calls.
- (2) The image should be loaded asynchronously. After loading, set the image according to the reference of UIImageView inside the cell.
- ③ If there are a large number of images, the size of the image should be compressed by the Transform matrix in advance as required (directly setting the contentMode of the image to compress itself will still affect the scrolling efficiency). Preview images and HD images should be prepared when necessary, and hd images can be loaded when necessary.
- (4) The “lazy loading” method of images, that is, lazy loading, avoids frequent requests for server data when scrolling speed is fast.
- (5) Try to manually draw the view to improve the smoothness, rather than subclassing the UITableViewCell and overwriting the drawRect method, because there is not only one ContentView in the cell. Use CALayer instead of UIView to draw cells.
3.2 Reduce computation in heightForRowAtIndexPath proxy:
- ① Because every tableView update (update) will call each cell height of the heightForRowAtIndexPath proxy, will greatly increase the calculation time. If all cell heights of the table are fixed, remove the heightForRowAtIndexPath proxy and set the rowHeight property of the tableView to the fixed height.
- (2) If the height is not fixed, the height data of the cell should be calculated and stored as far as possible, and taken directly when the proxy calls, that is, the calculation time complexity of height should be reduced to O(1).
For example, in an asynchronous request for server data, the cell is highly precomputed and stored as a datasource in the database for ready access.
see also
🍅 Contact author: iOS Reverse (public number: iosrev)
🍅 Author profile: CSDN blog expert certification 🏆 international Top 50, Huawei Cloud enjoy expert certification 🏆, iOS reverse public number master
🍅 Resume template, technical assistance. Pay attention to me, all for you.