如何是实现上面左右两边有圆角,下面没有圆角

简介: 如何是实现上面左右两边有圆角,下面没有圆角

画圆角参考:《如何实现画圆角》。

方法一:自己画圆角。

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, ERectangleCornerType)
{
    ERectangleCornerTypeNoCorner = 0,     //无圆角
    ERectangleCornerTypeUpLeftRight = 1,     //上部左右两个圆角
    ERectangleCornerTypeDownLeftRight = 2,   //下部左右两个圆角
    ERectangleCornerTypeLeftUpDown = 3,   //左边上下两个个圆角
    ERectangleCornerTypeRighUpDown = 4,   //右边上下两个圆角
    ERectangleCornerTypeAllCorner = 5,   //上下部左右四个圆角
};

@interface PPRectangleCornerView : BGBaseView
@property (nonatomic, strong) UIColor *fillColor;
@property (nonatomic, assign) CGFloat radius;
@property (nonatomic, assign) ERectangleCornerType rectangleCornerType;

- (instancetype)initWithFrame:(CGRect)frame radius:(CGFloat)radius fillColor:(UIColor *)fillColor rectangleCornerType:(ERectangleCornerType)rectangleCornerType;
@end

NS_ASSUME_NONNULL_END
#import "PPRectangleCornerView.h"
#import "PPSectorView.h"

@interface PPRectangleCornerView ()
@property (nonatomic, strong) PPSectorView *leftUpSectorView;
@property (nonatomic, strong) PPSectorView *rightUpSectorView;
@property (nonatomic, strong) PPSectorView *leftDownSectorView;
@property (nonatomic, strong) PPSectorView *rightDownSectorView;
@property (nonatomic, strong) UIView *centerView;
@property (nonatomic, strong) UIView *leftMiddleView;
@property (nonatomic, strong) UIView *rightMiddleView;
@property (nonatomic, strong) UIView *upMiddleView;
@property (nonatomic, strong) UIView *downMiddleView;
@end

@implementation PPRectangleCornerView

- (instancetype)initWithFrame:(CGRect)frame radius:(CGFloat)radius fillColor:(UIColor *)fillColor rectangleCornerType:(ERectangleCornerType)rectangleCornerType
{
    if(radius <= 0 || !fillColor)
    {
        return nil;
    }

    if((ERectangleCornerTypeNoCorner == rectangleCornerType || ERectangleCornerTypeUpLeftRight == rectangleCornerType || ERectangleCornerTypeDownLeftRight == rectangleCornerType || ERectangleCornerTypeAllCorner == rectangleCornerType) &&(frame.size.width <= radius*2 || frame.size.height <= radius*2))
    {
        return nil;
    }
    else if(frame.size.width <= radius || frame.size.height <= radius*2)
    {
        return nil;
    }
    self = [super initWithFrame:frame];
    if (self) {
        [self setAttributesWithRadius:radius fillColor:fillColor rectangleCornerType:rectangleCornerType];
        [self addSubview:self.leftUpSectorView];
        [self addSubview:self.rightUpSectorView];
        [self addSubview:self.leftDownSectorView];
        [self addSubview:self.rightDownSectorView];
        [self addSubview:self.leftMiddleView];
        [self addSubview:self.rightMiddleView];
        [self addSubview:self.upMiddleView];
        [self addSubview:self.downMiddleView];
        [self addSubview:self.centerView];
    }
    return self;
}

-(PPSectorView *)leftUpSectorView{
    if (!_leftUpSectorView) {
        _leftUpSectorView = [[PPSectorView alloc] init];
        if(ERectangleCornerTypeUpLeftRight == self.rectangleCornerType || ERectangleCornerTypeLeftUpDown == self.rectangleCornerType || ERectangleCornerTypeAllCorner == self.rectangleCornerType )
        {
            [_leftUpSectorView setAttributesWithIsBackgroundClear:YES radius:self.radius fillColor:_fillColor sectorType:ESectorTypeLeftUp];
            _leftUpSectorView.frame = CGRectMake(0, 0, self.radius, self.radius);
        }
        else
        {
            [_leftUpSectorView setAttributesWithIsBackgroundClear:YES radius:self.radius fillColor:_fillColor sectorType:ESectorTypeRectangle];
            
            if(ERectangleCornerTypeRighUpDown == self.rectangleCornerType && self.frame.size.width < self.radius*2)
            {
                _leftUpSectorView.frame = CGRectMake(0, 0, (self.frame.size.width-self.radius)/2, self.radius);
            }
            else
            {
                _leftUpSectorView.frame = CGRectMake(0, 0, self.radius, self.radius);
            }
        }
    }
    return _leftUpSectorView;
}

-(PPSectorView *)rightUpSectorView{
    if (!_rightUpSectorView) {
        _rightUpSectorView = [[PPSectorView alloc] init];
        if(ERectangleCornerTypeUpLeftRight == self.rectangleCornerType || ERectangleCornerTypeRighUpDown == self.rectangleCornerType || ERectangleCornerTypeAllCorner == self.rectangleCornerType )
        {
            [_rightUpSectorView setAttributesWithIsBackgroundClear:YES radius:self.radius fillColor:_fillColor sectorType:ESectorTypeRightUp];
            _rightUpSectorView.frame = CGRectMake(self.frame.size.width-self.radius, 0, self.radius, self.radius);
        }
        else
        {
            [_rightUpSectorView setAttributesWithIsBackgroundClear:YES radius:self.radius fillColor:_fillColor sectorType:ESectorTypeRectangle];
            if(ERectangleCornerTypeLeftUpDown == self.rectangleCornerType && self.frame.size.width < self.radius*2)
            {
                _rightUpSectorView.frame = CGRectMake(self.radius + (self.frame.size.width-self.radius)/2, 0, (self.frame.size.width-self.radius)/2, self.radius);
            }
            else
            {
                _rightUpSectorView.frame = CGRectMake(self.frame.size.width-self.radius, 0, self.radius, self.radius);
            }
            
        }
    }
    return _rightUpSectorView;
}

-(PPSectorView *)leftDownSectorView{
    if (!_leftDownSectorView) {
        _leftDownSectorView = [[PPSectorView alloc] init];
        if(ERectangleCornerTypeDownLeftRight == self.rectangleCornerType || ERectangleCornerTypeLeftUpDown == self.rectangleCornerType || ERectangleCornerTypeAllCorner == self.rectangleCornerType )
        {
            [_leftDownSectorView setAttributesWithIsBackgroundClear:YES radius:self.radius fillColor:_fillColor sectorType:ESectorTypeLeftDown];
            _leftDownSectorView.frame = CGRectMake(0, self.frame.size.height- self.radius, self.radius, self.radius);
        }
        else
        {
            if((ERectangleCornerTypeRighUpDown == self.rectangleCornerType) && self.frame.size.width < self.radius*2)
            {
                _leftDownSectorView.frame = CGRectMake(0, self.frame.size.height- self.radius, (self.frame.size.width-self.radius)/2, self.radius);
            }
            else
            {
                _leftDownSectorView.frame = CGRectMake(0, self.frame.size.height- self.radius, self.radius, self.radius);
            }
            [_leftDownSectorView setAttributesWithIsBackgroundClear:YES radius:self.radius fillColor:_fillColor sectorType:ESectorTypeRectangle];
        }
    }
    return _leftDownSectorView;
}

-(PPSectorView *)rightDownSectorView{
    if (!_rightDownSectorView) {
        _rightDownSectorView = [[PPSectorView alloc] init];
        if(ERectangleCornerTypeDownLeftRight == self.rectangleCornerType || ERectangleCornerTypeRighUpDown == self.rectangleCornerType || ERectangleCornerTypeAllCorner == self.rectangleCornerType )
        {
            [_rightDownSectorView setAttributesWithIsBackgroundClear:YES radius:self.radius fillColor:_fillColor sectorType:ESectorTypeRightDown];
            _rightDownSectorView.frame = CGRectMake(self.frame.size.width-self.radius, self.frame.size.height- self.radius, self.radius, self.radius);
        }
        else
        {
            [_rightDownSectorView setAttributesWithIsBackgroundClear:YES radius:self.radius fillColor:_fillColor sectorType:ESectorTypeRectangle];
            if((ERectangleCornerTypeLeftUpDown == self.rectangleCornerType) && self.frame.size.width < self.radius*2)
            {
                _rightDownSectorView.frame = CGRectMake(self.frame.size.width-(self.frame.size.width-self.radius)/2, self.frame.size.height- self.radius, (self.frame.size.width-self.radius)/2, self.radius);
            }
//            else if((ERectangleCornerTypeRighUpDown == self.rectangleCornerType) && self.frame.size.width < self.radius*2)
//            {
//                _rightDownSectorView.frame = CGRectMake(self.frame.size.width-(self.frame.size.width-self.radius)/2, self.frame.size.height- self.radius, (self.frame.size.width-self.radius)/2, self.radius);
//            }
            else
            {
                _rightDownSectorView.frame = CGRectMake(self.frame.size.width-self.radius, self.frame.size.height- self.radius, self.radius, self.radius);
            }
        }
    }
    return _rightDownSectorView;
}

-(UIView *)leftMiddleView{
    if (!_leftMiddleView) {
//        if((ERectangleCornerTypeLeftUpDown == self.rectangleCornerType) && self.frame.size.width < self.radius*2)
//        {
//            _leftMiddleView= [[UIView alloc] initWithFrame:CGRectMake(0, self.radius, self.radius, self.frame.size.height- self.radius*2)];
//        }
//        else
        if((ERectangleCornerTypeRighUpDown == self.rectangleCornerType) && self.frame.size.width < self.radius*2)
        {
            _leftMiddleView= [[UIView alloc] initWithFrame:CGRectMake(0, self.radius, (self.frame.size.width-self.radius)/2, self.frame.size.height- self.radius*2)];
        }
        else
        {
            _leftMiddleView= [[UIView alloc] initWithFrame:CGRectMake(0, self.radius, self.radius, self.frame.size.height- self.radius*2)];
        }
        _leftMiddleView.backgroundColor = self.fillColor;
    }
    return _leftMiddleView;
}

-(UIView *)rightMiddleView{
    if (!_rightMiddleView) {
        if((ERectangleCornerTypeLeftUpDown == self.rectangleCornerType) && self.frame.size.width < self.radius*2)
        {
            _rightMiddleView= [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width - (self.frame.size.width-self.radius)/2, self.radius, (self.frame.size.width-self.radius)/2, self.frame.size.height- self.radius*2)];
        }
//        else if((ERectangleCornerTypeRighUpDown == self.rectangleCornerType) && self.frame.size.width < self.radius*2)
//        {
//            _rightMiddleView= [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width - self.radius, self.radius, self.radius, self.frame.size.height- self.radius*2)];
//        }
        else
        {
            _rightMiddleView= [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width - self.radius, self.radius, self.radius, self.frame.size.height- self.radius*2)];
        }

        _rightMiddleView.backgroundColor = self.fillColor;
    }
    return _rightMiddleView;
}

-(UIView *)upMiddleView{
    if (!_upMiddleView) {
        if((ERectangleCornerTypeLeftUpDown == self.rectangleCornerType) && self.frame.size.width < self.radius*2)
        {
            _upMiddleView = [[UIView alloc] initWithFrame:CGRectMake(self.radius, 0, (self.frame.size.width-self.radius)/2, self.radius)];
        }
        else if((ERectangleCornerTypeRighUpDown == self.rectangleCornerType) && self.frame.size.width < self.radius*2)
        {
            _upMiddleView = [[UIView alloc] initWithFrame:CGRectMake((self.frame.size.width-self.radius)/2, 0, (self.frame.size.width-self.radius)/2, self.radius)];
        }
        else
        {
            _upMiddleView = [[UIView alloc] initWithFrame:CGRectMake(self.radius, 0, self.frame.size.width - self.radius*2, self.radius)];
        }
        
        _upMiddleView.backgroundColor = self.fillColor;
    }
    return _upMiddleView;
}

-(UIView *)downMiddleView{
    if (!_downMiddleView) {
        if((ERectangleCornerTypeLeftUpDown == self.rectangleCornerType) && self.frame.size.width < self.radius*2)
        {
            _downMiddleView= [[UIView alloc] initWithFrame:CGRectMake(self.radius, self.frame.size.height- self.radius, (self.frame.size.width-self.radius)/2, self.radius)];
        }
        else if((ERectangleCornerTypeRighUpDown == self.rectangleCornerType) && self.frame.size.width < self.radius*2)
        {
            _downMiddleView= [[UIView alloc] initWithFrame:CGRectMake((self.frame.size.width-self.radius)/2, self.frame.size.height- self.radius, (self.frame.size.width-self.radius)/2, self.radius)];
        }
        else
        {
            _downMiddleView= [[UIView alloc] initWithFrame:CGRectMake(self.radius, self.frame.size.height- self.radius, self.frame.size.width - self.radius*2, self.radius)];
        }
        _downMiddleView.backgroundColor = self.fillColor;
    }
    return _downMiddleView;
}

-(UIView *)centerView{
    if (!_centerView) {
        if((ERectangleCornerTypeLeftUpDown == self.rectangleCornerType) && self.frame.size.width < self.radius*2)
        {
            _centerView = [[UIView alloc] initWithFrame:CGRectMake(self.radius, self.radius, (self.frame.size.width-self.radius)/2, self.frame.size.height - self.radius*2)];
        }
        else if((ERectangleCornerTypeRighUpDown == self.rectangleCornerType) && self.frame.size.width < self.radius*2)
        {
            _centerView = [[UIView alloc] initWithFrame:CGRectMake((self.frame.size.width-self.radius)/2, self.radius, (self.frame.size.width-self.radius)/2, self.frame.size.height - self.radius*2)];
        }
        else
        {
            _centerView = [[UIView alloc] initWithFrame:CGRectMake(self.radius, self.radius, self.frame.size.width-self.radius*2, self.frame.size.height - self.radius*2)];
        }
        _centerView.backgroundColor = self.fillColor;
    }
    return _centerView;
}

-(void)setAttributesWithRadius:(CGFloat)radius fillColor:(UIColor *)fillColor rectangleCornerType:(ERectangleCornerType)rectangleCornerType
{
    self.radius = radius;
    self.fillColor = fillColor;
    self.rectangleCornerType = rectangleCornerType;
    self.backgroundColor = [UIColor clearColor];
}
@end

方法二:用贝塞尔曲线实现。

typedef NS_OPTIONS(NSUInteger, UIRectCorner) {

UIRectCornerTopLeft = 1 << 0,

UIRectCornerTopRight = 1 << 1,

UIRectCornerBottomLeft = 1 << 2,

UIRectCornerBottomRight = 1 << 3,

UIRectCornerAllCorners = ~0UL

};

    CGSize radii = CGSizeMake(15, 15);
    // 随意改变4个圆角
    UIRectCorner corners = UIRectCornerTopLeft | UIRectCornerTopRight;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:_photoImageView.bounds byRoundingCorners:corners cornerRadii:radii];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = path.CGPath;
    maskLayer.masksToBounds = NO;
    _photoImageView.layer.mask = maskLayer;
目录
相关文章
|
2月前
|
前端开发
css 设置背景色渐变、字体颜色渐变
css 设置背景色渐变、字体颜色渐变
|
1月前
|
前端开发
边框宽度
边框宽度。
27 9
|
4月前
|
前端开发 UED 容器
掌握内边距、外边距、边框的使用场景与用掌握内边距、外边距、边框的使用场景与用法
掌握内边距、外边距、边框的使用场景与用掌握内边距、外边距、边框的使用场景与用法
|
5月前
|
前端开发
canvas系列教程02——圆、弧线、圆角矩形、曲线(气泡、心形、N叶草)、扇形
canvas系列教程02——圆、弧线、圆角矩形、曲线(气泡、心形、N叶草)、扇形
53 0
|
7月前
|
前端开发
边框宽度和边框颜色
边框宽度和边框颜色。
41 1
|
前端开发 JavaScript
【前端】相对定位实现十字居中(上下左右居中)
【前端】相对定位实现十字居中(上下左右居中)
101 0
Core Animation - 如何来绘制三个圆角一个直角的矩形
Core Animation - 如何来绘制三个圆角一个直角的矩形
80 0
shape颜色渐变、圆角、半圆角、边框、填充
shape颜色渐变、圆角、半圆角、边框、填充
456 0
shape颜色渐变、圆角、半圆角、边框、填充
下一篇
DataWorks