Magical
Magical

Reputation: 263

CALayer setNeedsDisplay can not work?

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?

Upvotes: 2

Views: 2772

Answers (4)

Elise van Looij
Elise van Looij

Reputation: 4232

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.

Upvotes: 0

Peter Parker
Peter Parker

Reputation: 1

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;

Upvotes: 0

aepryus
aepryus

Reputation: 4825

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

Upvotes: 3

Bonzo
Bonzo

Reputation: 75

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.

Upvotes: 0

Related Questions