滚动视图计时器

@interface AdsCell()

{

NSTimer *_timer;

}

@property (nonatomic, weak) UIScrollView *backScrollView;

@end

@implementation AdsCell

+ (instancetype)cellWithTableview:(UITableView *)tableview AdsModel:(AdsModel *)model

{

static NSString *identifier = @"AdsCell";

AdsCell *cell = [tableview dequeueReusableCellWithIdentifier:identifier];

if (cell == nil)

{

cell = [[AdsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

//    设置model后再进行UI布局

cell.model = model;

[cell setupUI];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

}

return cell;

}

- (void)setupUI

{

UIScrollView *backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth, (LYScreenWidth - 50) * 0.53125 + 40)];

backScrollView.backgroundColor = [UIColor whiteColor];

backScrollView.contentSize = CGSizeMake(self.model.AdsData.count * (LYScreenWidth - 40) + 40, 170);

backScrollView.showsHorizontalScrollIndicator = NO;

backScrollView.delegate = self;

[self.contentView addSubview:backScrollView];

self.backScrollView = backScrollView;

for (int i = 0; i < self.model.AdsData.count; i++)

{

UIView *shadowView = [[UIView alloc] init];

shadowView.frame = CGRectMake(25 + (LYScreenWidth - 40) * i, 15, LYScreenWidth - 50, (LYScreenWidth - 50) * 0.53125);

shadowView.layer.shadowColor = [UIColor blackColor].CGColor;

shadowView.layer.shadowRadius = 5.0;

shadowView.layer.shadowOpacity = 0.3;

shadowView.layer.shadowOffset = CGSizeMake(-4, 4);

shadowView.userInteractionEnabled = YES;

shadowView.tag = i;

CALayer *AdsView = [[CALayer alloc] init];

AdsView.frame = CGRectMake(0, 0, LYScreenWidth - 50, (LYScreenWidth - 50) * 0.53125);

[AdsView yy_setImageWithURL:[NSURL URLWithString:self.model.AdsData[i][@"image"]] placeholder:nil options:kNilOptions progress:nil transform:^UIImage * _Nullable(UIImage * _Nonnull image, NSURL * _Nonnull url) {

image = [image yy_imageByRoundCornerRadius:5.0];

return image;

} completion:nil];

[shadowView.layer addSublayer:AdsView];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(AdsViewDidSelect:)];

[shadowView addGestureRecognizer:tap];

[self.backScrollView addSubview:shadowView];

}

//创建计时器

_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(time) userInfo:nil repeats:YES];

}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

[_timer invalidate];

}

//结束减速,滚动结束

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

NSInteger index=self.backScrollView.contentOffset.x/(LYScreenWidth - 40);

self.backScrollView.contentOffset=CGPointMake(index*(LYScreenWidth - 40), 0);

if (index==self.model.AdsData.count) {

index=0;

self.backScrollView.contentOffset = CGPointZero;

}

_timer=[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(time) userInfo:nil repeats:YES];

}

-(void)time{

self.backScrollView.contentOffset=CGPointMake(self.backScrollView.contentOffset.x+(LYScreenWidth - 40), 0);

NSInteger index=self.backScrollView.contentOffset.x/(LYScreenWidth - 40);

if (index==self.model.AdsData.count) {

index=0;

self.backScrollView.contentOffset = CGPointZero;

}

}

- (void)setModel:(AdsModel *)model

{

_model = model;

}

- (void)AdsViewDidSelect:(UITapGestureRecognizer *)sender

{

NSLog(@"adsView tapper: %ld",sender.view.tag);

if ([self.delegate respondsToSelector:@selector(adsCellTappedByTag:)])

{

[self.delegate adsCellTappedByTag:sender.view.tag];

}

}

- (void)dealloc {

[_timer invalidate];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容