前言
效果图
I 视图滚动的时候控制导航条标题及公告视图的alpha
应用场景:导航条的标题放到视图中,例如下图
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