滚动视图


{

NSArray *arr;

UIPageControl *pa;

UIButton *bu2;

}

[super viewDidLoad];

// 创建数组保存照片

arr=@[@"1.JPG",@"2.JPG",@"3.JPG",@"4.JPG",@"5.JPG",];

//创建滚动视图

UIScrollView *sc=[[UIScrollView alloc]initWithFrame:self.view.frame];

//设置范围

sc.contentSize = CGSizeMake(self.view.frame.size.width*arr.count, self.view.frame.size.height);

int x=0;

//创建图片控件

for (int i=0; i

{

UIImageView *ima=[[UIImageView alloc]initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height)];

//添加图片

ima.image=[UIImage imageNamed:arr[i]];

//加入滚动视图

[sc addSubview:ima];

if (i == arr.count-1)

{

bu2=[[UIButton alloc]initWithFrame:CGRectMake(x + 150, 500, 200, 44)];

[bu2 setTitle:@"立即体验" forState:UIControlStateNormal];

bu2.backgroundColor=[UIColor redColor];

[bu2 addTarget:self action:@selector(fang) forControlEvents:UIControlEventTouchUpInside];

[sc addSubview:bu2];

}

x +=self.view.frame.size.width;

}

//设置翻整页

sc.pagingEnabled=YES;

//设置代理

sc.delegate=self;

[self.view addSubview:sc];

//创建页面管理对象

pa=[[UIPageControl alloc]initWithFrame:CGRectMake(150, 650, 120, 60)];

//设置页数

pa.numberOfPages=arr.count;

[self.view addSubview:pa];

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

AaaViewController *a=[[AaaViewController alloc]init];

AppDelegate *app = [UIApplication sharedApplication].delegate;

pa.currentPage=scrollView.contentOffset.x/self.view.frame.size.width;

}

你可能感兴趣的:(滚动视图)