#import “ViewController.h”
#define HCWidth self.view.frame.size.width
#define HCHeight self.view.frame.size.height
@interface ViewController ()
{
UIScrollView *sc; // 滚动视图
UIPageControl *pageC; // 分页控制符
NSArray *imgArr; // 图片数组
NSTimer *timer; // 定时器
int k; // 计数
}
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(qqq) userInfo:nil repeats:YES];
// 定时器开启
[timer fire];// 滚动视图
sc = [[UIScrollView alloc]initWithFrame:self.view.frame];
// 设置滚动视图的代理
sc.delegate = self;
// 滚动范围
sc.contentSize = CGSizeMake(4 * HCWidth,HCHeight);
// 分页
sc.pagingEnabled = YES;
// 弹簧
sc.bounces = NO;
// 禁用水平滚动条
sc.showsHorizontalScrollIndicator = NO;
// 将滚动视图添加
[self.view addSubview:sc];// 添加图片
for (int i = 0; i < 4; i++){imgArr = @[@"1",@"2",@"3",@"4"]; // 图片 UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(i*HCWidth, 0, HCWidth, HCHeight)]; imgV.image = [UIImage imageNamed:imgArr[i]]; // 设置用户交互属性 imgV.userInteractionEnabled = YES; // 滚动视图添加图片 [sc addSubview:imgV];
// i = 3 最后一个图片
if(i== 3){
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(150, 600, 80, 40)];
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside
];
[imgV addSubview:btn];
}
}
// 豆豆
pageC = [[UIPageControl alloc]initWithFrame:CGRectMake((HCWidth - 200)/2, 660, 200 ,40)];
// 豆豆的数量
pageC.numberOfPages = imgArr.count;
// 豆豆颜色
pageC.pageIndicatorTintColor = [UIColor blackColor];
//当前选中颜色
pageC.currentPageIndicatorTintColor = [UIColor orangeColor];
// 默认选中
pageC.currentPage = 0;
//将豆豆往滚动视图里加
[self.view addSubview:pageC];
}
// 视图已经滚动的方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
pageC.currentPage = sc.contentOffset.x/HCWidth;
NSLog(@"%lf",sc.contentOffset.x);
}
// 按钮点击方法
-(void)abc{
NSLog(@"123");
}
// 定时器 (1s -> 进入一次)
-(void)qqq{
NSLog(@"%d",k);
// set 方法 -> 设置 滚动视图的偏移量
[sc setContentOffset:CGPointMake(k *HCWidth, 0)];
k++;
if(k > 3){
// 定时器停止
// [timer invalidate];
// // 禁止滚动
// sc.scrollEnabled =NO;
k=0;
}
}
@end
#############
#import “ViewController.h”
#import “Model.h”
#import “AFNetworking.h”
@interface ViewController (){
UIImageView *img;
UIScrollView *scr;
NSTimer *tim;
UIPageControl *pag;
int k;
}
@property (nonatomic,strong)NSMutableArray *arr;
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];self.arr=[[NSMutableArray alloc]init];
AFHTTPSessionManager *manager=[[AFHTTPSessionManager alloc]init];
[manager GET:@“http://123.126.40.109:7003/asmr/videos/A1100101.shtml” parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {NSLog(@"%@",responseObject); for (NSDictionary *dic in responseObject[@"result"]) { Model *mod=[[Model alloc]init]; [mod setValuesForKeysWithDictionary:dic]; [self.arr addObject:mod]; } self->tim=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(click) userInfo:nil repeats:YES]; [self->tim fire]; self->scr=[[UIScrollView alloc]initWithFrame:self.view.frame]; self->scr.pagingEnabled=YES; self->scr.delegate=self; self->scr.contentSize=CGSizeMake(self.arr.count* self.view.frame.size.width, self.view.frame.size.height) ; for (int i=0; i<self.arr.count; i++) { UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(i*self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)]; img.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[self.arr[i]imageUrl]]]]; img.userInteractionEnabled = YES; [self->scr addSubview:img]; } [self.view addSubview:self->scr]; self->pag=[[UIPageControl alloc]initWithFrame:CGRectMake(170, 700, 100, 30)]; self->pag.numberOfPages=self.arr.count; self->pag.currentPage=0; self->pag.currentPageIndicatorTintColor=[UIColor redColor]; self->pag.pageIndicatorTintColor=[UIColor blueColor]; [self.view addSubview:self->pag];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
pag.currentPage=scr.contentOffset.x/self.view.frame.size.width;
}
-(void)click{
[scr setContentOffset:CGPointMake(k *self.view.frame.size.width, 0)];
k++;
if(k > self.arr.count){
k=0;
}
}
@end