话不多说,先上代码
1.调用示例
//初始化
YJScrollView*scrollView = [[YJScrollViewalloc]init];
scrollView.frame=self.view.bounds;
//滚动间隔时间
scrollView.timeInval=1;
//事件代理
scrollView.delegate=self;
//图片源
scrollView.imageArray = @[[UIImage imageNamed:@"[email protected]"],[UIImage imageNamed:@"2"],[UIImage imageNamed:@"3"]];
//显示view
[self.viewaddSubview:scrollView];
2.实现点击事件代理
@interface ViewController()
@end
#pragma mark - YJScrollViewDelegate
- (void)selectedAtIndex:(NSInteger)index
{
NSLog(@"我按了第%ld个按钮",(long)index);
}
3.可根据需要需求在YJScrollView.m中修改代码
- (void)setImageArray:(NSArray*)imageArray
{
_imageArray=imageArray;
self.content = _imageArray.count;
//设置srollview的contentsize
self.scrollView.contentSize = CGSizeMake((_content+2)*kWidth , kHeight);
//设置pageconl的number
self.page.numberOfPages = _content;
//添加contentview(耗内存,以后摒弃)
for(inti=0; i<_content+2; i++){
//摈弃的原因就是在不断alloc新的imageview。
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i* kWidth , 0, kWidth, kHeight)];
//事件传递响应
imageView.userInteractionEnabled = YES;
//内容填充方式
[imageViewsetContentMode:UIViewContentModeScaleAspectFill];
//剪切超过边距
[imageViewsetClipsToBounds:YES];
//数据源
for(UIImage*image1in_imageArray){
//判定数据源是UIImage还是url
if([image1isKindOfClass:[UIImageclass]]){
imageView.image=_imageArray[(i+2)%_content];
}
elseif([image1isKindOfClass:[NSStringclass]]){
[imageViewsd_setImageWithURL:[NSURLURLWithString:(NSString*)_imageArray[(i+2)%_content]]];
}
else
return;
}
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=self.bounds;
btn.backgroundColor = [UIColor clearColor];
btn.tag= (i+2)%_content;
[btnaddTarget:self action:@selector(touchBtn:) forControlEvents:UIControlEventTouchUpInside];
[imageViewaddSubview:btn];
[self.scrollViewaddSubview:imageView];
}
[self addSubview:self.scrollView];
[self addSubview:self.page];
//开启自动轮播
[self timerInit];
}
4.仓库地址
https://github.com/yj9294/YJSwiperView
欢迎指导。qq:497148400.