Question

My class is a CALayer subclass, and I have added a observer through NSNotificationCenter, when catching the message, it will run function like this:

- (void)setCurrentAutoValue:(NSNotification *) obj
{

     [self setNeedsDiaplay];
}

but this does not work, can anyone help?

Was it helpful?

Solution

Apparently, it is necessary to call setNeedsDisplay on the main thread. You can do this by using the performSelectorOnMainThread method. For example in your case:

[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:0 waitUntilDone:NO];

OTHER TIPS

I had the same issue updating my CALayers through another thread. If i called any of the following lines directly in the same thread it worked perfectly:

              [self setNeedsDisplay:YES];
              [self needsDisplay];
              [self.circleLayer needsDisplay];
              [self.circleLayer setNeedsDisplay];

But when calling these through another thread, there was no change. I played around a bit and ended up using

              [self display];

which was the only thing that forced the CALayers to update. Self is an NSOpenGLView which contains several layers and sublayers. Maybe this can help. Every CALayer has the same method available. Maybe you should call display.

My problem was that i could only update a CALayer with an animation, not by setting it's content (a CGPath to the property "path"). What seems to be the only solution was removing all animations of the layer first and then set the new path:

[_myLayer removeAllAnimations];
UIBezierPath *newPath = ...
_myLayer.path = newPath.CGPath;

Note to self: Or the frame of the CALayer was never set. Whenever there's a problem with a CALayer, first makes sure its frame != NSZeroRect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top