美文网首页
iOS-简单高效的圆角实现TKRoundedView源码

iOS-简单高效的圆角实现TKRoundedView源码

作者: 见哥哥长高了 | 来源:发表于2019-07-06 22:57 被阅读0次
TKRoundedView.h
typedef NS_OPTIONS(NSUInteger, TKRoundedCorner) {
    TKRoundedCornerNone         = 0,
    TKRoundedCornerTopRight     = 1 <<  0,
    TKRoundedCornerBottomRight  = 1 <<  1,
    TKRoundedCornerBottomLeft   = 1 <<  2,
    TKRoundedCornerTopLeft      = 1 <<  3,
};

typedef NS_OPTIONS(NSUInteger, TKDrawnBorderSides) {
    TKDrawnBorderSidesNone      = 0,
    TKDrawnBorderSidesRight     = 1 <<  0,
    TKDrawnBorderSidesLeft      = 1 <<  1,
    TKDrawnBorderSidesTop       = 1 <<  2,
    TKDrawnBorderSidesBottom    = 1 <<  3,
};

typedef NS_ENUM(NSInteger, TKGradientDirection) {
    TKGradientDirectionHorizontal,
    TKGradientDirectionVertical,
    TKGradientDirectionUp,
    TKGradientDirectionDown,
};

extern const TKRoundedCorner TKRoundedCornerAll;
extern const TKDrawnBorderSides TKDrawnBorderSidesAll;

UIImage * TKRoundedCornerImage(CGSize size,
                               TKRoundedCorner corners,
                               TKDrawnBorderSides drawnBorders,
                               UIColor *fillColor,
                               UIColor *borderColor,
                               CGFloat borderWidth,
                               CGFloat cornerRadius);

@interface TKRoundedView : UIView

/* Which borders should be drawn - default TKDrawnBordersSidesAll - only not rounded borders can *NOT* be drawn atm  */
@property (nonatomic, assign) TKDrawnBorderSides drawnBordersSides;

/* Which corners should be rounded - default TKRoundedCornerAll */
@property (nonatomic, assign) TKRoundedCorner roundedCorners;

/* Fill color of the figure - default white */
@property (nonatomic, strong) UIColor *fillColor;

/* Stroke color for the figure, default is light gray */
@property (nonatomic, strong) UIColor *borderColor;

/* Border line width, default 1.0f */
@property (nonatomic, assign) CGFloat borderWidth;

/* Corners radius , default 15.0f */
@property (nonatomic, assign) CGFloat cornerRadius;

/* Direction of the gradient [options -, |,  /,  \] (if there will be a gradient drawn), default vertical  */
@property (nonatomic, assign) TKGradientDirection gradientDirection;

/* NSArray of NSDictionaries with NSNumber with color's locations and the UIColor object, default nil  */
@property (nonatomic, strong) NSArray *gradientColorsAndLocations;

@end
TKRoundedView.m
#import "TKRoundedView.h"
#import <QuartzCore/QuartzCore.h>

const TKRoundedCorner TKRoundedCornerAll = TKRoundedCornerTopRight | TKRoundedCornerBottomRight | TKRoundedCornerBottomLeft | TKRoundedCornerTopLeft;

const TKDrawnBorderSides TKDrawnBorderSidesAll = TKDrawnBorderSidesRight | TKDrawnBorderSidesLeft | TKDrawnBorderSidesTop | TKDrawnBorderSidesBottom;

UIImage * TKRoundedCornerImage(CGSize size,
                               TKRoundedCorner corners,
                               TKDrawnBorderSides drawnBorders,
                               UIColor *fillColor,
                               UIColor *borderColor,
                               CGFloat borderWidth,
                               CGFloat cornerRadius){
    
    TKRoundedView *view = [[TKRoundedView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, size.width, size.height)];
    view.roundedCorners = corners;
    view.drawnBordersSides = drawnBorders;
    view.fillColor = fillColor;
    view.borderColor = borderColor;
    view.borderWidth = borderWidth;
    view.cornerRadius = cornerRadius;
    
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    view = nil;
    
    return img;
    
}

@interface TKRoundedView (){
    CGColorSpaceRef _colorSpace;
    CGFloat* _locationsTable;
    CFArrayRef _cfColors;
    CGGradientRef _gradient;
    NSArray *_observableKeys;
}
@end

@implementation TKRoundedView

#pragma mark - Initialization

- (void)dealloc{
    
    [self.observableKeys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [self removeObserver:self forKeyPath:obj];
    }];
    
    if(_locationsTable != NULL)
        free(_locationsTable);
    
    if (_cfColors != NULL) {
        CFRelease(_cfColors);
    }
    
    if (_colorSpace != NULL) {
        CGColorSpaceRelease(_colorSpace);
    }
    
    if (_gradient != NULL) {
        CGGradientRelease(_gradient);
    }
}

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (!self)return nil;
    
    [self commonInit];
    
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (!self)return nil;
    
    [self commonInit];
    
    return self;
}

- (void)commonInit{
    
    self.opaque = NO;
    self.backgroundColor = [UIColor clearColor];
    self.contentMode = UIViewContentModeRedraw;
    
    _gradientDirection = TKGradientDirectionVertical;
    _fillColor = [UIColor whiteColor];
    _borderColor = [UIColor lightGrayColor];
    _cornerRadius = 15.0f;
    _borderWidth = 1.0f;
    _roundedCorners = TKRoundedCornerAll;
    _drawnBordersSides = TKDrawnBorderSidesAll;
    
    [self.observableKeys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [self addObserver:self forKeyPath:obj options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
    }];
}

#pragma mark - Drawing

- (void)drawRect:(CGRect)rect{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGFloat halfLineWidth = _borderWidth / 2.0f;
    
    CGFloat topInsets = _drawnBordersSides & TKDrawnBorderSidesTop ? halfLineWidth : -halfLineWidth;
    CGFloat leftInsets = _drawnBordersSides & TKDrawnBorderSidesLeft ? halfLineWidth : -halfLineWidth;
    CGFloat rightInsets = _drawnBordersSides & TKDrawnBorderSidesRight ? halfLineWidth : -halfLineWidth;
    CGFloat bottomInsets = _drawnBordersSides & TKDrawnBorderSidesBottom ? halfLineWidth : -halfLineWidth;
    
    UIEdgeInsets insets = UIEdgeInsetsMake(topInsets, leftInsets, bottomInsets, rightInsets);
    
    CGRect properRect = UIEdgeInsetsInsetRect(rect, insets);
    
    /* Setup line width */
    CGContextSetLineWidth(ctx, 0.0f);
    
    // Add and fill rect
    [self addPathToContext:ctx inRect:properRect respectDrawnBorder:NO];
    
    // Close the path
    CGContextClosePath(ctx);
    
    if (_gradientColorsAndLocations.count) {
        CGContextSaveGState(ctx);
        CGContextClip(ctx);
        [self drawGradientToContext:ctx inRect:rect];
        CGContextRestoreGState(ctx);
    }
    else{
        // Fill and Stroke path
        CGContextSetFillColorWithColor(ctx, _fillColor.CGColor);
        CGContextDrawPath(ctx, kCGPathFill);
    }
    
    /* Setup colors and line width */
    CGContextSetStrokeColorWithColor(ctx, _borderColor.CGColor);
    CGContextSetLineWidth(ctx, _borderWidth);
    
    // Add and stroke rect
    [self addPathToContext:ctx inRect:properRect respectDrawnBorder:YES];
    
    // Fill and Stroke path
    CGContextDrawPath(ctx, kCGPathStroke);
    
}

- (void)addPathToContext:(CGContextRef)ctx inRect:(CGRect)rect respectDrawnBorder:(BOOL)respectDrawnBorders{
    
    CGFloat minx = CGRectGetMinX(rect);
    CGFloat midx = CGRectGetMidX(rect);
    CGFloat maxx = CGRectGetMaxX(rect);
    
    CGFloat miny = CGRectGetMinY(rect);
    CGFloat midy = CGRectGetMidY(rect);
    CGFloat maxy = CGRectGetMaxY(rect);
    
    
    CGContextMoveToPoint(ctx, minx, midy);
    
    /* Top Left Corner */
    if (_roundedCorners & TKRoundedCornerTopLeft && _drawnBordersSides & (TKDrawnBorderSidesTop | TKDrawnBorderSidesLeft)) {
        CGContextAddArcToPoint(ctx, minx, miny, midx, miny, _cornerRadius);
        CGContextAddLineToPoint(ctx, midx, miny);
    }
    else{
        
        if (_drawnBordersSides & TKDrawnBorderSidesLeft || !respectDrawnBorders){
            CGContextAddLineToPoint(ctx, minx, miny);
        }
        else{
            CGContextDrawPath(ctx, kCGPathStroke);
            CGContextMoveToPoint(ctx, minx, miny);
        }
        
        if (_drawnBordersSides & TKDrawnBorderSidesTop  || !respectDrawnBorders){
            CGContextAddLineToPoint(ctx, midx, miny);
        }
        else{
            CGContextDrawPath(ctx, kCGPathStroke);
            CGContextMoveToPoint(ctx, midx, miny);
        }
    }
    
    /* Top Right Corner */
    if (_roundedCorners & TKRoundedCornerTopRight && _drawnBordersSides & (TKDrawnBorderSidesTop | TKDrawnBorderSidesRight)) {
        CGContextAddArcToPoint(ctx, maxx, miny, maxx, midy, _cornerRadius);
        CGContextAddLineToPoint(ctx, maxx, midy);
    }
    else{
        
        if (_drawnBordersSides & TKDrawnBorderSidesTop  || !respectDrawnBorders){
            CGContextAddLineToPoint(ctx, maxx, miny);
        }
        else{
            CGContextDrawPath(ctx, kCGPathStroke);
            CGContextMoveToPoint(ctx, maxx, miny);
        }
        
        if (_drawnBordersSides & TKDrawnBorderSidesRight  || !respectDrawnBorders){
            CGContextAddLineToPoint(ctx, maxx, midy);
        }
        else{
            CGContextDrawPath(ctx, kCGPathStroke);
            CGContextMoveToPoint(ctx, maxx, midy);
        }
    }
    
    /* Bottom Right Corner */
    if (_roundedCorners & TKRoundedCornerBottomRight && _drawnBordersSides & (TKDrawnBorderSidesBottom | TKDrawnBorderSidesRight)) {
        CGContextAddArcToPoint(ctx, maxx, maxy, midx, maxy, _cornerRadius);
        CGContextAddLineToPoint(ctx, midx, maxy);
        
    }
    else{
        
        if (_drawnBordersSides & TKDrawnBorderSidesRight  || !respectDrawnBorders){
            CGContextAddLineToPoint(ctx, maxx, maxy);
        }
        else{
            CGContextDrawPath(ctx, kCGPathStroke);
            CGContextMoveToPoint(ctx, maxx, maxy);
        }
        
        if (_drawnBordersSides & TKDrawnBorderSidesBottom  || !respectDrawnBorders){
            CGContextAddLineToPoint(ctx, midx, maxy);
        }
        else{
            CGContextDrawPath(ctx, kCGPathStroke);
            CGContextMoveToPoint(ctx, midx, maxy);
        }
    }
    
    /* Bottom Left Corner */
    if (_roundedCorners & TKRoundedCornerBottomLeft && _drawnBordersSides & (TKDrawnBorderSidesBottom | TKDrawnBorderSidesLeft)) {
        CGContextAddArcToPoint(ctx, minx, maxy, minx, midy, _cornerRadius);
        CGContextAddLineToPoint(ctx, minx, midy);
    }
    else{
        
        if (_drawnBordersSides & TKDrawnBorderSidesBottom  || !respectDrawnBorders){
            CGContextAddLineToPoint(ctx, minx, maxy);
        }
        else{
            CGContextDrawPath(ctx, kCGPathStroke);
            CGContextMoveToPoint(ctx, minx, maxy);
        }
        
        if (_drawnBordersSides & TKDrawnBorderSidesLeft  || !respectDrawnBorders){
            CGContextAddLineToPoint(ctx, minx, midy);
        }
        else{
            CGContextMoveToPoint(ctx, minx, midy);
            CGContextDrawPath(ctx, kCGPathStroke);
        }
        
    }
    
}

- (void)drawGradientToContext:(CGContextRef)ctx inRect:(CGRect)rect{
    
    if (_gradient == NULL) {
        _gradient = CGGradientCreateWithColors(_colorSpace,_cfColors, _locationsTable);
    }
    
    CGPoint startPoint = CGPointZero;
    CGPoint endPoint = CGPointZero;
    
    switch (_gradientDirection) {
        case TKGradientDirectionVertical:
            startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
            endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
            break;
        case TKGradientDirectionHorizontal:
            startPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMidY(rect));
            endPoint = CGPointMake(CGRectGetMaxX(rect), CGRectGetMidY(rect));
            break;
        case TKGradientDirectionDown:
            startPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect));
            endPoint = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect));
            break;
        case TKGradientDirectionUp:
            startPoint = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect));
            endPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect));
            break;
            
        default:
            break;
    }
    
    CGContextSaveGState(ctx);
    CGContextAddRect(ctx, rect);
    CGContextClip(ctx);
    CGContextDrawLinearGradient(ctx, _gradient, startPoint, endPoint, 0);
    CGContextRestoreGState(ctx);
    
}

//#pragma mark - Setters
//
//- (void)setDrawnBordersSides:(TKDrawnBorderSides)drawnBordersSides{
//    _drawnBordersSides = drawnBordersSides;
//    [self setNeedsDisplay];
//}
//
//- (void)setRoundedCorners:(TKRoundedCorner)roundedCorners{
//    _roundedCorners = roundedCorners;
//    [self setNeedsDisplay];
//}
//
//- (void)setBorderColor:(UIColor *)borderColor{
//    if (_borderColor != borderColor) {
//        _borderColor = borderColor;
//        [self setNeedsDisplay];
//    }
//}
//
//- (void)setFillColor:(UIColor *)fillColor{
//    if (_fillColor != fillColor) {
//        _fillColor = fillColor;
//        [self setNeedsDisplay];
//    }
//}
//
//- (void)setGradientColorsAndLocations:(NSArray *)gradientColorsAndLocations{
//    if (_gradientColorsAndLocations != gradientColorsAndLocations) {
//        _gradientColorsAndLocations = gradientColorsAndLocations;
//        [self prepareGradient];
//        [self setNeedsDisplay];
//    }
//}
//
//- (void)setGradientDirection:(TKGradientDirection)gradientDirection{
//    if (_gradientDirection != gradientDirection) {
//        _gradientDirection = gradientDirection;
//        [self setNeedsDisplay];
//    }
//}
//
//- (void)setBorderWidth:(CGFloat)borderWidth{
//    _borderWidth = borderWidth;
//    [self setNeedsDisplay];
//}
//
//- (void)setCornerRadius:(CGFloat)cornerRadius{
//    _cornerRadius = cornerRadius;
//    [self setNeedsDisplay];
//}


#pragma mark - Key-Value Observing

+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key{
    return YES;
}


- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"gradientColorsAndLocations"])
    {
        [self prepareGradient];
        [self setNeedsDisplay];
        return ;
    }
    
    [self.observableKeys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
        if ([keyPath isEqualToString:obj])
        {
            [self setNeedsDisplay];
            return;
        }
    }];
}


- (void)didChange:(NSKeyValueChange)changeKind valuesAtIndexes:(NSIndexSet *)indexes forKey:(NSString *)key{
    
    if ([key isEqualToString:@"gradientColorsAndLocations"])
    {
        [self prepareGradient];
        [self setNeedsDisplay];
    }
}

- (NSArray *)observableKeys{
    if (!_observableKeys) {
        _observableKeys = (@[
                             @"drawnBordersSides",
                             @"roundedCorners",
                             @"fillColor",
                             @"borderColor",
                             @"borderWidth",
                             @"cornerRadius",
                             @"gradientDirection",
                             @"gradientColorsAndLocations"
                             ]);
    }
    return _observableKeys;
}

#pragma mark - Private

- (void)prepareGradient{
    NSMutableArray *colors = [NSMutableArray arrayWithCapacity:_gradientColorsAndLocations.count];
    NSMutableArray *locations = [NSMutableArray arrayWithCapacity:_gradientColorsAndLocations.count];
    
    for (NSDictionary *dictionary in self.gradientColorsAndLocations) {
        if ([dictionary isKindOfClass:[NSDictionary class]]) {
            for (NSString *key in dictionary) {
                id object = dictionary[key];
                if ([object isKindOfClass:[NSNumber class]]) {
                    [locations addObject:object];
                }
                else if ([object isKindOfClass:[UIColor class]]){
                    [colors addObject:object];
                }
            }
        }
    }
    
    if (colors.count == locations.count) {
        
        if (_colorSpace == NULL) {
            _colorSpace = CGColorSpaceCreateDeviceRGB();
        }
        
        NSInteger count = locations.count;
        
        
        if (_locationsTable != NULL) {
            free(_locationsTable);
        }
        
        _locationsTable = malloc((size_t) sizeof(CGFloat) * count);
        
        CFMutableArrayRef cfColorsMutable = CFArrayCreateMutable(kCFAllocatorDefault, count, &kCFTypeArrayCallBacks);
        
        for(int i = 0; i < count; i++){
            NSNumber *locationNumber = locations[i];
            _locationsTable[i] = [locationNumber floatValue];
            CGColorRef color = [colors[i] CGColor];
            CFArrayAppendValue(cfColorsMutable, color);
        }
        
        if (_cfColors != NULL) {
            CFRelease(_cfColors);
        }
        _cfColors = CFArrayCreateCopy(kCFAllocatorDefault, cfColorsMutable);
        
        CFRelease(cfColorsMutable);
        
        if (_gradient != NULL) {
            CGGradientRelease(_gradient);
            _gradient = NULL;
        }
    }
}

@end
使用
#import "TKViewController.h"
#import "TKRoundedView.h"

@interface TKViewController ()
@property (nonatomic, strong) UISwitch *aSwitch;
@property (nonatomic, strong) UILabel *label;
@end

@implementation TKViewController

- (id)init{
    self = [super initWithNibName:nil bundle:nil];
    if(!self)return nil;
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];

    self.aSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 90 , self.view.frame.size.height - 34, 88, 44)];
    [self.view addSubview:_aSwitch];
    [_aSwitch addTarget:self action:@selector(switchSwitched:) forControlEvents:UIControlEventValueChanged];
    _aSwitch.on = NO;
    
    [self switchSwitched:_aSwitch];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height - 34, self.view.frame.size.width - 105 , _aSwitch.frame.size.height)];
    _label.text = @"borders on/off";
    _label.textAlignment = NSTextAlignmentCenter;
    _label.textColor = [UIColor blackColor];
    [self.view addSubview:_label];
    
    
    TKRoundedView *roundedView = [[TKRoundedView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    
    roundedView.roundedCorners = TKRoundedCornerBottomLeft;
        
    roundedView.borderColor = [UIColor redColor];
    
    roundedView.fillColor = [UIColor yellowColor];
    
    roundedView.borderWidth = 5.0f;
    
    roundedView.cornerRadius = 20;
    
    [self.view addSubview:roundedView];
    
    
    
    
}

- (void)switchSwitched:(UISwitch *)theSwitch{
    [self showCornersOrBorders:theSwitch.on];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

- (void)showCornersOrBorders:(BOOL)corners{
    
    for (UIView *subview in self.view.subviews) {
        if (subview != _aSwitch && subview != _label) {
            [subview removeFromSuperview];    
        }
    
    }
    
    NSString *colorKey = @"color";
    NSString *locationKey = @"location";
    
    
    TKGradientDirection gradientDirection = TKGradientDirectionDown;
    
    CGFloat offset = 10.0f;
    
    CGFloat side = (self.view.frame.size.width - 4 * offset)/ 3.0f ;
    
    CGRect frame = CGRectMake(offset, offset, side, side);
    
    if (corners) {
        
        TKRoundedCorner cornerOptions[13] = {
            TKRoundedCornerNone,
            TKRoundedCornerAll,
            TKRoundedCornerTopLeft,
            TKRoundedCornerTopRight,
            TKRoundedCornerBottomRight,
            TKRoundedCornerBottomLeft,
            TKRoundedCornerTopLeft | TKRoundedCornerTopRight,
            TKRoundedCornerBottomLeft | TKRoundedCornerBottomRight,
            TKRoundedCornerTopLeft | TKRoundedCornerBottomRight,
            TKRoundedCornerBottomLeft | TKRoundedCornerTopRight,
            TKRoundedCornerTopLeft | TKRoundedCornerTopRight,
            TKRoundedCornerTopLeft | TKRoundedCornerTopRight | TKRoundedCornerBottomRight,
            TKRoundedCornerTopLeft | TKRoundedCornerTopRight | TKRoundedCornerBottomLeft,
        };
        
        NSArray *gradientColorsAndLocations = (@[
                                               @{colorKey: [UIColor blackColor],locationKey: @(0.0f)},
                                               @{colorKey: [UIColor lightGrayColor]  ,locationKey: @(0.5f)},
                                               @{colorKey: [UIColor whiteColor],locationKey: @(1.0f)}
                                               ]);
        
        for (int i = 0; i < 13; i++) {
            
            TKRoundedView *view1 = [[TKRoundedView alloc] initWithFrame:CGRectInset(frame, 10, 10)];
            view1.roundedCorners = cornerOptions[i];
            view1.borderColor = [UIColor colorWithRed:0.123 green:0.435 blue:0.52 alpha:1.0];
            view1.fillColor = [UIColor colorWithWhite:0.6 alpha:0.1];
            view1.borderWidth = 5.0f;
            view1.gradientColorsAndLocations = gradientColorsAndLocations;
            view1.gradientDirection = gradientDirection;
            view1.cornerRadius = side/4;
            [self.view addSubview:view1];
            
            frame.origin.y += side + offset;
            
            if (self.view.frame.size.height < CGRectGetMaxY(frame)) {
                frame.origin.y = offset;
                frame.origin.x += offset + side;
            }
            
        }
    }
    else{
        
        NSArray *gradientColorsAndLocations = (@[
                                               @{colorKey: [UIColor colorWithRed:0.0f green:137.0f/255.0f blue:248.0f/255.0f alpha:1.0f],locationKey: @(0.0f)},
                                               @{colorKey: [UIColor colorWithRed:0.0f green:89.0f/255.0f blue:234.0f/255.0f alpha:1.0f],locationKey: @(1.0f)}
                                               ]);
        
        
        for (int i = 0; i < 10; i++) {
            
            TKRoundedView *view1 = [[TKRoundedView alloc] initWithFrame:frame];
            
            
            if (i == 0) {
                view1.roundedCorners = TKRoundedCornerTopLeft | TKRoundedCornerTopRight;
                view1.drawnBordersSides = TKDrawnBorderSidesLeft | TKDrawnBorderSidesTop | TKDrawnBorderSidesRight;
            }
            else if(i == 1){
                view1.roundedCorners = TKRoundedCornerBottomLeft | TKRoundedCornerBottomRight;
                view1.drawnBordersSides = TKDrawnBorderSidesLeft | TKDrawnBorderSidesBottom | TKDrawnBorderSidesRight;
            }
            else if(i == 2){
                view1.roundedCorners = TKRoundedCornerNone;
                view1.drawnBordersSides = TKDrawnBorderSidesLeft | TKDrawnBorderSidesRight;
            }
            else if(i == 3){
                view1.roundedCorners = TKRoundedCornerAll;
                view1.drawnBordersSides = TKDrawnBorderSidesAll;
            }
            else if(i == 4){
                view1.roundedCorners = TKRoundedCornerBottomLeft | TKRoundedCornerTopLeft;
                view1.drawnBordersSides = TKDrawnBorderSidesLeft | TKDrawnBorderSidesBottom | TKDrawnBorderSidesTop;
            }
            else if(i == 5){
                view1.roundedCorners = TKRoundedCornerBottomRight | TKRoundedCornerTopRight;
                view1.drawnBordersSides = TKDrawnBorderSidesRight | TKDrawnBorderSidesBottom | TKDrawnBorderSidesTop;
            }
            else if(i == 6){
                view1.roundedCorners = TKRoundedCornerNone;
                view1.drawnBordersSides = TKDrawnBorderSidesTop| TKDrawnBorderSidesBottom;
            }
            else if(i == 7){
                view1.roundedCorners = TKRoundedCornerNone;
                view1.drawnBordersSides = TKDrawnBorderSidesAll;
            }
            else if(i == 8){
                view1.roundedCorners = TKRoundedCornerBottomRight;
                view1.drawnBordersSides = TKDrawnBorderSidesRight | TKDrawnBorderSidesBottom;
            }
            else if(i == 9){
                view1.roundedCorners = TKRoundedCornerTopLeft;
                view1.drawnBordersSides = TKDrawnBorderSidesLeft | TKDrawnBorderSidesTop;
            }
            
            view1.borderColor = [UIColor colorWithRed:1.000 green:0.899 blue:0.520 alpha:1.000];
            view1.fillColor = [UIColor redColor];
            view1.gradientColorsAndLocations = gradientColorsAndLocations;
            view1.borderWidth = 5.0f;
            view1.cornerRadius = 30.0f;
            [self.view addSubview:view1];
            
            frame.origin.y += offset + side;
            
            if (self.view.frame.size.height < CGRectGetMaxY(frame)) {
                frame.origin.y = offset;
                frame.origin.x += offset + side;
            }
            
        }
    }
    [self.view bringSubviewToFront:_aSwitch];
}

@end

相关文章

网友评论

      本文标题:iOS-简单高效的圆角实现TKRoundedView源码

      本文链接:https://www.haomeiwen.com/subject/jexvhctx.html