In recent projects, some of the boundaries of some views have to be set to rounded corners, such as specifying a rectangular view that needs to be rounded to the left, so encapsulate a class to achieve a rounded corner of a specified boundary
1, first create a taxonomy that inherits from UIView (because I need to set a button fillet in the project, so named BTN)
2. Setting the interface in the interface file
Here are the parameters: distance the height or width of the boundary where the fillet is to be set
#import <UIKit/UIKit.h>enum { zssideroundleft, zssideroundright, Zssideroundup, Zssiderounddown} zssideround; @interface UIView (ZSSIDEROUNDBTN)-(void) roundside: (zssideround) side distance: (cgfloat) distance; @end
3. Implemented in. m (Specify the boundary set to semicircle here)
#import "Uiview+zssideroundbtn.h"@implementationUIView (ZSSIDEROUNDBTN)- (void) Roundside: (zssideround) side distance: (cgfloat) distance{Uibezierpath*Maskpath; if(Side = =zssideroundleft) {Maskpath=[Uibezierpath bezierPathWithRoundedRect:self.bounds byroundingcorners: (uirect Cornertopleft|uirectcornerbottomleft) cornerradii:cgsizemake (distance/2, distance/2)]; } Else if(Side = =zssideroundright) {Maskpath=[Uibezierpath bezierPathWithRoundedRect:self.bounds byroundingcorners: (uirect Cornertopright|uirectcornerbottomright) cornerradii:cgsizemake (distance/2, distance/2)]; } Else if(Side = =zssideroundup) {Maskpath=[Uibezierpath bezierPathWithRoundedRect:self.bounds byroundingcorners: (uirect Cornertopleft|uirectcornertopright) cornerradii:cgsizemake (distance/2, distance/2)]; } Else{Maskpath=[Uibezierpath bezierPathWithRoundedRect:self.bounds byroundingcorners: (uirect Cornerbottomleft|uirectcornerbottomright) cornerradii:cgsizemake (distance/2, distance/2)]; } //Create a shape layer, set its pathcashapelayer* Masklayer =[Cashapelayer layer]; Masklayer.frame=Self.bounds; Masklayer.path=Maskpath.cgpath; //The newly created shape layer is set to the mask of the image view layerSelf.layer.mask =Masklayer; [Self.layer Setmaskstobounds:yes];}@end
I need to set some UIView sub-Class View when the boundary is semicircle, the direct reference is OK
Set the specified bounding fillet