ぼかしオーバーレイビューを作成する 質問する

ぼかしオーバーレイビューを作成する 質問する

新しい iOS のミュージック アプリでは、ぼやけたビューの背後にアルバム カバーが表示されます。

どうすればそのようなことを実現できるのでしょうか? ドキュメントを読みましたが、何も見つかりませんでした。

ベストアンサー1

この効果を実現するには、を使用できますUIVisualEffectView。これは、パフォーマンスと優れたバッテリー寿命のために微調整されており、実装も簡単なネイティブ API です。

迅速:

//only apply the blur if the user hasn't disabled transparency effects
if !UIAccessibility.isReduceTransparencyEnabled {
    view.backgroundColor = .clear

    let blurEffect = UIBlurEffect(style: .dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    //always fill the view
    blurEffectView.frame = self.view.bounds
    blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    view.addSubview(blurEffectView) //if you have more UIViews, use an insertSubview API to place it where needed
} else {
    view.backgroundColor = .black
}

目的:

//only apply the blur if the user hasn't disabled transparency effects
if (!UIAccessibilityIsReduceTransparencyEnabled()) {
    self.view.backgroundColor = [UIColor clearColor];

    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    //always fill the view
    blurEffectView.frame = self.view.bounds;
    blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    [self.view addSubview:blurEffectView]; //if you have more UIViews, use an insertSubview API to place it where needed
} else {
    self.view.backgroundColor = [UIColor blackColor];
}

このビュー コントローラーをモーダルで表示して基になるコンテンツをぼかす場合は、モーダル表示スタイルを [現在のコンテキストのオーバー] に設定し、背景色をクリアに設定して、このビュー コントローラーが上に表示された後も基になるビュー コントローラーが表示されるようにする必要があります。

おすすめ記事