外贸网站服务商/网络营销策略优化
首先看效果图如上
核心代码
实践了苹果cell 重用思想,支持轮播单个view的重用
,同一个轮播组件支持展示不同的view,
///刷新
- (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)fadeTimeHandler
{self.isRefresing = NO;float w = self.frame.size.width;float h = self.frame.size.height;self.isAnimating = YES;[self.reuseCells removeObject:_currentCell];[self.reuseCells removeObject:_willShowCell];///动画隐藏当前的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);}} 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.isAnimating = NO;int willShowIndex = self->_currentIndex + 1;if (willShowIndex > count - 1) {willShowIndex = 0;}self->_currentCell = self->_willShowCell;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];}];
}
附上链接
轮播组件
使用方法
pod 'XBRollingView'
///自定义cell,继承于 XBNoticeViewCell
@interface LXBCustomRollingCell : XBNoticeViewCell@endtypedef NS_ENUM(NSUInteger, RollingStyle) {RollingStyleDefault = 0, ///默认样式,滚动轮播RollingStyleFade = 1, /// 渐隐轮播
};
- (void)viewDidLoad
{[super viewDidLoad];XBRollingNoticeView *rollView = [[XBRollingNoticeView alloc] initWithFrame:CGRectMake(10, 100, 200, 70)];[rollView registerClass:[LXBCustomRollingCell class] forCellReuseIdentifier:NSStringFromClass([LXBCustomRollingCell class])];///动画时间rollView.animationDuration = 0.3;///停留时间rollView.stayInterval = 3;rollView.style = RollingStyleFade;rollView.delegate = self;rollView.dataSource = self;[rollView reloadDataAndStartRoll];[self.view addSubview:rollView];
}#pragma mark - LXBRollingNoticeViewDelegate, LXBRollingNoticeViewDelegate
- (XBNoticeViewCell *)rollingNoticeView:(XBRollingNoticeView *)rollingView cellAtIndex:(NSUInteger)index
{LXBCustomRollingCell *cell = [rollingView dequeueReusableCellWithIdentifier:NSStringFromClass([LXBCustomRollingCell class])];[cell updateWithRow:index];return cell;
}- (NSInteger)numberOfRowsForRollingNoticeView:(XBRollingNoticeView *)rollingView
{return 4;
}