2016-09-24 28 views
30

tôi trình bày một UIViewController có chứa một UIVisualEffectView như sau:UIVisualEffectView trong iOS 10

-(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; 
    } 
} 

Như bạn thấy, tôi đang sử dụng UIModalPresentationStyleOverFullScreen để khi bộ điều khiển xem hình ảnh bị mờ xuất hiện, mờ sẽ được 'áp dụng' vào nội dung của bộ điều khiển xem đang trình bày nó; segue có kiểu chuyển đổi Cross Dissolve.

Hiệu ứng có vẻ như mong đợi. Tuy nhiên, trong iOS 9 bản trình bày mượt mà hơn trong iOS 10. Trong iOS 10 khi bộ điều khiển xem xuất hiện nó có vẻ giống như một hoạt ảnh 2 bước, trong khi iOS 9 làm mờ được áp dụng ngay lập tức.

Một bức tranh trị giá một ngàn chữ vì vậy tôi tải lên một đoạn video cho thấy hành vi kỳ lạ này:

UIVisualEffectView iOS 9 vs iOS 10

Câu hỏi của tôi là: Làm thế nào tôi có thể trình bày các điều khiển xem trong iOS 10 vì nó được trình bày trong iOS 9?

Trả lời

26

iOS 10 đã thay đổi cách hoạt động của UIVisualEffectView và đã phá vỡ nhiều trường hợp sử dụng không nghiêm chỉnh "hợp pháp" nhưng đã hoạt động trước đó. Gắn bó với tài liệu, bạn không nên mờ dần trong UIVisualEffectView, đó là những gì sẽ xảy ra khi bạn sử dụng UIModalTransitionStyleCrossDissolve. Có vẻ như giờ đây đã bị hỏng trên iOS 10, cùng với việc che giấu chế độ xem hiệu ứng hình ảnh và các chế độ xem khác.

Trong trường hợp của bạn, tôi sẽ đề xuất sửa lỗi dễ dàng và cũng sẽ tạo hiệu ứng tốt hơn trước đây và được hỗ trợ trên cả iOS 9 và 10. Tạo bản trình bày tùy chỉnh và thay vì làm mờ chế độ xem, kích hoạt thuộc tính effect từ nil đến hiệu ứng mờ. Bạn có thể mờ dần trong phần còn lại của phân cấp chế độ xem nếu cần. Điều này sẽ làm gọn gàng bán kính mờ tương tự như cách nó trông khi bạn kéo các biểu tượng màn hình chủ xuống.

+3

Tuyệt vời! Hoạt động tuyệt vời. Tôi đã sử dụng cùng một dự án tôi đã sử dụng cho video mà tôi đã đăng và tải nó lên GitHub cùng với giải pháp của bạn. Cảm ơn bạn. > https://github.com/Axort/BlurTest-iOS10 – Axort

+1

Tôi đề xuất sử dụng hoạt ảnh mùa xuân và thời lượng là 0,5. Đây là những gì người bản địa làm. –

+0

@Bạn có phiên bản nhanh không? – user2722667

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

Thử nghiệm trên cả iOS9 và 10 trình tốt cho tôi

3

Mã dưới đây mờ điều khiển xem cha mẹ khi ViewController trình bày. Thử nghiệm trên cả iOS9 và 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 
Các vấn đề liên quan