話不多說(shuō),先上代碼
1.調(diào)用示例
????//初始化
? ? YJScrollView*scrollView? = [[YJScrollViewalloc]init];
? ? scrollView.frame=self.view.bounds;
? ? //滾動(dòng)間隔時(shí)間
? ? scrollView.timeInval=1;
? ? //事件代理
? ? scrollView.delegate=self;
? ? //圖片源
? ? scrollView.imageArray = @[[UIImage imageNamed:@"1@2x.png"],[UIImage imageNamed:@"2"],[UIImage imageNamed:@"3"]];
? ? //顯示view
? ? [self.viewaddSubview:scrollView];
2.實(shí)現(xiàn)點(diǎn)擊事件代理
@interface ViewController() ?<YJScrollViewDelegate>
@end
#pragma mark - YJScrollViewDelegate
- (void)selectedAtIndex:(NSInteger)index
{
? ? NSLog(@"我按了第%ld個(gè)按鈕",(long)index);
}
3.可根據(jù)需要需求在YJScrollView.m中修改代碼
- (void)setImageArray:(NSArray*)imageArray
{
? ? _imageArray=imageArray;
? ? self.content = _imageArray.count;
? ? //設(shè)置srollview的contentsize
? ? self.scrollView.contentSize = CGSizeMake((_content+2)*kWidth , kHeight);
? ? //設(shè)置pageconl的number
? ? self.page.numberOfPages = _content;
? ? //添加contentview(耗內(nèi)存,以后摒棄)
? ? for(inti=0; i<_content+2; i++){
? ? ? ? //擯棄的原因就是在不斷alloc新的imageview税娜。
? ? ? ? UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i* kWidth , 0, kWidth, kHeight)];
? ? ? ? //事件傳遞響應(yīng)
? ? ? ? imageView.userInteractionEnabled = YES;
? ? ? ? //內(nèi)容填充方式
? ? ? ? [imageViewsetContentMode:UIViewContentModeScaleAspectFill];
? ? ? ? //剪切超過(guò)邊距
? ? ? ? [imageViewsetClipsToBounds:YES];
? ? ? ? //數(shù)據(jù)源
? ? ? ? for(UIImage*image1in_imageArray){
? ? ? ? ? ? //判定數(shù)據(jù)源是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];
? ? //開(kāi)啟自動(dòng)輪播
? ? [self timerInit];
}
4.倉(cāng)庫(kù)地址
https://github.com/yj9294/YJSwiperView
歡迎指導(dǎo)挂签。qq:497148400.