ios - 创建带边框的 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];
解决方案
根据您的代码,您似乎想要一个左上角和右上角圆角的轮廓矩形,作为另一个视图的子层......
创建一个新的视图类,并将其设置为您的自定义类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
您甚至会在设计时看到结果:
推荐阅读
- sql-server - 辅助节点的 SharePoint 2013 SQL 群集问题
- javascript - HowTo 在 drawrect d3 中选择 svg 对象
- c# - 启动期间加载记录器配置需要很长时间
- java - 为什么 java.time 使用 'of' 而不是 'new' 作为日期?
- javascript - 在 Cytoscape JS 中选择节点时更改颜色
- apache - apache2 服务器状态页面使用 mod_perl 并且没有 apache 工作者
- timezone - 在构建奇点图像时确定时区
- sql - CTE SQL Server:语句完成前最大递归100已用完
- amcharts4 - amcharts - 在类别之间添加刻度
- ecmascript-6 - Es6 程序中的类和构造函数