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

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

#import 

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

如何是实现上面左右两边有圆角,下面没有圆角_第1张图片

你可能感兴趣的:(ios)