Question

I am using storyboard and have implemented a custom UIVIew which draws concentric circles. I am using constraints to keep the UIView horizontally centric and this is working fine when I rotate my device. But my shape layers are not adjust properly on rotation. I tried printing the self.frame and when I launch the app being in landscape mode, the UIView frame it assumes is that of Portrait mode though my UIView is rotate properly (checked by keeping background color to black). I am calling setNeedsDisplay in handleViewRotation below which brings the view to correct mode if I rotate the device. But once it is in that view and I go out of the current screen and rotate the device and come back then UIView gets adjusted but layers remains on the same place (with previous orientation assumption).

Any idea how to fix this and adjust layers.

[[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;

}

Was it helpful?

Solution

The issue was that core graphics draws faster than than UI orientation notification. Using next run loop to draw the UI and fading effect makes it look better.

I added below piece of code to make it work:

- (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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top