封装了一个仿照抖音评论轮播效果的iOS轮播视图

2024-06-06 23:12

本文主要是介绍封装了一个仿照抖音评论轮播效果的iOS轮播视图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

效果图

请添加图片描述

原理

就是我们在一个视图里面有两个子视图,一个是currentView,
一个是willShowView,在一次动画过程中,我们改变current View的frame,同时改变willShowView的frame,同时,需要改变currentVIew 的transform.y不然的话,currentView里面的内容就没有缩放效果了,看起来就是单纯的展示不下的感觉,动画结束之后,将currentView指向willView, willView指向currentView, 同时,将刚刚消失的视图,放到底部,等待下次动画展示

#代码

//
//  RollingCell.m
//  TEXT
//
//  Created by 刘博 on 2021/3/18.
//  Copyright © 2021 刘博. All rights reserved.
//#import "XBNoticeViewCell.h"
#import "XBRollingNoticeView.h"@interface XBRollingNoticeView ()@property (nonatomic, strong) NSMutableDictionary *cellClsDict; //注册 cell 的字典,key为cell的类名,value 为identifier
@property (nonatomic, strong) NSMutableArray *reuseCells; //重用cell的实例对象数组
@property (nonatomic, strong) NSTimer *timer; //计时器
@property (nonatomic, strong) XBNoticeViewCell *currentCell; //当前展示的cell
@property (nonatomic, strong) XBNoticeViewCell *willShowCell; //即将展示的cell
@property (nonatomic, assign) BOOL isAnimating; //动画
@property (nonatomic, assign) BOOL isRefresing ; ///在刷新, 多次刷新的时候,防止上次未执行完的动画对新的一轮刷新造成干扰
///
@property (nonatomic, strong) NSMutableArray *array ;@end@implementation XBRollingNoticeView- (instancetype)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {[self setupNoticeViews];}return self;
}- (void)setupNoticeViews
{self.clipsToBounds = YES;_stayInterval = 2;_animationDuration = 0.66;_fadeTranslationY = 6;[self addGestureRecognizer:[self createTapGesture]];
}- (void)registerClass:(nonnull Class)cellClass forCellReuseIdentifier:(NSString *)identifier
{[self.cellClsDict setObject:NSStringFromClass(cellClass) forKey:identifier];
}- (__kindof XBNoticeViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier
{for (XBNoticeViewCell *cell in self.reuseCells){if ([cell.reuseIdentifier isEqualToString:identifier]) {cell.userInteractionEnabled = NO;return cell;}}Class cellCls = NSClassFromString(self.cellClsDict[identifier]);XBNoticeViewCell *cell = [[cellCls alloc] initWithReuseIdentifier:identifier];cell.userInteractionEnabled = NO;return cell;
}#pragma mark- rolling
- (void)layoutCurrentCellAndWillShowCell
{int count = (int)[self.dataSource numberOfRowsForRollingNoticeView:self];if (_currentIndex > count - 1) {_currentIndex = 0;}int willShowIndex = _currentIndex + 1;if (willShowIndex > count - 1) {willShowIndex = 0;}float w = self.frame.size.width;float h = self.frame.size.height;if (!_currentCell) {// 第一次没有currentcell// currentcell is null at first time_currentCell = [self.dataSource rollingNoticeView:self cellAtIndex:_currentIndex];_currentCell.frame = CGRectMake(0, 0, w, h);if (![self.subviews containsObject:self.currentCell]) {[self addSubview:_currentCell];}if (self.style == RollingStyleDefault) {///默认轮播滚动样式,首次展示不需要加载下一个return;}}CGFloat willY = h + self.spaceOfItem;if (self.style == RollingStyleFade) {//淡入淡出的样式willY = 4;} else if (self.style == RollingStyleScaleY) {willY = h + self.spaceOfItem;}_willShowCell = [self.dataSource rollingNoticeView:self cellAtIndex:willShowIndex];_willShowCell.frame = CGRectMake(0, willY, w, h);if (self.style == RollingStyleFade) {///首次展示currentCell的时候,will 需要隐藏_willShowCell.alpha = 0;}if (![self.subviews containsObject:_willShowCell]) {[self addSubview:_willShowCell];}self.isRefresing = YES;[self.reuseCells removeObject:_currentCell];[self.reuseCells removeObject:_willShowCell];
}- (void)reloadDataAndStartRoll
{[self stopTimer];[self layoutCurrentCellAndWillShowCell];NSInteger count = [self.dataSource numberOfRowsForRollingNoticeView:self];if (count && count < 2) {return;}__weak typeof(self) weakSelf = self;self.timer = [NSTimer timerWithTimeInterval:self.stayInterval + self.animationDuration repeats:YES block:^(NSTimer * _Nonnull timer) {[weakSelf timerHandle];}];NSRunLoop *runLoop = [NSRunLoop currentRunLoop];[runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
}- (void)stopTimer
{if (_timer) {[_timer invalidate];_timer = nil;}_isAnimating = NO;_currentIndex = 0;[_currentCell removeFromSuperview];[_willShowCell removeFromSuperview];_currentCell = nil;_willShowCell = nil;[self.reuseCells removeAllObjects];
}- (void)pause
{if (_timer) {[_timer setFireDate:[NSDate distantFuture]];}
}- (void)proceed
{if (_timer) {[_timer setFireDate:[NSDate date]];}
}- (void)timerHandle
{if (self.isAnimating) {return;}if (self.style == RollingStyleDefault) {[self defaultTimeHandler];} else if (self.style == RollingStyleFade) {[self fadeTimeHandler];} else if (self.style == RollingStyleScaleY) {[self scaleYTimeHandler];}
}- (void)defaultTimeHandler
{[self layoutCurrentCellAndWillShowCell];_currentIndex++;int count = (int)[self.dataSource numberOfRowsForRollingNoticeView:self];if (_currentIndex > count - 1) {_currentIndex = 0;}float w = self.frame.size.width;float h = self.frame.size.height;self.isAnimating = YES;[UIView animateWithDuration:_animationDuration animations:^{self.currentCell.frame = CGRectMake(0, - h - self.spaceOfItem, w, h);self.willShowCell.frame = CGRectMake(0, 0, w, h);} completion:^(BOOL finished) {// fixed bug: reload data when animate runningif (self.currentCell && self.willShowCell) {[self.reuseCells addObject:self.currentCell];[self.currentCell removeFromSuperview];self.currentCell = self.willShowCell;}self.isAnimating = NO;}];
}- (void)fadeTimeHandler
{self.isRefresing = NO;self.isAnimating = YES;float w = self.frame.size.width;float h = self.frame.size.height;int count = (int)[self.dataSource numberOfRowsForRollingNoticeView:self];int willShowIndex = self->_currentIndex + 1;if (willShowIndex > count - 1) {willShowIndex = 0;}self->_willShowCell = [self.dataSource rollingNoticeView:self cellAtIndex:willShowIndex];self->_willShowCell.frame = CGRectMake(0, self.fadeTranslationY, w, h);self->_willShowCell.alpha = 0;[self addSubview:self.willShowCell];[self.reuseCells removeObject:self.willShowCell];[self.reuseCells removeObject:self.currentCell];///动画隐藏当前的cell[UIView animateWithDuration:self.animationDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{if (self.isRefresing) {self.currentCell.alpha = 1;} else {self.currentCell.alpha = 0;}} completion:^(BOOL finished) {}];[UIView animateWithDuration:self.animationDuration - 0.1 delay:0.1 options:UIViewAnimationOptionCurveLinear animations:^{if (self.isRefresing) {self.currentCell.frame = CGRectMake(0, 0, w, h);} else {self.currentCell.frame = CGRectMake(0, - self.fadeTranslationY, w, h);self.currentCell.alpha = 0;}} completion:^(BOOL finished) {}];///动画展示下一个cell ,/*这里减0.07是需要在上面文案的动画还没有结束的时候,下面文案的动画就要开始了*/dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((self.animationDuration - 0.07) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{[self showNext];});
}- (void)showNext
{[UIView animateWithDuration:self.animationDuration animations:^{if (self.isRefresing) {self.willShowCell.alpha = 0;} else {self.willShowCell.alpha = 1;}} completion:^(BOOL finished) {}] ;float w = self.frame.size.width;float h = self.frame.size.height;[UIView animateWithDuration:self.animationDuration - 0.1 delay:0.1 options:UIViewAnimationOptionCurveLinear animations:^{if (self.isRefresing) {self.willShowCell.frame = CGRectMake(0, self.fadeTranslationY, w, h);} else {self.willShowCell.frame = CGRectMake(0, 0, w, h);}} completion:^(BOOL finished) {if (self.isRefresing) {return;}self->_currentIndex++;int count = (int)[self.dataSource numberOfRowsForRollingNoticeView:self];if (self->_currentIndex > count - 1) {self->_currentIndex = 0;}if (self.currentCell && self.willShowCell) {[self.reuseCells addObject:self.currentCell];}self.currentCell = self.willShowCell;self.isAnimating = NO;}];
}- (void)scaleYTimeHandler
{NSInteger count = [self.dataSource numberOfRowsForRollingNoticeView:self];float w = self.frame.size.width;float h = self.frame.size.height;[UIView animateWithDuration:self.animationDuration animations:^{self.currentCell.frame = CGRectMake(0, 0, w, 0);self.currentCell.transform = CGAffineTransformMakeScale(1, 0.01);self.willShowCell.frame = CGRectMake(0, 0, w, h);} completion:^(BOOL finished) {self.currentCell.frame = CGRectMake(0, w + self.spaceOfItem, w, h);self.currentCell.transform = CGAffineTransformMakeScale(1, 1);if (self.willShowCell && self.currentCell) {[self.reuseCells addObject:self.currentCell];}self.currentCell = self.willShowCell;self->_currentIndex += 1;if (self.currentIndex >= count) {self->_currentIndex = 0;}NSInteger willIndex = self.currentIndex + 1;if (willIndex >= count) {willIndex = 0;}self.willShowCell = [self.dataSource rollingNoticeView:self cellAtIndex:willIndex];self.willShowCell.frame = CGRectMake(0, w + self.spaceOfItem, w, h);[self addSubview:self.willShowCell];[self.reuseCells removeObject:self.willShowCell];}];
}#pragma mark - gesture- (void)handleCellTapAction
{int count = (int)[self.dataSource numberOfRowsForRollingNoticeView:self];if (_currentIndex > count - 1) {_currentIndex = 0;}if ([self.delegate respondsToSelector:@selector(didClickRollingNoticeView:forIndex:)]) {[self.delegate didClickRollingNoticeView:self forIndex:_currentIndex];}
}- (UITapGestureRecognizer *)createTapGesture
{return [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleCellTapAction)];
}#pragma mark- lazy
- (NSMutableDictionary *)cellClsDict
{if (!_cellClsDict) {_cellClsDict = [[NSMutableDictionary alloc]init];}return _cellClsDict;
}- (NSMutableArray *)reuseCells
{if (!_reuseCells) {_reuseCells = [[NSMutableArray alloc]init];}return _reuseCells;
}- (void)dealloc
{if (self.timer) {[self.timer invalidate];self.timer  = nil;}
}- (NSMutableArray *)array
{if (!_array) {_array = [NSMutableArray array];}return _array;
}@end

如果对您有帮助,欢迎给个star
demo

这篇关于封装了一个仿照抖音评论轮播效果的iOS轮播视图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1037498

相关文章

基于Python实现PDF动画翻页效果的阅读器

《基于Python实现PDF动画翻页效果的阅读器》在这篇博客中,我们将深入分析一个基于wxPython实现的PDF阅读器程序,该程序支持加载PDF文件并显示页面内容,同时支持页面切换动画效果,文中有详... 目录全部代码代码结构初始化 UI 界面加载 PDF 文件显示 PDF 页面页面切换动画运行效果总结主

React实现原生APP切换效果

《React实现原生APP切换效果》最近需要使用Hybrid的方式开发一个APP,交互和原生APP相似并且需要IM通信,本文给大家介绍了使用React实现原生APP切换效果,文中通过代码示例讲解的非常... 目录背景需求概览技术栈实现步骤根据 react-router-dom 文档配置好路由添加过渡动画使用

使用Python实现生命之轮Wheel of life效果

《使用Python实现生命之轮Wheeloflife效果》生命之轮Wheeloflife这一概念最初由SuccessMotivation®Institute,Inc.的创始人PaulJ.Meyer... 最近看一个生命之轮的视频,让我们珍惜时间,因为一生是有限的。使用python创建生命倒计时图表,珍惜时间

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

防近视护眼台灯什么牌子好?五款防近视效果好的护眼台灯推荐

在家里,灯具是属于离不开的家具,每个大大小小的地方都需要的照亮,所以一盏好灯是必不可少的,每个发挥着作用。而护眼台灯就起了一个保护眼睛,预防近视的作用。可以保护我们在学习,阅读的时候提供一个合适的光线环境,保护我们的眼睛。防近视护眼台灯什么牌子好?那我们怎么选择一个优秀的护眼台灯也是很重要,才能起到最大的护眼效果。下面五款防近视效果好的护眼台灯推荐: 一:六个推荐防近视效果好的护眼台灯的

数据视图(AngularJS)

<!DOCTYPE html><html ng-app="home.controller"><head><meta charset="utf-8"><title>数据视图</title><link href="page/common/css/bootstrap.min.css" rel="stylesheet"><script src="page/common/js/angular.js"></

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序

JavaSE——封装、继承和多态

1. 封装 1.1 概念      面向对象程序三大特性:封装、继承、多态 。而类和对象阶段,主要研究的就是封装特性。何为封装呢?简单来说就是套壳屏蔽细节 。     比如:对于电脑这样一个复杂的设备,提供给用户的就只是:开关机、通过键盘输入,显示器, USB 插孔等,让用户来和计算机进行交互,完成日常事务。但实际上:电脑真正工作的却是CPU 、显卡、内存等一些硬件元件。

哈希表的封装和位图

文章目录 2 封装2.1 基础框架2.2 迭代器(1)2.3 迭代器(2) 3. 位图3.1 问题引入3.2 左移和右移?3.3 位图的实现3.4 位图的题目3.5 位图的应用 2 封装 2.1 基础框架 文章 有了前面map和set封装的经验,容易写出下面的代码 // UnorderedSet.h#pragma once#include "HashTable.h"

封装MySQL操作时Where条件语句的组织

在对数据库进行封装的过程中,条件语句应该是相对难以处理的,毕竟条件语句太过于多样性。 条件语句大致分为以下几种: 1、单一条件,比如:where id = 1; 2、多个条件,相互间关系统一。比如:where id > 10 and age > 20 and score < 60; 3、多个条件,相互间关系不统一。比如:where (id > 10 OR age > 20) AND sco