iOS-毛玻璃、navigationBar滑动颜色渐变
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];
}
 
    人这一辈子没法做太多的事情,
所以每一件都要做得精彩绝伦。
你的时间有限,
所以不要为别人而活。
不要被教条所限,
不要活在别人的观念里。
不要让别人的意见左右自己内心的声音。
最重要的是,
勇敢的去追随自己的心灵和直觉,
只有自己的心灵和直觉才知道你自己的真实想法,
其他一切都是次要。
工作是生活的一部分,所以工作不能全部占用于生活。
                    
                
                
            
        
浙公网安备 33010602011771号