【发布时间】:2014-10-24 14:56:27
【问题描述】:
我正在屏幕上画一条线,其中包括对角线,而对角线又是这样绘制的:
注意它看起来一点也不平滑。我已经阅读了几篇关于此的文章,似乎 this 看起来更接近我遇到的问题,但该解决方案对我不起作用。
这是我设置图层的方法:
- (void)viewDidLoad
{
[super viewDidLoad];
// Instantiate the navigation line view
CGRect navLineFrame = CGRectMake(0.0f, 120.0f, self.view.frame.size.width, 15.0f);
self.navigationLineView = [[HyNavigationLineView alloc] initWithFrame:navLineFrame];
// Make it's background transparent
self.navigationLineView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.0f];
self.navigationLineView.opaque = NO;
HyNavigationLineLayer * layer = [[HyNavigationLineLayer alloc] init];
layer.shouldRasterize = YES;
layer.rasterizationScale = [UIScreen mainScreen].scale;
layer.contentsScale = [[UIScreen mainScreen] scale];
layer.needsDisplayOnBoundsChange = YES;
[[self.navigationLineView layer] addSublayer:layer];
[self.view addSubview:self.navigationLineView];
[self.navigationLineView.layer setNeedsDisplay];
}
这是我在图层中的绘制方式:
- (void)drawInContext:(CGContextRef)ctx
{
CGFloat bottomInset = 1.0f / [[UIScreen mainScreen] scale] * 2.0f;
CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
GLfloat padding = kHyNavigationLineViewPadding * 4.0f;
GLfloat searchSpace = self.frame.size.width - padding - kHyNavigationLineViewPointerSize * 2.0f;
GLfloat x = kHyNavigationLineViewPadding * 2.0f + searchSpace * self.offset;
CGContextSetLineWidth(ctx, 2.0f);
CGContextMoveToPoint(ctx, kHyNavigationLineViewPadding, 0.0f);
CGContextAddLineToPoint(ctx, x, bottomInset);
CGContextAddLineToPoint(ctx, x + kHyNavigationLineViewPointerSize, self.frame.size.height);
CGContextAddLineToPoint(ctx, x + kHyNavigationLineViewPointerSize * 2.0f, bottomInset);
CGContextAddLineToPoint(ctx, self.frame.size.width - kHyNavigationLineViewPadding, 0.0f);
// Draw
CGContextStrokePath(ctx);
}
关于如何解决这个问题的任何提示?
编辑:忘了提到我首先在图层中绘制的原因:我需要为使这条线动画的属性设置动画效果很好,所以在drawRect 中绘制不起作用。
【问题讨论】:
-
你在哪里/何时告诉图层自己绘制?什么时候设置图层的框架?具体来说,我想知道是否有任何方法可以在图层较小的情况下绘制图层的内容,然后在布局期间按比例放大。一种检查方法是在图层设置期间设置
layer.needsDisplayOnBoundsChange = YES;,看看是否能解决问题。 -
请看我的编辑。
layer.needsDisplayOnBoundsChange似乎没有帮助... -
谢谢。耻辱!那么你
HyNavigationLineLayer的框架呢?这是在哪里设置的? -
在调试器中暂停并运行以下命令:
po [[UIApp keyWindow] recursiveDescription]。然后,在输出中找到您的HyNavigationLineLayer的地址。在上面运行po。 (例如:如果输出包含<HyNavigationLineLayer: 0x79f5aee0>,则运行po 0x79f5aee0。)复制两个命令的所有输出并将其粘贴到您的问题中。 -
@stefandouganhyde,这就是我的所有设置......
标签: ios objective-c calayer antialiasing