iOS-毛玻璃、navigationBar滑动颜色渐变

本文介绍iOS应用中实现毛玻璃效果的方法及导航栏随滑动渐变颜色的技术细节,包括不同模糊风格的展示及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、毛玻璃实现

iOS8以上,官方提供了系统方法实现毛玻璃,代码如下:

// iOS8 UIVisualEffectView
    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"car.png"]];
    bgView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:bgView];
    UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    blurView.alpha = 0.9; // 控制模糊程度
    blurView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [bgView addSubview:blurView];
    
    UIVibrancyEffect *vibrancyView = [UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)blurView.effect];
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:vibrancyView];
    visualEffectView.translatesAutoresizingMaskIntoConstraints = NO;
    [blurView.contentView addSubview:visualEffectView];

a> UIBlurEffectStyle = UIBlurEffectStyleExtraLight;

 
Paste_Image.png

b> UIBlurEffectStyle = UIBlurEffectStyleLight;

 
Paste_Image.png

c> UIBlurEffectStyle = UIBlurEffectStyleDark;

 
Paste_Image.png
2、导航栏滑动渐变

导航栏最开始的状态是透明的状态,可以看到后面的图片。之后随着用户的滑动颜色开始加深。
代码如下:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                                  forBarMetrics:UIBarMetricsDefault];
     self.navigationController.navigationBar.shadowImage = [UIImage new];
}

在tableview或scrollview的代理方法中:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat minAlphaOffset_Y = - 64;
    CGFloat maxAlphaOffset_Y = 125;
    CGFloat offset_Y = scrollView.contentOffset.y;
    CGFloat alpha = (offset_Y - minAlphaOffset_Y) / (maxAlphaOffset_Y - minAlphaOffset_Y);
    [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:alpha];
}

但是在跳转的时候需要恢复原状,即导航栏呈现不透明状态,可以封装跳转方法:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:1];
    [self.navigationController pushViewController:viewController animated:animated];
}

 

转载于:https://www.cnblogs.com/shenlaiyaoshi/p/8807182.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值