// // HomeHeaderView.m // redstar // // Created by admin on 15/10/26. // Copyright © 2015年 ZWF. All rights reserved. // #import "HomeHeaderView.h" #define kCount 5 @interface HomeHeaderView () { UIImageView *_centerView; UIImageView *_reusableView; } @property (nonatomic,assign) BOOL isDragging; //是否正在拖动 @property (nonatomic,strong) NSTimer *timer; //设置动画 @end @implementation HomeHeaderView #pragma mark - init - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = kMineBackGroundColor; [self setup]; } return self; } #pragma mark - Private Methods - (void)setup { // 天气label self.weatherLabel.text = @"上海 ⛅️ 多云转晴 21-29℃"; // 设置代理 self.scrollView.delegate = self; CGFloat w = _scrollView.frame.size.width; CGFloat h = _scrollView.frame.size.height; _scrollView.pagingEnabled = YES; _scrollView.contentSize = CGSizeMake(w * 3, 0); _scrollView.contentOffset = CGPointMake(w, 0); _scrollView.showsHorizontalScrollIndicator = NO; _centerView = [[UIImageView alloc] init]; _centerView.image = [UIImage imageNamed:@"00.jpg"]; _centerView.frame = CGRectMake(w, 0, w, h); _centerView.tag = 0; _centerView.userInteractionEnabled = YES; [_scrollView addSubview:_centerView]; // 添加点击事件 UITapGestureRecognizer *tapCenterGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRClick:)]; [_centerView addGestureRecognizer:tapCenterGR]; _reusableView = [[UIImageView alloc] init]; _reusableView.frame = _scrollView.bounds; UITapGestureRecognizer *tapReusableGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRClick:)]; [_reusableView addGestureRecognizer:tapReusableGR]; // 设置时钟动画 定时器 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(update:) userInfo:nil repeats:YES]; // 将定时器添加到主线程 [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; } - (void)update:(NSTimer *)timer{ //定时移动 if (_isDragging == YES) { return ; } CGPoint offSet = self.scrollView.contentOffset; offSet.x +=offSet.x; [self.scrollView setContentOffset:offSet animated:YES]; if (offSet.x >= self.frame.size.width *2) { offSet.x = self.frame.size.width; } } - (void)tapGRClick:(UITapGestureRecognizer *)sender { NSLog(@"轮播图点击···"); } #pragma mark - UIScrollView Delegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat offsetX = scrollView.contentOffset.x; CGFloat w = scrollView.frame.size.width; // 1.设置 循环利用view 的位置 CGRect f = _reusableView.frame; NSInteger index = 0; if (offsetX > _centerView.frame.origin.x) { // 显示在最右边 f.origin.x = scrollView.contentSize.width - w; index = _centerView.tag + 1; if (index >= kCount) index = 0; } else { // 显示在最左边 f.origin.x = 0; index = _centerView.tag - 1; if (index < 0) index = kCount - 1; } _reusableView.frame = f; _reusableView.tag = index; NSString *icon = [NSString stringWithFormat:@"0%ld.jpg", (long)index]; _reusableView.image = [UIImage imageNamed:icon]; // 2.显示了 最左 或者 最右 的图片 if (offsetX <= 0 || offsetX >= w * 2) { // 2.1.交换 中间的 和 循环利用的 UIImageView *temp = _centerView; _centerView = _reusableView; _reusableView = temp; // 2.2.设置显示位置 _centerView.frame = _reusableView.frame; scrollView.contentOffset = CGPointMake(w, 0); [_reusableView removeFromSuperview]; } else { [_scrollView addSubview:_reusableView]; } } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { _isDragging = YES; } // 停止滚动 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { _isDragging = NO; } #pragma mark - lazy loading - (UIScrollView *)scrollView { if (!_scrollView) { _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)]; _scrollView.bounces = NO; _scrollView.pagingEnabled = YES; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.showsVerticalScrollIndicator = NO; [self addSubview:_scrollView]; } return _scrollView; } - (UILabel *)weatherLabel { if (!_weatherLabel) { _weatherLabel = [[UILabel alloc] init]; _weatherLabel.translatesAutoresizingMaskIntoConstraints = NO; _weatherLabel.textAlignment = NSTextAlignmentRight; _weatherLabel.textColor = [UIColor whiteColor]; _weatherLabel.font = [UIFont boldSystemFontOfSize:14.0]; [self insertSubview:_weatherLabel aboveSubview:self.scrollView]; NSLayoutConstraint *weatherHeight = [NSLayoutConstraint constraintWithItem:_weatherLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20]; [_weatherLabel addConstraint:weatherHeight]; NSLayoutConstraint *weatherRight = [NSLayoutConstraint constraintWithItem:_weatherLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10]; [self addConstraint:weatherRight]; NSLayoutConstraint *weatherBottom = [NSLayoutConstraint constraintWithItem:_weatherLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]; [self addConstraint:weatherBottom]; } return _weatherLabel; } @end