今天项目又有滚动页了,还好记得以前自己弄过一下下。分分钟搞定到项目中了。
创建一个视图类,继承了UIView, 视图里面使用ScrollView。感觉没啥可写的,直接粘代码咯。
// 使用block回调,如果有点击广告图需求的就可以了,根据index判断是点击了那张图片
typedef void(^Block) (NSInteger index);
@interface AdvertisementView : UIView
// frame 是滚动视图的frame了, imageNames是图片名称数组, buttonBlock,方法回调
- (instancetype)initWithFrame:(CGRect)frame ImageNames:(NSArray *)imageNames andBlock:(Block)buttonBlock;
@end
实现文件也很简单了
#import "AdvertisementView.h"
@interface AdvertisementView ()<UIScrollViewDelegate>
@property (nonatomic, copy)Block block;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, strong) NSArray *imageNames;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@end
@implementation AdvertisementView
- (instancetype)initWithFrame:(CGRect)frame ImageNames:(NSArray *)imageNames andBlock:(Block)buttonBlock {
if (self = [super initWithFrame:frame]) {
self.imageNames = [self imageNamesWithImageNames:imageNames];
self.block = buttonBlock;
self.width = frame.size.width;
self.height = frame.size.height;
[self addSubview:[UIView new]];
[self scrollView];
[self pageControl];
[self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0) animated:NO];
// 自动
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeOffset:) userInfo:nil repeats:YES];
}
return self;
}
#pragma mark - Normal Mehod
- (NSArray *)imageNamesWithImageNames:(NSArray *)imageNames {
NSMutableArray *multiArr = [NSMutableArray new];
NSString *firstStr = imageNames.firstObject;
NSString *lastStr = imageNames.lastObject;
[multiArr addObject:lastStr];
[multiArr addObjectsFromArray:imageNames];
[multiArr addObject:firstStr];
return [multiArr copy];
}
- (void)changeOffset:sender{
//动画修改偏移量 Duration:动画时长
[UIView animateWithDuration:1 animations:^{
//动画终结状态时,视图的样式
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + self.scrollView.frame.size.width, 0);
} completion:^(BOOL finished) {
//动画结束后,调用
[self scrollViewDidEndDecelerating:self.scrollView];
}];
}
#pragma mark - UIScrollView Delegate
//只有是用手势滑动, 才触发
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
CGFloat width = scrollView.frame.size.width;
long int page = lroundf(scrollView.contentOffset.x / width);
// 2 1 2 1
if (page == 0) {
[scrollView setContentOffset:CGPointMake((self.imageNames.count -2)*width, 0) animated:NO];
return;
}
if (page == self.imageNames.count - 1) {
[scrollView setContentOffset:CGPointMake(width, 0) animated:NO];
return;
}
self.pageControl.currentPage = page - 1;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
long int page = lroundf(scrollView.contentOffset.x / scrollView.frame.size.width);
// 2 1 2 1
if (page == 0) {
return;
}
if (page == self.imageNames.count - 1) {
return;
}
self.pageControl.currentPage = page - 1;
}
#pragma mark - lazyMehtod
- (UIScrollView *)scrollView {
if(_scrollView == nil) {
_scrollView = [[UIScrollView alloc] init];
[self addSubview:_scrollView];
_scrollView.frame = CGRectMake(0, 0, self.width, self.height);
_scrollView.contentSize = CGSizeMake(self.width *self.imageNames.count, self.height);
_scrollView.delegate = self;
_scrollView.scrollEnabled = YES;
_scrollView.pagingEnabled = YES;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.bounces = NO;
[self.imageNames enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:obj]];
imageView.frame = CGRectMake(idx * self.width, 0, self.width, self.height);
[_scrollView addSubview:imageView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonAction:)];
[_scrollView addGestureRecognizer:tap];
}];
}
return _scrollView;
}
- (UIPageControl *)pageControl {
if(_pageControl == nil) {
_pageControl = [[UIPageControl alloc] init];
[self addSubview:_pageControl];
_pageControl.frame = CGRectMake(0, self.height - 30, self.width, 30);
_pageControl.numberOfPages = self.imageNames.count - 2;
_pageControl.pageIndicatorTintColor = [UIColor whiteColor];
_pageControl.currentPageIndicatorTintColor = [UIColor lightGrayColor];
}
return _pageControl;
}
#pragma ButtonAction
- (void)buttonAction:(UITapGestureRecognizer *)tap {
if (self.block) {
self.block(self.pageControl.currentPage);
}
}
@end
写的不完善,需要用的自己修改里面的内容吧
网友评论