美文网首页
纯代码创建无限轮播器(网络加载图片+本地图片)

纯代码创建无限轮播器(网络加载图片+本地图片)

作者: 醉叶惜秋 | 来源:发表于2016-03-25 00:39 被阅读288次

Appdelegate.m

import "AppDelegate.h"

import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    _window.rootViewController = [[ViewController alloc] init];

    [_window makeKeyAndVisible];

    return YES;
    }

ViewController.m

import "ViewController.h"

import "DCPicScrollView.h"

import "DCWebImageManager.h"

@interface ViewController ()

@end

static CGFloat h = 180;

@implementation ViewController

NSArray *titleArray = [@"午夜寂寞 谁来陪我" componentsSeparatedByString:@"."];

//显示顺序和数组顺序一致
//设置图片url数组,和滚动视图位置

DCPicScrollView  *picView = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, h * 2) WithImageUrls:UrlStringArray];

//显示顺序和数组顺序一致
//设置标题显示文本数组



picView.titleData = titleArray;

//占位图片,你可以在下载图片失败处修改占位图片

picView.placeImage = [UIImage imageNamed:@"place.png"];

//图片被点击事件,当前第几张图片被点击了,和数组顺序一致

[picView setImageViewDidTapAtIndex:^(NSInteger index) {
    printf("第%zd张图片\n",index);
}];

//default is 2.0f,如果小于0.5不自动播放
picView.AutoScrollDelay = 1.0f;

// picView.textColor = [UIColor redColor];

[self.view addSubview:picView];

//下载失败重复下载次数,默认不重复,
[[DCWebImageManager shareManager] setDownloadImageRepeatCount:1];

//图片下载失败会调用该block(如果设置了重复下载次数,则会在重复下载完后,假如还没下载成功,就会调用该block)
//error错误信息
//url下载失败的imageurl
[[DCWebImageManager shareManager] setDownLoadImageError:^(NSError *error, NSString *url) {
    NSLog(@"%@",error);
}];

}

//本地加载只要放图片名数组就行了

-(void)demo2 {

NSMutableArray *arr2 = [[NSMutableArray alloc] init];

NSMutableArray *arr3 = [[NSMutableArray alloc] init];

for (int i = 1; i < 8; i++) {
    [arr2 addObject:[NSString stringWithFormat:@"%d.jpg",i]];
    [arr3 addObject:[NSString stringWithFormat:@"我是第%d张图片",i]];
};


DCPicScrollView  *picView1 = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0,self.view.frame.size.height - h*2,self.view.frame.size.width, h) WithImageUrls:arr2];

picView1.titleData = arr3;

picView1.backgroundColor = [UIColor clearColor];
[picView1 setImageViewDidTapAtIndex:^(NSInteger index) {
    printf("你点到我了😳index:%zd\n",index);
}];

picView1.AutoScrollDelay = 2.0f;

[self.view addSubview:picView1];

}

@end

相关文章

网友评论

      本文标题:纯代码创建无限轮播器(网络加载图片+本地图片)

      本文链接:https://www.haomeiwen.com/subject/eogrlttx.html