我想在UIImageView上使用CALayer添加虛線邊框。我找到了一種方法,但它是在迅速工作,我如何將它轉換爲快速? o另一個具有邊框的imageView將是使用CALayer的最佳解決方案,所以它們看起來相似?我如何獲得這個虛線邊界UIImageView swift
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;
}
感謝@jsiodtb添加該行。 – 2015-03-18 10:07:43
這對我有用!謝謝 – 2015-05-14 15:48:50
精美的作品,我編輯了一下,使它更通用。非常感謝! – n13 2016-02-15 01:42:49