ホーム+ロックを押したときに再生される「まばたき」アニメーションをコピーする方法を探しています。
このアニメーションが何らかの形で利用可能かどうか誰か知っていますか?
iOS デバイスでは、ホーム + ロックを押して画面が白く点滅したときにスクリーンショットを撮ります。この効果のことですか?もしそうなら、これを試してください:
UIView
画面全体を覆うように、ビュー階層に白い背景色の を追加します。次に、このビューの不透明度をゼロにするアニメーションを開始します。完了したら、スーパービューからビューを削除します。
[UIView animateWithDuration: 0.5
animations: ^{
whiteView.alpha = 0.0;
}
completion: ^(BOOL finished) {
[whiteView removeFromSuperview];
}
];
試す:
[UIView animateWithDuration:1 animations:^{
self.view.backgroundColor = [UIColor blackColor];
for (UIView *view2 in self.view.subviews) {
view2.backgroundColor = [UIColor blackColor];
}
}];
[UIView animateWithDuration:1 animations:^{
self.view.backgroundColor = [UIColor whiteColor];
for (UIView *view2 in self.view.subviews) {
view2.backgroundColor = [UIColor whiteColor];
}
}];
これらの例は私にインスピレーションを与えてくれましたが、望ましい効果は得られませんでした。これは私の試みであり、本物にかなり近いように見えます。
func mimicScreenShotFlash() {
let aView = UIView(frame: self.view.frame)
aView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(aView)
UIView.animateWithDuration(1.3, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
aView.alpha = 0.0
}, completion: { (done) -> Void in
aView.removeFromSuperview()
})
}
現在のソリューションには 2 つの問題があります。
他のビューの上にビューを配置すると、(UICollectionView やその他のがらくたでの私の経験では) 自動レイアウトがトリガーされる可能性があります。自動レイアウトが悪い。Autolayout は、マシンに爆発的な作業をさせ、CPU とバッテリーを燃やす以外の理由もなく計算するだけでなく、副作用をもたらす可能性があります。したがって、自動レイアウトの理由を与えたくない場合があります。たとえば、ビューにサブビューを追加して、それ自体を適切にレイアウトしておくことを本当に気にかけている場合などです。
ビューは必ずしも画面全体をカバーするとは限らないため、画面全体をフラッシュしたい場合は、UIWindow を使用することをお勧めします。
これは私の実装であり、UIView のカテゴリです。ウィンドウをフラッシュする前にスクリーンショットを撮り、後でカメラロールに保存するメソッドを含めました。UIWindow は、追加時に独自のアニメーションを実行するように見えることに注意してください。通常、おそらく 3 分の 1 秒以上フェードインします。これを行わないように指示するより良い方法があるかもしれません。
// stupid blocks
typedef void (^QCompletion)(BOOL complete);
@interface UIView (QViewAnimation)
+ (UIImage *)screenshot; // whole screen
+ (void)flashScreen:(QCompletion)complete;
- (UIImage *)snapshot; // this view only
- (void)takeScreenshotAndFlashScreen;
@end
@implementation UIView (QViewAnimation)
+ (UIImage *)screenshot;
{
NSArray *windows = [[UIApplication sharedApplication] windows];
UIWindow *window = nil;
if (windows.count) {
window = windows[0];
return [window snapshot];
} else {
NSLog(@"Screenshot failed.");
return nil;
}
}
- (UIImage *)snapshot;
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (void)flashScreen:(QCompletion)complete;
{
UIScreen *screen = [UIScreen mainScreen];
CGRect bounds = screen.bounds;
UIWindow * flash = [[UIWindow alloc] initWithFrame:bounds];
flash.alpha = 1;
flash.backgroundColor = [UIColor whiteColor];
[UIView setAnimationsEnabled:NO];
[flash makeKeyAndVisible];
[UIView setAnimationsEnabled:YES];
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationCurveEaseOut
animations:^{
flash.alpha = 0;
}
completion: ^(BOOL finished)
{
flash.hidden = YES;
[flash autorelease];
if (complete) {
complete(YES);
}
}];
}
- (void)takeScreenshotAndFlashScreen;
{
UIImage *image = [UIView screenshot];
[UIView flashScreen:^(BOOL complete){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0),
^{
UIImageWriteToSavedPhotosAlbum(image,self,@selector(imageDidFinishSaving:withError:context:),nil);
});
}];
}
- (void)imageDidFinishSaving:(UIImage *)image
withError:(NSError *)error
context:(void *)context;
{
dispatch_async(dispatch_get_main_queue(),^{
// send an alert that the image saved
});
}
@end