gpt4 book ai didi

iphone - 尝试将 CGrect 传递给 PerformSelector withObject

转载 作者:行者123 更新时间:2023-12-03 18:53:25 26 4
gpt4 key购买 nike

我正在尝试传递 CGRect:

SEL frameSel = NSSelectorFromString(@"setFrame:");
CGRect rect = CGRectMake(10, 10, 200, 100);
[object performSelector:frameSel withObject:rect ];

但这不能编译

我也尝试过:

SEL frameSel = NSSelectorFromString(@"setFrame:");
CGRect rect = CGRectMake(10, 10, 200, 100);
NSValue * value = [NSValue valueWithCGRect:rect];
[object performSelector:frameSel withObject:value ];

实际上,这确实可以编译,但是当我调试时,框架设置不正确:

po object
<UILabel: 0x39220f0; frame = (0 0; 200 100); text = 'reflectionLabel'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x3922240>>

但应该是frame = (10 10; 200 100)

如何解决这个问题?

提前谢谢您!

最佳答案

CGRect rect = CGRectMake(10, 10, 200, 100);
[object performSelector:frameSel withObject:rect ];

But this does not compile

正确,因为 rect 是一个结构,而不是指向对象的指针。

CGRect rect = CGRectMake(10, 10, 200, 100);
NSValue * value = [NSValue valueWithCGRect:rect];
[object performSelector:frameSel withObject:value ];

Actually, this does compile but when I debug, the frame is not setted correctly:

po object
<UILabel: 0x39220f0; frame = (0 0; 200 100); text = 'reflectionLabel'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x3922240>>

嗯,这不是垃圾,所以看起来它是有效的,尽管原点不知何故仍然为零。您可能想就此提出一个单独的问题。

无论如何,你为什么要使用performSelector:withObject:?您不会将其发送给特定的线程,也不会延迟它;为什么不简单地说object.frame = rect

关于iphone - 尝试将 CGrect 传递给 PerformSelector withObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2302415/

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