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

来源:互联网 发布:如何做淘宝客服代理 编辑:程序博客网 时间:2024/05/07 01:10

以下代码可以产生屏幕闪烁一次,类似于用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);    }