{
int k;
}
@property(nonatomic,strong)UIScrollView*src;
@property(nonatomic,strong)UIPageControl*pag;
@property(nonatomic,strong)NSTimer*time;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.barTintColor=[UIColor whiteColor];
self.view.backgroundColor=[UIColor whiteColor];
//初始化ScrollView,设定位置
self.src = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 360)];
// self.src.backgroundColor = [UIColor greenColor];
//设置显示内容的大小,这里表示可以下滑十倍原高度
self.src.contentSize = CGSizeMake(3*self.view.frame.size.width, 0);
//设置当滚动到边缘继续滚时是否像橡皮经一样弹回
self.src.bounces = YES;
//设置滚动条指示器的类型,默认是白边界上的黑色滚动条
self.src.pagingEnabled = YES;
// self.src.indicatorStyle = UIScrollViewIndicatorStyleDefault;
self.src.bouncesZoom = YES;
//设置委托
self.src.delegate = self;
NSArray*arr=@[@"WechatIMG2.jpeg",@"WechatIMG3.jpeg",@"WechatIMG4.jpeg"];
for (int i=0; i<3; i++) {
//创建对象
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(i*self.view.frame.size.width, 0, self.view.frame.size.width, 360)];
//添加图片
img.image = [UIImage imageNamed:arr[i]];
//添加到滚动视图
[self.src addSubview:img];
}
[self.view addSubview:self.src];
self.pag=[[UIPageControl alloc] initWithFrame:CGRectMake(120, 300, 150, 25)];
self.pag.currentPage=0;
self.pag.numberOfPages=3;
// 选中颜色
self.pag.currentPageIndicatorTintColor=[UIColor orangeColor];
self.pag.pageIndicatorTintColor=[UIColor whiteColor];
[self.view addSubview:self.pag];
//设置定时器
self.time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ding) userInfo:nil repeats:YES];
//开启
[self.time fire];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//设置偏移量
self.pag.currentPage = self.src.contentOffset.x/self.view.frame.size.width;
}
- (void)ding{
[self.src setContentOffset:CGPointMake(k*self.view.frame.size.width, 0)];
k++;
if (k>2) {
k=0;
}
}
滚动视图
最新推荐文章于 2022-08-28 10:02:58 发布