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

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

前言

image.png

效果图

image.png

image.png

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

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

image.png

1.1 视图的创建

  • setupnavView
- (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
- (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即可。

   vc.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

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

  UIButton *tmpView = [[UIButton alloc]init];
        UIImage *img = [[UIImage imageNamed:QCTNAVicon_left] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        tmpView.tintColor = UIColor.whiteColor;

UIImageRenderingMode

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概率抽奖算法 & 转盘算法 &轮盘边框动画

  • h
#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
//
#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


目录
相关文章
|
iOS开发
iOS MFMessageComposeViewController不显示取消按钮,导航条上白色,无取消按钮,无法返回应用...
iOS MFMessageComposeViewController不显示取消按钮,导航条上白色,无取消按钮,无法返回应用...
70 0
|
7月前
|
iOS开发
SwiftUI适配iOS16导航控制器引起的闪退
SwiftUI适配iOS16导航控制器引起的闪退
77 0
|
iOS开发
iOS 15后设置导航控制器的导航条背景色无效的问题处理
iOS 15后设置导航控制器的导航条背景色无效的问题处理
495 0
|
iOS开发
iOS 多个滚动控件嵌套Demo
iOS 多个滚动控件嵌套Demo
72 0
|
iOS开发
IOS15上纯代码布局之导航控制器的导航条为透明的问题
IOS15上纯代码布局之导航控制器的导航条为透明的问题
229 0
|
iOS开发
iOS开发-导航栏标题动画
iOS开发-导航栏标题动画
202 0
iOS开发-导航栏标题动画
|
iOS开发
iOS开发 - 渐变导航条升级版(判断滚动的方向和改变方向时的位置)
iOS开发 - 渐变导航条升级版(判断滚动的方向和改变方向时的位置)
138 0
iOS开发 - 渐变导航条升级版(判断滚动的方向和改变方向时的位置)
|
iOS开发 开发者
iOS开发 - placeholder默认灰色在同系统同型号手机上显示不一致(灰和黑)
iOS开发 - placeholder默认灰色在同系统同型号手机上显示不一致(灰和黑)
200 0
iOS开发 - placeholder默认灰色在同系统同型号手机上显示不一致(灰和黑)
|
iOS开发
iOS开发 - 滚动选择器
iOS开发 - 滚动选择器
208 0
iOS开发 - 滚动选择器
|
iOS开发
iOS开发-banner滚动图自定义
iOS开发-banner滚动图自定义
206 0
iOS开发-banner滚动图自定义