简体   繁体   English

CALayer在轮换时重绘

[英]CALayer redraw on rotation

I've got this in my CALayer drawing code 我的CALayer绘图代码中有这个

- (void) drawInContext:(CGContextRef)ctx {
self.frame = self.superlayer.bounds;

CGFloat height = self.bounds.size.height;
CGFloat width = self.bounds.size.width;

CGContextSetFillColorWithColor(ctx, [UIColor grayColor].CGColor);

//Draw a stripe every other pixel
int count = 0; 
while (count < height) {   
    CGContextFillRect(ctx, CGRectMake(0, count, width, 1));
    count=count+2;
}

} }

Basically, its drawing pin stripes on top a view. 基本上,它的图钉条纹在顶部的视图上。 I want it to fill the view. 我希望它填补视图。

The view is fixed at 40 high and the width of the iPad. 视图固定在40高和iPad的宽度。

When i rotate, I tell the CALayer to redraw (which is another problem - i'd rather it just automatically scale to the new width). 当我旋转时,我告诉CALayer重绘(这是另一个问题 - 我宁愿它只是自动缩放到新的宽度)。

Even with log outputs of the width and height above, it is correct. 即使上面的宽度和高度的日志输出,它是正确的。 But when it draws in landscape - there is a ~100 pixel gap on the right... 但当它在景观中绘制时 - 右边有一个约100像素的间隙......

Any ideas, or better way of doing this? 任何想法,或更好的方式这样做?

-(void)layoutSublayersOfLayer:(CALayer *)layer{
CGFloat ht = self.frame.size.height/2 - 55/2;
arrowImage.frame = CGRectMake(self.frame.size.width-30, ht, 30.0f, 55.0f);
}

arrowImage is a CALayer

This worked perfectly for me. 这对我很有用。

You need to override layoutSublayers in the parent layer. 您需要覆盖父图层中的layoutSublayers。 Or layoutSubviews in the parent view (telling it to redraw in either of those methods). 或父视图中的layoutSubviews(告诉它在这两种方法中重绘)。

What a waste of my time.... I gave up and did it a different way. 多么浪费我的时间......我放弃了,并以不同的方式做到了。

Also.....Core Animation Programming Guide lied to me and told me CALayer had an autoResizingMask - which would probably solve this for me. 另外......核心动画编程指南骗了我,告诉我CALayer有一个autoResizingMask - 这可能会解决这个问题。 It doesn't have this property, not on the iOS version of it anyway.... MacOS version probably has it. 它没有这个属性,不管它的iOS版本.... MacOS版本可能有它。

I scrapped doing it in a CALayer and did the same thing in drawRect on a UIView and set the autoResizingMask of my UIView. 我在一个CALayer中废弃了它并在UIView上的drawRect中做了同样的事情并设置了我的UIView的autoResizingMask。

This solves it. 这解决了它。 Although somehow I feel like I should be doing it in a CALayer...? 虽然不知何故,我觉得我应该在CALayer中做这件事......?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM