iOS开发Navigation 导航栏渐变

方法有多种,无外乎监听scrollView变化,通过代理方法实现。
下面是比较简单直观的方法:

-(void)viewWillAppear:(BOOL)animated{
    [superviewWillAppear:animated];
     if (self.navigationController) {
        [self.navigationControllersetNavigationBarHidden:YES animated:animated];
    }
}
-(void)viewDidDisappear:(BOOL)animated{    
    [superviewDidDisappear:animated];  
    if (self.navigationController) {
        [self.navigationControllersetNavigationBarHidden:NOanimated:NO];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y < 0) {
        [selfshowView:_naviViewhidden:YES];
        [selfsetStatusBarStyle:UIStatusBarStyleDefault];   
    }else if(scrollView.contentOffset.y > 50) {
        [selfshowView:_naviViewhidden:NO];
        CGFloat alpha = MIN(1, 1 - ((50 + 64 - scrollView.contentOffset.y) / 64));
        [_naviViewsetBackgroundColor:[[UIColorwhiteColor] colorWithAlphaComponent:alpha]];
    }else{
        [selfshowView:_naviViewhidden:NO];
        [_naviViewsetBackgroundColor:[[UIColorwhiteColor] colorWithAlphaComponent:0.0f]];
    }
}

-(void)showView:(UIView *)view hidden:(BOOL)hidden{      
        CATransition *animation = [CATransitionanimation];        
        animation.type = kCATransitionFade;    
        animation.duration = 0.4;    
        [view.layeraddAnimation:animation forKey:nil];    
        view.hidden = hidden;
}
//根据数组随机生成一个长度为3内容不重复的数组
-(NSArray*)createRandomArray:(NSMutableArray*)mutableArray{

    if (mutableArray.count < 4) {
        return mutableArray;
    }else{
        NSMutableArray *startArray = [NSMutableArray array];
        NSMutableArray *resultArray=[[NSMutableArray alloc] initWithCapacity:0];
        for (NSInteger i = 0; i < mutableArray.count; i++) {
            [startArray addObject:[NSString stringWithFormat:@"%ld",i]];
        }
        for (int i = 0; i<3; i++) {
            int t = arc4random()%startArray.count;
            resultArray[i] = startArray[t];
            startArray[t] = [startArray lastObject];
            [startArray removeLastObject];
        }
        NSInteger i = [resultArray[0] integerValue];
        NSInteger j = [resultArray[1] integerValue];
        NSInteger k = [resultArray[2] integerValue];

        return @[mutableArray[i], mutableArray[j], mutableArray[k]];
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值