gpt4 book ai didi

iphone - 使用 NSIncation 在主线程上执行选择器

转载 作者:行者123 更新时间:2023-12-03 20:19:48 25 4
gpt4 key购买 nike

我想在主线程上执行动画(因为 UIKit 对象不是线程安全的),但在某个单独的线程中准备它。我有(baAnimation - CABasicAnimation 是之前分配和初始化的):

SEL animationSelector = @selector(addAnimation:forKey:);
NSString *keyString = @"someViewAnimation";

NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[workView.layer methodSignatureForSelector:animationSelector]];
[inv setTarget:workView.layer];
[inv setSelector:animationSelector];
[inv setArgument:baAnimation atIndex:2];
[inv setArgument:keyString atIndex:3];
[inv performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:NO];

我得到:

*** +[NSCFString length]: unrecognized selector sent to class 0x1fb36a0

通话:

>     #0 0x020984e6 in objc_exception_throw
> #1 0x01f7e8fb in +[NSObject doesNotRecognizeSelector:]
> #2 0x01f15676 in ___forwarding___
> #3 0x01ef16c2 in __forwarding_prep_0___
> #4 0x01bb3c21 in -[CALayer addAnimation:forKey:]
> #5 0x01ef172d in __invoking___
> #6 0x01ef1618 in -[NSInvocation invoke]

但是[workView.layer addAnimation:baAnimation forKey:@"someViewAnimation"];工作正常。我做错了什么?

最佳答案

除了 [inv keepArguments](如 Chris Suter 提到的)之外,您还需要将参数作为指向底层内存的指针传递。引用 API:

“当参数值是对象时,将指针传递给应从中复制对象的变量(或内存):

NSArray *anArray;  
[invocation setArgument:&anArray atIndex:3];

"

关于iphone - 使用 NSIncation 在主线程上执行选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2963933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com