前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS视图滚动的时候控制导航条标题及公告视图的alpha(显示与隐藏)

iOS视图滚动的时候控制导航条标题及公告视图的alpha(显示与隐藏)

作者头像
公众号iOS逆向
发布2021-07-29 16:10:00
1.6K0
发布2021-07-29 16:10:00
举报
文章被收录于专栏:iOS逆向与安全

前言

效果图

I 视图滚动的时候控制导航条标题及公告视图的alpha

应用场景:导航条的标题放到视图中,例如下图

在这里插入图片描述

1.1 视图的创建

  • setupnavView
代码语言:javascript
复制


- (void)setupnavView
{
    navView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kStatusBarHeight)];
                               navView.userInteractionEnabled = YES;//   需要处理点击事件

    
    navView.image = [UIImage imageNamed:@"img_banner_top_ios_top"];
    
    
    //1、添加导航视图UIImageView,并设置背景图片
    
    [self.view addSubview:navView];
    
    
    
    
    
    
    
    navLab = [ControlManager text:@"" font:displayFontSize(18.0f) color:[UIColor whiteColor] alingment:1 fontWithName:@"PingFang-SC-Medium"];
    navLab.frame = CGRectMake(0, kStatusBarHeight - 44, kWidth, 44);
    navLab.backgroundColor = [UIColor clearColor];
    // 1.2 添加导航条标题视图navLab
    [navView addSubview:navLab];
    
    
    
    // 1.3 公告视图
    
        
    [self.noteViw setModel:self.viewModel.noteViweModel];
    


}


- (NoteViw *)noteViw{
    if (nil == _noteViw) {
        NoteViw *tmpView = [[NoteViw alloc]init];
        
        
        
        
        [tmpView setBackgroundColor: HWColor(255, 240, 205)];
        _noteViw = tmpView;
        
        
        
        
        [navView addSubview:_noteViw];
        
        
        
        
        __weak __typeof__(self) weakSelf = self;
        
        [_noteViw mas_makeConstraints:^(MASConstraintMaker *make) {
            

            make.left.right.equalTo(weakSelf.view);
            




            CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
            
            make.top.equalTo(weakSelf.view).offset(rectStatus.size.height);

            make.height.mas_equalTo(kAdjustRatio(k_noteViewH));//32
            


            

        }];

        
        [[_noteViw rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            
            
            
            
            NSLog(@"跳转到公告详情页");
            
            if(weakSelf.gotoNoteDetailViewControllerblock){
                weakSelf.gotoNoteDetailViewControllerblock(nil);
            }
            
        }];
        
        
        
        
        

        
    }
    
    return _noteViw;
}

1.2 滚动的时候控制导航条标题和公告视图的alpha(显示与隐藏)

  • scrollViewDidScroll
代码语言:javascript
复制
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    navLab.text = @"工作台";
    
    
   double _alpha = 1 - _tableView.contentOffset.y / CGRectGetMaxY(workLab.frame);
    
    
    if (_tableView.contentOffset.y > CGRectGetMaxY(workLab.frame)) {
        
      navLab.alpha = 1;
        
    } else {
        
        navLab.alpha = 1 - _alpha;
        
    }
    
    
    
    self.noteViw.alpha = 1 - navLab.alpha;
    
    
    

    
    
    if (_tableView.contentOffset.y <= 0) {
        
        _tableView.contentOffset = CGPointMake(0, 0);
        
        _tableView.bounces = NO;
    } else {
        
        _tableView.bounces = YES;
    }
}




II 更改UIButton的图片颜色

应用场景:比如设计给我们的是一张黑色的返回箭头图.我们某个页面需要弄成白色的话.又不想重新设计一下新的图片

解决方法:修改tintColor

如果按钮是UIButtonTypeSystem类型的,比如修改系统导航栏的barButtonItem,直接设置tintColor即可。

代码语言:javascript
复制
    vc.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

但是修改自定义按钮的tintColor,需要设置图片的渲染模式为UIImageRenderingModeAlwaysTemplate,并设置对应的tintColor

代码语言:javascript
复制
        UIButton *tmpView = [[UIButton alloc]init];
        
        UIImage *img = [[UIImage imageNamed:QCTNAVicon_left] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        
        tmpView.tintColor = UIColor.whiteColor;

UIImageRenderingMode

代码语言:javascript
复制
typedef NS_ENUM(NSInteger, UIImageRenderingMode) {
    UIImageRenderingModeAutomatic,          // Use the default rendering mode for the context where the image is used
    
    UIImageRenderingModeAlwaysOriginal,     // Always draw the original image, without treating it as a template
    UIImageRenderingModeAlwaysTemplate,     // Always draw the image as a template image, ignoring its color information
} API_AVAILABLE(ios(7.0));

III iOS跑马灯控件的封装(公告栏)

1、应用场景:公告栏和抽奖轮盘边框动画

2、CSDN文章https://blog.csdn.net/z929118967/article/details/106238484

3、相关公众号文章: iOS概率抽奖算法 & 转盘算法 &轮盘边框动画

https://mp.weixin.qq.com/s?__biz=MzI0MjU5MzU5Ng==&mid=2247485619&idx=2&sn=4197689040e29e4ca2570e425fbbd1fa&chksm=e978b2e0de0f3bf624f2e57a788ef200957b92400c879bf64369c04cf135d4d0115ccd2889f2&mpshare=1&scene=1&srcid=0517TGtEbUEiCrxiOVwX8NYK&sharer_sharetime=1621235316089&sharer_shareid=950ffd19311e26ea54be030ed69e0400&version=3.1.1.2358&platform=mac#rd

  • h
代码语言:javascript
复制

#import <UIKit/UIKit.h>

#import "QCTQCTNoteViweModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface NoteViw : UIButton
@property (nonatomic,copy) QCTQCTNoteViweModel *model;
@property (nonatomic,weak) QMUIMarqueeLabel *titleLable;

@end


  • m
代码语言:javascript
复制
//

#import "NoteViw.h"

@interface NoteViw ()
@property (nonatomic,weak) UIButton *btn;

@end

@implementation NoteViw




- (UIButton *)btn{
    if (_btn == nil
        ) {
        UIButton *tmp  = [UIButton new];
        
        [tmp setImage:[UIImage imageNamed:@"icon_daiban_dinghuo"] forState: UIControlStateNormal];
        
        _btn = tmp;
        __weak __typeof__(self) weakSelf = self;
        [self addSubview:tmp];

        [_btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakSelf);
            make.height.width.mas_equalTo(kAdjustRatio(15));
            make.right.equalTo(weakSelf).offset(kAdjustRatio(-16));
        }];
        
    }
    return _btn;
}


- (UILabel *)titleLable{
    if (nil == _titleLable) {
        QMUIMarqueeLabel *tmpView = [[QMUIMarqueeLabel alloc]init];
        _titleLable = tmpView;
        tmpView.textAlignment = NSTextAlignmentCenter;// 跑马灯文字一般都是居中显示,所以 Demo 里默认使用 center
        
//        tmpView.shouldFadeAtEdge = NO;// 关闭渐隐遮罩
//        tmpView.speed = 1.5;// 调节滚动速度
        
        tmpView.textStartAfterFade = YES;// 文字停靠在遮罩的右边



        
        [self addSubview:_titleLable];
        __weak __typeof__(self) weakSelf = self;

        [_titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.bottom.equalTo(weakSelf);
            make.centerY.equalTo(weakSelf);

            make.left.equalTo(weakSelf).offset(kAdjustRatio(10));

            make.right.equalTo(weakSelf.btn.mas_left);
        }];
    }
    return _titleLable;
}


- (void)setModel:(QCTQCTNoteViweModel *)model{
    _model = model;
    [self.btn setImage:[UIImage imageNamed:model.btnImg] forState:UIControlStateNormal];
    [self setuptitle:model.title];
}

- (void)setuptitle:(NSString*)title{
    
    //
    self.titleLable.text = title;
//    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"公告:收款码仅用于在门店消费当面付款使用,为防诈骗,请谨慎向..."];
//    [attributedString addAttribute:NSFontAttributeName value: range:NSMakeRange(0, 34)];
//    [attributedString addAttribute:NSForegroundColorAttributeName value: range:NSMakeRange(0, 34)];
    
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:_titleLable.text];//_label.text
    
    
    //    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"可用设备"];
    
    
    [attributedString addAttribute:NSFontAttributeName  value:[UIFont fontWithName:@"PingFang-SC-Medium" size:11.0f]  range:NSMakeRange(0, _titleLable.text.length)];
    //    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1.0f] range:NSMakeRange(0, _label.text.length)];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:255.0f/255.0f green:162.0f/255.0f blue:29.0f/255.0f alpha:1.0f] range:NSMakeRange(0, _titleLable.text.length)];
    
    
    _titleLable.attributedText = attributedString;

}


@end

see also

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-07-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 iOS逆向 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • I 视图滚动的时候控制导航条标题及公告视图的alpha
    • 1.1 视图的创建
      • 1.2 滚动的时候控制导航条标题和公告视图的alpha(显示与隐藏)
      • II 更改UIButton的图片颜色
      • III iOS跑马灯控件的封装(公告栏)
      • see also
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档