2012-07-31 82 views
6

我想在拍攝相機的瞬間閃爍(然後淡出)屏幕,以向用戶指示已拍攝照片(除了聽覺線索外)。相機捕捉時刻的閃屏爲白色?

這樣的動畫放在哪裏?另外,它將如何實施,以便我可以控制淡出的持續時間?

注意:我爲我的特定照相機選取器創建了自定義覆蓋圖。

任何表明照片已被拍攝的東西都是我正在尋找的東西。

回答

9

我不確定你會在哪裏放置動畫,因爲我不知道你是如何捕捉圖片(也許你可以發佈代碼),但這裏是動畫的代碼閃白色屏幕:

//Header (.h) file 
@property (nonatomic, strong) UIView *whiteScreen; 

//Implementation (.m) file 
@synthesize whiteScreen; 

- (void)viewDidLoad { 
    self.whiteScreen = [[UIView alloc] initWithFrame:self.view.frame]; 
    self.whiteScreen.layer.opacity = 0.0f; 
    self.whiteScreen.layer.backgroundColor = [[UIColor whiteColor] CGColor]; 
    [self.view addSubview:self.whiteScreen]; 
} 

-(void)flashScreen { 
    CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"]; 
    NSArray *animationValues = @[ @0.8f, @0.0f ]; 
    NSArray *animationTimes = @[ @0.3f, @1.0f ]; 
    id timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
    NSArray *animationTimingFunctions = @[ timingFunction, timingFunction ]; 
    [opacityAnimation setValues:animationValues]; 
    [opacityAnimation setKeyTimes:animationTimes]; 
    [opacityAnimation setTimingFunctions:animationTimingFunctions]; 
    opacityAnimation.fillMode = kCAFillModeForwards; 
    opacityAnimation.removedOnCompletion = YES; 
    opacityAnimation.duration = 0.4; 

    [self.whiteScreen.layer addAnimation:opacityAnimation forKey:@"animation"]; 
} 

您還問過如何控制淡出持續時間。您可以通過調整animationTimes陣列中的值來完成此操作。如果你不熟悉CAKeyframeAnimations是如何工作的,那麼這裏快速簡要。動畫的總時長由opacityAnimation.duration = 0.4控制。這使得動畫長0.4秒。現在到animationTimes做什麼。數組中的每個值都是介於0.0和1.0之間的數字,並對應於'animationValues'數組中的一個元素。 times數組中的值將相應關鍵幀值的持續時間定義爲動畫總持續時間的一小部分。

例如,在上面的動畫中,times數組包含值0.3和1.0,它們對應於值0.8和0.0。總的持續時間是0.4,這樣就意味着最初具有其不透明度爲0.0的whiteScreen視圖,取

0.4 * 0.3 = 0.12 seconds. 

到不透明度提高到0.8。第二個值0.0使圖層再次變爲透明。這佔用了剩餘的時間(0.4 - 0.12 = 0.28秒)。