iOS 實現(xiàn)半邊圓角或部分圓角

正常設(shè)置view的圓角
- (UIImageView *)mainImageView {
    if (!_mainImageView) {
        _mainImageView = [[UIImageView alloc] init];
        _mainImageView.layer.cornerRadius = 5.0;
        _mainImageView.layer.masksToBounds = YES;
    }
    return _mainImageView;
}
設(shè)置一個或多個

封裝了Category方法, 兩種方法是一個針對絕對布局一個針對相對布局

UIView+MLCore.h 中的實現(xiàn)
#import <UIKit/UIKit.h>
@interface UIView (LSCore)

#pragma mark - 設(shè)置部分圓角
/**
 *  設(shè)置部分圓角(絕對布局)
 *
 *  @param corners 需要設(shè)置為圓角的角 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerAllCorners
 *  @param radii   需要設(shè)置的圓角大小 例如 CGSizeMake(20.0f, 20.0f)
 */
- (void)addRoundedCorners:(UIRectCorner)corners
                withRadii:(CGSize)radii;
/**
 *  設(shè)置部分圓角(相對布局)
 *
 *  @param corners 需要設(shè)置為圓角的角 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerAllCorners
 *  @param radii   需要設(shè)置的圓角大小 例如 CGSizeMake(20.0f, 20.0f)
 *  @param rect    需要設(shè)置的圓角view的rect
 */
- (void)addRoundedCorners:(UIRectCorner)corners
                withRadii:(CGSize)radii
                 viewRect:(CGRect)rect;

@end
UIView+LSCore.m 中的實現(xiàn)
#import "UIView+LSCore.h"
@implementation UIView (LSCore)
#pragma mark - 設(shè)置部分圓角
/**
 *  設(shè)置部分圓角(絕對布局)
 *
 *  @param corners 需要設(shè)置為圓角的角 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerAllCorners
 *  @param radii   需要設(shè)置的圓角大小 例如 CGSizeMake(20.0f, 20.0f)
 */
- (void)addRoundedCorners:(UIRectCorner)corners
                withRadii:(CGSize)radii {
    
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:radii];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    
    self.layer.mask = shape;
}

/**
 *  設(shè)置部分圓角(相對布局)
 *
 *  @param corners 需要設(shè)置為圓角的角 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerAllCorners
 *  @param radii   需要設(shè)置的圓角大小 例如 CGSizeMake(20.0f, 20.0f)
 *  @param rect    需要設(shè)置的圓角view的rect
 */
- (void)addRoundedCorners:(UIRectCorner)corners
                withRadii:(CGSize)radii
                 viewRect:(CGRect)rect {
    
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    
    self.layer.mask = shape;
}
@end

#######示例

- (UIImageView *)blackBGimageView {
    if (!_blackBGimageView) {
        _blackBGimageView = [[UIImageView alloc] init];
        _blackBGimageView.backgroundColor = black_color;
        _blackBGimageView.alpha = 0.7;
        [_blackBGimageView addRoundedCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight  withRadii:CGSizeMake(5.0, 5.0) viewRect:CGRectMake(0, 0, ((SCREEN_WIDTH - 4*10)/2.0), 35)];
    }
    return _blackBGimageView;
}

內(nèi)容擴展

高性能設(shè)置圓角,uiimage封裝Category方法

// 等比縮放
- (UIImage *)QS_cropSameImageToSize:(CGSize)size
{
    CGFloat scale =  [UIScreen mainScreen].scale;
    UIGraphicsBeginImageContextWithOptions(size, YES, scale);
    CGSize aspectFitSize = CGSizeZero;
    if (self.size.width != 0 && self.size.height != 0)
    {
        CGFloat rateWidth = size.width / self.size.width;
        CGFloat rateHeight = size.height / self.size.height;
        CGFloat rate = MIN(rateHeight, rateWidth);
        aspectFitSize = CGSizeMake(self.size.width * rate, self.size.height * rate);
    }
    [self drawInRect:CGRectMake(0, 0, aspectFitSize.width, aspectFitSize.height)];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

// 非等比縮放
- (UIImage *)QS_noSameImageToSize:(CGSize)size
{
    CGFloat scale =  [UIScreen mainScreen].scale;
    UIGraphicsBeginImageContextWithOptions(size, YES, scale);
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

//圓角切割
- (UIImage *)QS_getCornerRadius:(CGFloat)cornerRadius
{
    CGFloat scale = [UIScreen mainScreen].scale;
    UIGraphicsBeginImageContextWithOptions(self.size, NO, scale);
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
    CGContextAddPath(c, path.CGPath);
    CGContextClip(c);
    [self drawInRect:rect];
    CGContextDrawPath(c, kCGPathFillStroke);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。