@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];
}