#import "MainViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MainViewController ()<UIScrollViewDelegate>
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor whiteColor];
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
scrollView.backgroundColor=[UIColor yellowColor];
[self.view addSubview:scrollView];
[scrollView release];
// 重要的属性,这个属性可以让scrollView滚动起来
// contentSize设置scrollView的滚动范围
scrollView.contentSize=CGSizeMake(WIDTH * 6, HEIGHT);
// 按页来进行滚动
scrollView.pagingEnabled=YES;
UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"4.jpg"]];
imageView.frame=CGRectMake(0,0,WIDTH, HEIGHT);
[scrollView addSubview:imageView];
[imageView release];
for (NSInteger i=1; i<5; i++) {
// 拼接图片名
NSString *picName=[NSString stringWithFormat:@"%ld.jpg",i];
// 通过图片名来创建UIImageView
UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:picName]];
imageView.frame=CGRectMake(WIDTH *i,0,WIDTH, HEIGHT);
// 把imageView放到scrollView中
[scrollView addSubview:imageView];
[imageView release];
// NSMutableArray *PicArr=[NSMutableArray array];
}
UIImageView *imageView2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
imageView2.frame=CGRectMake(WIDTH *5,0,WIDTH, HEIGHT);
[scrollView addSubview:imageView2];
[imageView2 release];
// 偏移量
scrollView.contentOffset=CGPointMake(WIDTH * 2, 0);
// 关掉默认的边界回弹效果
scrollView.bounces=NO;
NSLog(@"%@",scrollView.subviews);
// 水平和垂直的滚动条会作为两个子视图添加到scrollView的子视图里,如果把滚动条效果关闭,这两个视图就不会添加到scrollView的子视图里
// 关闭滚动条
scrollView.showsHorizontalScrollIndicator=NO;
scrollView.showsVerticalScrollIndicator=NO;
// 代理
// 滚动 缩放
scrollView.delegate=self;
// 设置tag值
scrollView.tag=1;
// 创建一个计时器
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES]
}
-(void)changeImage
{
// 通过tag值找scrollView
UIScrollView *scrollView=(UIScrollView *)[self.view viewWithTag:1];
[scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x+WIDTH, 0) animated:YES];
if (scrollView.contentOffset.x==WIDTH * 6) {
scrollView.contentOffset=CGPointMake(WIDTH, 0);
}
}
#pragma mark 滚动就会触发
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x==0) {
scrollView.contentOffset=CGPointMake(WIDTH * 4, 0);
}
else if (scrollView.contentOffset.x==(WIDTH * 5))
{
scrollView.contentOffset=CGPointMake(WIDTH * 1, 0);
}
}
#pragma mark 当scrollView减速停止时会触发
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"减速时停止");
}
ScrollView
最新推荐文章于 2022-07-19 18:44:04 发布