故事板:CAShapeLayer没有被绘制到正确的框架中

时间:2013-09-16 20:01:22

标签: objective-c cocoa-touch storyboard cashapelayer nsautolayout

我正在使用故事板,并实现了一个绘制同心圆的自定义UIVIew。我正在使用约束来保持UIView水平中心,这在我旋转设备时工作正常。但我的形状图层在旋转时无法正确调整。我尝试打印self.frame,当我启动应用程序处于横向模式时,它假设的UIView框架是纵向模式,尽管我的UIView正确旋转(通过将背景颜色保持为黑色来检查)。我在handleViewRotation中调用setNeedsDisplay,如果我旋转设备,它会将视图带到正确的模式。但是一旦进入该视图并且我离开当前屏幕并旋转设备然后返回,则UIView会被调整,但是图层仍保留在同一位置(使用先前的方向假设)。

任何想法如何解决这个问题并调整图层。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
- (void)didRotate:(NSNotification *)iNotification {
    [self.myView handleViewRotation];
}

- (CAShapeLayer *)circleForRadius:(CGFloat)iRadius withColor:(CGColorRef)iColor andDashPattern:(BOOL)isDashPattern {
CAShapeLayer *aSignalcircle = [CAShapeLayer layer];
aSignalcircle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(self.frame.size.width / 2.0, 0.0, 2.0 * iRadius, 2.0 * iRadius) cornerRadius:iRadius].CGPath;
aSignalcircle.position = CGPointMake(0.0 - iRadius, 0.0 - iRadius);
aSignalcircle.fillColor = [UIColor clearColor].CGColor;
aSignalcircle.strokeColor = iColor;
aSignalcircle.lineWidth = kPSSignalStrokeWidth;

if (self.enableShadow) {
    aSignalcircle.shadowColor = [UIColor blackColor].CGColor;
    aSignalcircle.shadowOpacity = 0.5;
    aSignalcircle.shadowOffset = CGSizeMake(0, 0.25);
    aSignalcircle.shadowRadius = 0.5;
}

if (isDashPattern) {
    aSignalcircle.lineDashPattern = @[@1, @1];
}

return aSignalcircle;

}

1 个答案:

答案 0 :(得分:0)

问题在于核心图形比UI方向通知绘制速度更快。使用下一个运行循环来绘制UI和淡化效果使它看起来更好。

我在下面添加了一段代码以使其正常工作:

- (void)viewDidAppear:(BOOL)iAnimated {
    [super viewDidAppear:iAnimated];

    double delayInSeconds = 0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self.myView handleViewRotation];
    });

    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.beaconView.alpha = 1.0;
    } completion:nil];