我遇到了get属性问题。当我将_videoRect传递给drawArea函数时,没有任何反应。但是,如果我改变了自我.videoRect,那就完美了。
我的代码:
@property (nonatomic, assign) BOOL isRecording;
@property (nonatomic) NSValue* videoRect;
@implementation Interactor
@synthesize videoRect = _videoRect;
- (id)init {
_isRecording = NO;
[self createVideoObserver];
return self;
}
- (void)record {
_isRecording = !_isRecording;
if (!_isRecording) {
[self stop];
[_presenter animateRecordButton:_isRecording];
} else {
[self drawArea:&(_videoRect) completion:nil];
}
}
- (void)drawArea:(NSValue* __strong *)rect completion:(void (^ __nullable)(void))completion {
if (!_isFullScreen) {
*rect = [NSValue valueWithRect:[self fullScreenRect]];
} else {
[self drawScreenRect];
}
if (completion != NULL) {
completion();
}
}
Setter and getter:
- (void)setVideoRect:(NSValue*)videoRect {
_videoRect = videoRect;
}
- (NSValue*)videoRect {
return _videoRect;
}
创建视频观察者:
- (void)createVideoObserver {
[[RACObserve(self, videoRect) skip:1] subscribeNext:^(id _) {
[self start];
[_presenter animateRecordButton:_isRecording];
}];
}
我不明白为什么观察者不起作用。如何将self.videoRect传递给drawArea函数?
答案 0 :(得分:1)
您的-drawArea:completion:
方法指向NSValue
,而self.videoRect
是NSValue
的属性。在Swift中,您可以通过引用传递这样的属性,但在Objective-C中,属性实际上只是几个方法,其中一个设置实例变量,另一个返回它。所以只是将属性传递给方法将无法正常工作。您需要做的是读取属性,将指针传递给结果值,然后使用新值修改属性:
NSValue *videoRect = self.videoRect;
[self drawArea:&videoRect completion:^{
...
self.videoRect = videoRect;
}];