首页 > 解决方案 > 创建带边框的 topLeft 和 topRight 角?

问题描述

此代码仅创建 topLeft 边框,但我也想要 topRight。如何?

UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.colorSliderBackgroundView.bounds
                             byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
                                   cornerRadii:CGSizeMake(10.0,10.0)];

CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init];
borderLayer.frame = self.colorSliderBackgroundView.bounds;
borderLayer.path  = maskPath.CGPath;

borderLayer.lineWidth   = 1.5f;
borderLayer.strokeColor = [UIColor colorWithRed:243.0/255.0 green:243.0/255.0 blue:243.0/255.0 alpha:1.0].CGColor;
borderLayer.fillColor   = [UIColor clearColor].CGColor;

[self.colorSliderBackgroundView.layer addSublayer:borderLayer];

标签: iosobjective-cuiview

解决方案


根据您的代码,您似乎想要一个左上角和右上角圆角的轮廓矩形,作为另一个视图的子层......

创建一个新的视图类,并将其设置为您的自定义类colorSliderBackgroundView


TopCornersRoundedView.h

//
//  TopCornersRoundedView.h
//
//  Created by Don Mag on 10/30/19.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

IB_DESIGNABLE

@interface TopCornersRoundedView : UIView

@end

NS_ASSUME_NONNULL_END

TopCornersRoundedView.m

//
//  TopCornersRoundedView.m
//  ObjCXIBTest
//
//  Created by Don Mag on 10/30/19.
//  Copyright © 2019 Don Mag. All rights reserved.
//

#import "TopCornersRoundedView.h"

@interface TopCornersRoundedView ()

@property (strong, nonatomic) CAShapeLayer *borderLayer;

@end

@implementation TopCornersRoundedView

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self commonInit];
    }
    return self;
}

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

- (void)prepareForInterfaceBuilder {
    [super prepareForInterfaceBuilder];
    [self commonInit];
}

- (void) commonInit {

    // instantiate the shape layer
    _borderLayer = [CAShapeLayer new];

    // set line width, stroke and fill colors
    _borderLayer.lineWidth   = 1.5f;
    _borderLayer.strokeColor = [UIColor colorWithRed:243.0/255.0 green:243.0/255.0 blue:243.0/255.0 alpha:1.0].CGColor;
    _borderLayer.fillColor   = [UIColor clearColor].CGColor;

    // add the shape layer as a sublayer of self
    [self.layer addSublayer:_borderLayer];

}

- (void)layoutSubviews {
    [super layoutSubviews];

    // create a bezier path with top left and right corners rounded
    // doing this in layoutSubviews will keep the frame size correct when
    // the view changes size
    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                     byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
                                           cornerRadii:CGSizeMake(10.0,10.0)];

    _borderLayer.frame = self.bounds;
    _borderLayer.path  = maskPath.CGPath;

}

@end

通过指定此类,IB_DESIGNABLE您甚至会在设计时看到结果:

在此处输入图像描述


推荐阅读