Ios 缩放时调整CALayer边框宽度的大小

Ios 缩放时调整CALayer边框宽度的大小,ios,objective-c,Ios,Objective C,在头文件中: @property (strong,nonatomic) IBOutlet UIView *zoomView; 在m文件中,我使用一些大小、颜色和边框宽度初始化zoomView: - (void)viewDidLoad { [super viewDidLoad]; //initialize the zoom rectangle [zoomView.layer setBorderColor: [[UIColor orangeColor] CGColor]]

在头文件中:

@property (strong,nonatomic) IBOutlet UIView *zoomView;
在m文件中,我使用一些大小、颜色和边框宽度初始化zoomView:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //initialize the zoom rectangle
    [zoomView.layer setBorderColor: [[UIColor orangeColor] CGColor]];
    [zoomView.layer setBorderWidth: 4.0];
    [zoomView.layer setFrame: CGRectMake(zoomView.layer.frame.origin.x, zoomView.layer.frame.origin.y, 100, 200)];
}
然后,对于一些用户操作,特别是挤压,我称之为以下代码:

-(void)redrawBox {
    [zoomView.layer setBorderWidth: 4.0];
    [zoomView.layer setNeedsLayout];
}
不幸的是,边界宽度变化很大。当长方体缩小时,边框宽度按比例增加,结果看起来非常糟糕。我仔细检查了这段代码是否被调用,边框宽度是否设置正确。如果我将宽度设置为0,则框将消失,但边框宽度为4.0在不同的缩放级别下的渲染效果似乎非常不同


无论矩形如何缩放,如何将边框宽度设置为常量?谢谢

尝试使用CATiledLayer而不是CALayer。这个类知道伸缩性。 您可以使用属性levelsOfDetail和levelsOfDetailsBias设置视图所需的分辨率

我不确定这是否能解决你的问题,但我认为值得一试


希望有帮助

我意识到问题在于变换仍然设置在长方体上,因此它会缩放边框。我提出的解决方案是:

-(void)redrawBox {
    //set the border width again so that the width doesn't change with zooming
   CGRect currentFrame = zoomView.layer.frame;
    zoomView.transform = CGAffineTransformIdentity;
    [zoomView.layer setBorderWidth: 4.0];
    [zoomView.layer setFrame: CGRectMake(currentFrame.origin.x, currentFrame.origin.y, currentFrame.size.width, currentFrame.size.height)];
}

这将从头开始以正确的大小重新绘制长方体,但没有变换集,因此边框实际上是4个点。

此代码在缩放UIScrollView时保持CALayer边框的固定宽度

- (void) scrollViewDidZoom:(UIScrollView *)scrollView
{
    CGFloat newBorderWidth = UIScreen.mainScreen.scale / scrollView.zoomScale;
    zoomView.layer.borderWidth = newBorderWidth;
}

我现在使用的CALayer是UIView的一部分。有没有办法让UIView改用CATiledLayer?@quantka是的,您可以覆盖+ClasslayerClass;虽然这个代码片段可以回答这个问题,但它没有提供任何上下文来解释如何或为什么。考虑一两句话来解释你的答案。