Have part of a method execute when a variable value changes?

123 Views Asked by At

I'm translating some working swift code to Objective-C that renders a SceneView into a m4v video, and the render handling method has a return statement that only executes when a condition is met.

The swift code is as follows:

func renderScene() {    
    self.videoRenderer.delegate = self
    var options = VideoRendererOptions()
    options.sceneDuration = self.rotationDuration * 
    TimeInterval(self.totalRotations)
    options.videoSize = CGSize(width: 1280, height: 720)
    options.fps = 60

    let startTime = Date()
    self.videoRenderer.render(
    scene: arView.scene,
    withOptions: options,
    until: {
        print(self.rotations, self.totalRotations)
        return self.rotations >= self.totalRotations
    },
    andThen: {
        outputPath in

        print(
            String(format:"Finished render in time: %.2fs", startTime.timeIntervalSinceNow * -1)
        )

        PhotosUtil.saveVideo(atPath: outputPath)

        let alert = UIAlertController(title: "Done", message: "A new video has been added to the Camera Roll", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true)
        }
    )
}

This swift code is working and returns once the condition in the until is met (self.rotations >= self.totalRotations).

My Objective-C code mirrors this code, but for some reason, the section in the until does not get fired. In this case the function should return based off a boolean value in the until block.

-(void)renderScene{
    videoRenderer.delegate = self;
    struct VideoRendererOptions options;
    options.videoSize = CGSizeMake(1280, 720);
    options.fps = 60;

    NSDate *startTime = [NSDate date];
    [videoRenderer render:_sceneView.scene withOptions:options until:^BOOL{
        return !isRecording;
    } andThen:^(NSString * outputPath) {
        NSLog(@"%@", [NSString stringWithFormat:@"Finished render time in %.2fs", ([startTime timeIntervalSinceNow] * - 1)]);
        [PhotoUtil saveVideoAtPath:outputPath];
        [PSIAlertUtility showAlertTitle:@"Video Saved" message:@"Video has been added to your camera roll yo"];
    }];}

I also have a method that changes the value of isRecording and calls the rendering function based on this value. It is as follows:

-(void)recordPressed{
    isRecording = !isRecording;
    if (isRecording) {
        [self renderScene];
    }
}

Would appreciate any help and I'm sorry about the last bracket formatting, can't seem to get it right.

Thanks

0

There are 0 best solutions below