gpt4 book ai didi

ios - 虚线边框 UIImageView swift

转载 作者:搜寻专家 更新时间:2023-10-30 21:48:35 27 4
gpt4 key购买 nike

我正在尝试在 UIImageView 上使用 CALayer 添加虚线边框。我找到了一种方法,但它是否在 swift 中工作,我如何将它转换为 swift? o 有另一个有边框的 imageView,所以使用 CALayer 是最好的解决方案,所以它们看起来很相似?我怎样才能得到这个

obj-c 代码到 swift?

- (CAShapeLayer *) addDashedBorderWithColor: (CGColorRef) color {
CAShapeLayer *shapeLayer = [CAShapeLayer layer];

CGSize frameSize = self.size;

CGRect shapeRect = CGRectMake(0.0f, 0.0f, frameSize.width, frameSize.height);
[shapeLayer setBounds:shapeRect];
[shapeLayer setPosition:CGPointMake( frameSize.width/2,frameSize.height/2)];

[shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
[shapeLayer setStrokeColor:color];
[shapeLayer setLineWidth:5.0f];
[shapeLayer setLineJoin:kCALineJoinRound];
[shapeLayer setLineDashPattern:
[NSArray arrayWithObjects:[NSNumber numberWithInt:10],
[NSNumber numberWithInt:5],
nil]];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:shapeRect cornerRadius:15.0];
[shapeLayer setPath:path.CGPath];

return shapeLayer;
}

最佳答案

好的,我会在自定义 View 类中简单地这样做:

针对 Swift 4 进行了更新

class DashedBorderView: UIView {

let _border = CAShapeLayer()

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

init() {
super.init(frame: .zero)
setup()
}

func setup() {
_border.strokeColor = UIColor.black.cgColor
_border.fillColor = nil
_border.lineDashPattern = [4, 4]
self.layer.addSublayer(_border)
}

override func layoutSubviews() {
super.layoutSubviews()
_border.path = UIBezierPath(roundedRect: self.bounds, cornerRadius:10).cgPath
_border.frame = self.bounds
}
}

关于ios - 虚线边框 UIImageView swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26445894/

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