滚动视图

{

UIScrollView *scoll;

UIPageControl *page;

UIButton *nb;

NSArray *aa;

}

@end

@implementation myViewController

- (void)viewDidLoad {

[super viewDidLoad];

//    self.navigationItem.title=@"->滑动视图";

//

//自动调整状态为no

self.automaticallyAdjustsScrollViewInsets = NO;

//

//    self.navigationController.navigationBar.barTintColor=[UIColor whiteColor];

//self.navigationController.navigationBarHidden=YES;//隐藏导航栏

//创建滚动页面

self.view.backgroundColor=[UIColor whiteColor];

scoll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

scoll.delegate=self;

scoll.pagingEnabled=YES;

scoll.contentSize=CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height);

[self.view addSubview:scoll];

int x=0;

for (int i=0;i<3; i++)

{

aa=@[@"u=3443982149,1257493796&fm=21&gp=0.jpg",@"u=2635543389,3766719838&fm=21&gp=0.jpg",@"u=1152973842,2731130308&fm=21&gp=0.jpg"];

UIImageView *mag=[[UIImageView alloc]initWithImage:[UIImage imageNamed:aa[i]]];

mag.frame=CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height);

[scoll addSubview:mag];

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

}

page=[[UIPageControl alloc]initWithFrame:CGRectMake((self.view.frame.size.width-100)/2,700, 100, 50)];

page.currentPage=0;

page.numberOfPages=3;

page.currentPageIndicatorTintColor=[UIColor blackColor];

page.pageIndicatorTintColor=[UIColor redColor];

[page addTarget:self action:@selector(pa) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:page];

//添加定时器

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(dsq) userInfo:nil repeats:YES];

//创建按钮

nb=[[UIButton alloc]initWithFrame:CGRectMake(165,250, 100, 50)];

[nb setTitle:@"@@" forState:UIControlStateNormal];

[nb setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

[nb addTarget:self action:@selector(jm) forControlEvents:UIControlEventTouchUpInside];

}

-(void)pa

{

int n=(int)page.currentPage;

[scoll scrollRectToVisible:CGRectMake(n*scoll.frame.size.width, 0, scoll.frame.size.width, scoll.frame.size.height) animated:YES];

}

//定时器

-(void)dsq

{

NSInteger pa=page.currentPage;

pa++;

if (pa==aa.count)

{

pa=2;

}

scoll.contentOffset=CGPointMake(pa*self.view.frame.size.width, 0);

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;

{

NSInteger u=scoll.contentOffset.x/scoll.frame.size.width;

page.currentPage=u;

if (u==2)

{

[self.view addSubview:nb];

}

else

{

[nb removeFromSuperview];

}

}

-(void)jm

{

ViewController *i=[ViewController new];

[self.navigationController pushViewController:i animated:YES];

}

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