簡體   English   中英

iOS 10中的UIVisualEffectView

[英]UIVisualEffectView in iOS 10

我提出了一個包含UIVisualEffectView的UIViewController,如下所示:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self performSegueWithIdentifier:@"segueBlur" sender:nil];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"segueBlur"]) {
        ((UIViewController *)segue.destinationViewController).providesPresentationContextTransitionStyle = YES;
        ((UIViewController *)segue.destinationViewController).definesPresentationContext = YES;
        ((UIViewController *)segue.destinationViewController).modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
}

正如您所看到的,我正在使用UIModalPresentationStyleOverFullScreen,以便在出現模糊的視圖控制器時,模糊將“應用”到呈現它的視圖控制器的內容; segue具有Cross Dissolve過渡風格。

效果看起來像預期的那樣。 但是,在iOS 9中,演示比iOS 10更流暢。在iOS 10中,當視圖控制器出現時,它看起來像是兩步動畫,而在iOS 9中,模糊立即應用。

一張圖片勝過千言萬語所以我上傳了一段顯示這種奇怪行為的視頻:

UIVisualEffectView iOS 9 vs iOS 10

我的問題是:如何在iOS 10中呈現iOS 10中的視圖控制器?

iOS 10已經改變了UIVisualEffectView工作方式,並且已經打破了許多用例,這些用例並非嚴格意義上的“合法”,而是之前的工作。 堅持使用文檔,你不應該淡化UIVisualEffectView ,這是你使用UIModalTransitionStyleCrossDissolve時會發生的事情。 它現在似乎在iOS 10上被破壞,同時屏蔽了視覺效果視圖等等。

在你的情況下,我建議一個簡單的修復,它也將創建一個比以前更好的效果,並在iOS 9和10都支持。創建一個自定義的演示文稿,而不是淡入視圖,將effect屬性設置為從nil動畫到模糊效果。 如果需要,您可以淡入視圖層次結構的其余部分。 這樣可以巧妙地為模糊半徑設置動畫,類似於向下拉主屏幕圖標時的模糊半徑。

    UIView.animate(withDuration: 0.5) {
        self.effectView.effect = UIBlurEffect(style: .light)
    }

在iOS9和10上都經過測試。對我來說很好

當ViewController出現時,下面的代碼會模糊父視圖控制器。 在iOS9和10上都進行了測試。

@interface ViewController () <UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning>

@property (nonatomic) UIVisualEffectView *blurView;

@end


@implementation ViewController

- (instancetype)init
{
    self = [super init];
    if (!self)
        return nil;

    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    self.transitioningDelegate = self;

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor clearColor];
    self.blurView = [UIVisualEffectView new];
    [self.view addSubview:self.blurView];
    self.blurView.frame = self.view.bounds;
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                       presentingController:(UIViewController *)presenting
                                                                   sourceController:(UIViewController *)source
{
    return self;
}

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.3;
}

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIView *container = [transitionContext containerView];

    [container addSubview:self.view];

    self.blurView.effect = nil;

    [UIView animateWithDuration:0.3 animations:^{
        self.blurView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:finished];
    }];
}

@end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM