【问题标题】:Create topLeft and topRight corners with border?创建带边框的 topLeft 和 topRight 角?
【发布时间】:2020-02-25 16:00:30
【问题描述】:

此代码仅创建 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];

【问题讨论】:

  • 您应该使用 colorSliderBackgroundView 的边界更改来更新 shapelayer 的路径。
  • @Cy-4AH,你能写代码吗
  • @iOS - 你希望colorSliderBackgroundView 被勾勒出来,顶部圆角吗?或者你想让colorSliderBackgroundView 有一个sublayer,它是一个带有圆角的轮廓矩形?

标签: ios objective-c uiview


【解决方案1】:

您只需要这样做:

UIView *view = [[UIView alloc] initWithFrame:frame];

CALayer *layer = [CALayer layer]; 
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRoundedRect:frame byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) 
 cornerRadii:CGSizeMake(3.0, 3.0)]; 
layer.shadowPath = shadowPath.CGPath; 
view.layer.mask = layer;

或者您可以查看此链接以获取更多详细信息

how to set cornerRadius for only bottom-left,bottom-right and top-left corner textview?

【讨论】:

    【解决方案2】:

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

    创建一个新的视图类,并将其设置为您的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,您甚至可以在设计时看到结果:

    【讨论】:

    • 嗯...我应该在 cmets 中要求澄清 OP 的目标。
    • @matt - 作为旁注,我工作的应用程序仍然支持回 iOS 9...自从.maskedCorners 进入 iOS 11 以来,这不是我想到的第一个方法。
    猜你喜欢
    • 2020-05-07
    • 2015-03-18
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    相关资源
    最近更新 更多