gpt4 book ai didi

ios - 创建带边框的左上角和右上角?

转载 作者:行者123 更新时间:2023-11-28 23:26:15 29 4
gpt4 key购买 nike

此代码仅创建左上边框,但我也想要右上边框。怎么办?

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];

最佳答案

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

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

enter image description here

关于ios - 创建带边框的左上角和右上角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58623926/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com