图片滚动
#import "FIrstViewController.h"
@interface FIrstViewController () {
UIScrollView *mainSV;
}
@end
@implementation FIrstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
self.navigationItem.title = @"第1个";
//上面的scrollview
UIScrollView *upSV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, 320, 150)];
upSV.contentSize = CGSizeMake(400, 150);//设置区域
for (int i = 0; i < 4; i++) {//把图片放进去
UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(i*100, 0, 100, 150)];
iv.userInteractionEnabled = YES;
iv.tag = i+10;
iv.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
[upSV addSubview:iv];
//给图片添加点击事件
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(upPicClick:)];
[iv addGestureRecognizer:tgr];
[tgr release];
}
[self.view addSubview:upSV];
[upSV release];
//下面的大sv
mainSV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 210, 320, 480-210)];
mainSV.contentSize = CGSizeMake(320*4, 480-210);//设置区域
mainSV.pagingEnabled = YES;
for (int i = 0; i < 4; i++) {//把图片放进去
UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(i*320, 0, 320, 480-210)];
iv.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
[mainSV addSubview:iv];
}
mainSV.delegate = self;//设置代理
[self.view addSubview:mainSV];
NSArray *picArr = @[[UIImage imageNamed:@"btn_prepage"],[UIImage imageNamed:@"btn_home"],[UIImage imageNamed:@"btn_nextpage"]];
for (int i = 0; i < 3; i++) {
UIButton *bu = [UIButton buttonWithType:UIButtonTypeCustom];
bu.frame = CGRectMake(10+i*(92+45), 480-55, 45, 45);
[bu setImage:[picArr objectAtIndex:i] forState:UIControlStateNormal];
bu.tag = 100+i;
[bu addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bu];
}
}
-(void)buttonClick:(UIButton *)sender
{
//当前是在第几页(从0开始)
int f = (int)mainSV.contentOffset.x/320;
if (sender.tag == 100) {
//上一个
if (f < 1) {
//如果当前是第0个就不执行方法
return;
}
f--;
} else if (sender.tag == 101) {
//首页
f = 0;
} else {
//下一个
if (f > 2) {
//如果是最后一个就不执行
return;
}
f++;
}
[mainSV setContentOffset:CGPointMake(320*f, 0) animated:YES];
//从1开始
self.navigationItem.title = [NSString stringWithFormat:@"第%d个",f+1];
}
- (void)upPicClick:(UITapGestureRecognizer *)tgr
{//小图片的点击事件
UIImageView *iv = (UIImageView *)tgr.view;
self.navigationItem.title = [NSString stringWithFormat:@"第%d个",iv.tag-9];
[mainSV setContentOffset:CGPointMake((iv.tag-10)*320, 0) animated:YES];
}
//delegate方法,scrollView停止滑动时调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//contentOffset代表scrollView的偏移量,也就是x移动了多少,y移动了多少
CGFloat f = scrollView.contentOffset.x;
self.navigationItem.title = [NSString stringWithFormat:@"第%g个",f/320+1];
}
- (void)dealloc
{
[mainSV release];
mainSV = nil;
[super dealloc];
}
posted on 2014-03-29 20:50 Sinner_Yun 阅读(206) 评论(0) 编辑 收藏 举报