Cocoa/iOS 屏幕闪烁效果 (Screen Flash)

司英飙
2023-12-01

以下代码可以产生屏幕闪烁一次,类似于用console时的visual bell的效果,或者是在iOS上截屏时产生的屏幕闪烁效果。由于用到了Core Graphic的东西,项目中需要调用QuartzCore.framework.

//==================================================================
//
// Flash the screen
// - SHAGRU.COM -
//==================================================================
- (void)playFlash
{
    [self flashScreenUsingFlashColor:[NSColor whiteColor] inDuration:0.01 outDuration:0.5];
}

-(void)flashScreenUsingFlashColor:(NSColor *)flashColor
                       inDuration:(NSTimeInterval)inDuration
                      outDuration:(NSTimeInterval)outDuration{
    
    CGDisplayFadeReservationToken fadeToken;
    NSColor *colorToUse = [flashColor colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
    
    CGError error = CGAcquireDisplayFadeReservation (inDuration + outDuration, &fadeToken);
    if (error != kCGErrorSuccess){
        NSLog(@"Error aquiring fade reservation. Will do nothing.");
        return;
    }
    
    CGDisplayFade (fadeToken, inDuration, kCGDisplayBlendNormal, 0.5, colorToUse.redComponent, colorToUse.greenComponent, colorToUse.blueComponent, true);
    CGDisplayFade (fadeToken, outDuration, 0.5, kCGDisplayBlendNormal,colorToUse.redComponent, colorToUse.greenComponent, colorToUse.blueComponent, false);
    
}


 类似资料: