How to call setNeedsDisplayInRect using performSelectorOnMainThread?

How to call setNeedsDisplayInRect using performSelectorOnMainThread? The problem is that the problem. I do not know how to pass rect to performSelectorOnMainThread. This method requests an NSObject, but CGRect is not an NSObject, it's just a * structure.

//[self setNeedsDisplayInRect:rect];
[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:0 waitUntilDone:YES];
}

-(void)drawRect:(CGRect)rect {

    /// drawing...

}

I need to call the setNeedsDisplayInRect method in MainThread from a non-main topic. Does anyone know how to do this ?????????? Thanks in advance.

Really thanks.

+3
source share
1 answer

If you are using iOS 4.0 or later, you can use the following

dispatch_async(dispatch_get_main_queue(), ^{
    [self setNeedsDisplayInRect:theRect];
});

On iOS 3.2 and earlier, you can configure NSInvocation and run this in the main thread:

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(setNeedsDisplayInRect:)]];
[invocation setTarget:self];
[invocation setSelector:@selector(setNeedsDisplayInRect:)];
// assuming theRect is my rect
[invocation setArgument:&theRect atIndex:2];
[invocation retainArguments]; // retains the target while it waiting on the main thread
[invocation performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:YES];

, waitUntilDone NO, , .

+4

Source: https://habr.com/ru/post/1775747/


All Articles