Specifies the border rounded corner and the border rounded corner.

Source: Internet
Author: User

Specifies the border rounded corner and the border rounded corner.

Some boundaries of some views need to be set as rounded corners in recent projects. For example, to specify a rectangular view, you need to set its left to a circular shape. Therefore, a class is encapsulated to implement the rounded corner of the specified border.

1. First create a category that inherits from UIView (Btn is named because I need to set a button rounded corner in the Project)

 

2. Set the interface in the interface file

Here parameter: distance is the height or width of the boundary of the rounded corner.

#import <UIKit/UIKit.h>typedef enum {    ZSSideRoundLeft,    ZSSideRoundRight,    ZSSideRoundUp,    ZSSideRoundDown} ZSSideRound;@interface UIView (ZSSideRoundBtn)- (void)roundSide:(ZSSideRound)side distance:(CGFloat)distance;@end

 

 

3. Implement in. m (set the boundary to semi-circle here)

# Import "UIView + ZSSideRoundBtn. h "@ implementation UIView (ZSSideRoundBtn)-(void) roundSide :( ZSSideRound) side distance :( CGFloat) distance {UIBezierPath * maskPath; if (side = ZSSideRoundLeft) {maskPath = [UIBezierPath bezierPathWithRoundedRect: self. bounds byRoundingCorners :( UIRectCornerTopLeft | percent) cornerRadii: CGSizeMake (distance/2, distance/2)];} else if (side = percent) {maskPath = [UIBezierPath limit: self. bounds byRoundingCorners :( alert | UIRectCornerBottomRight) cornerRadii: CGSizeMake (distance/2, distance/2)];} else if (side = signature) {maskPath = [UIBezierPath handle: self. bounds byRoundingCorners :( UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii: CGSizeMake (distance/2, distance/2)];} else {maskPath = [UIBezierPath self. bounds byRoundingCorners :( UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii: CGSizeMake (distance/2, distance/2)];} // create a shape layer, set its path to CAShapeLayer * maskLayer = [CAShapeLayer layer]; maskLayer. frame = self. bounds; maskLayer. path = maskPath. CGPath; // the newly created shape layer is set as the mask self of the Image view layer. layer. mask = maskLayer; [self. layer setMasksToBounds: YES];} @ end

 

 

After that, you need to set the boundary of some subclass views of the UIView to semi-circle.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.