IOS轮播图以及跳转TabBar

ViewController.m

#import "AppDelegate.h"

#import "oneViewController.h"

#import "twoViewController.h"

#import "threeViewController.h"

@interface ViewController (){

UIScrollView *scroll;

NSArray *arr;

UIPageControl *pag;

UIButton *but;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//创建滚动

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

scroll.delegate=self;

[self.view addSubview:scroll];

//设置内容尺寸

scroll.contentSize=CGSizeMake(scroll.frame.size.width*3, scroll.frame.size.height);

//设置按页滚动

scroll.pagingEnabled=YES;

arr=@[[UIImage imageNamed:@"2.JPG"],[UIImage imageNamed:@"002.JPG"],[UIImage imageNamed:@"003.JPG"]];

for(int i=0;i

{

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

img.image=arr[i];

[scroll addSubview:img];

img.userInteractionEnabled=YES;

if(i==arr.count-1)

{

but=[[UIButton alloc]initWithFrame:CGRectMake(140, self.view.frame.size.height -50, 100, 30)];

[but setTitle:@"进入" forState:UIControlStateNormal];

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

[but setBackgroundImage:[UIImage imageNamed:@"2.JPG"] forState:UIControlStateNormal];

[img addSubview:but];

[but addTarget:self action:@selector(jr) forControlEvents:UIControlEventTouchUpInside];

but.layer.cornerRadius=10;

but.layer.masksToBounds=YES;

}

}

//创建页面控制器

pag=[[UIPageControl alloc]initWithFrame:CGRectMake((self.view.frame.size.width -100)/2, self.view.frame.size.height -70, 100, 30)];

pag.numberOfPages=3;

pag.currentPage=0;

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

[self.view addSubview:pag];

}

-(void)jr

{

oneViewController *one=[oneViewController new];

one.tabBarItem.title=@"1";

one.tabBarItem.image=[UIImage imageNamed:@"[email protected]"];

twoViewController *two=[twoViewController new];

two.tabBarItem.title=@"2";

two.tabBarItem.image=[UIImage imageNamed:@"[email protected]"];

threeViewController *three=[threeViewController new];

three.tabBarItem.title=@"3";

three.tabBarItem.image=[UIImage imageNamed:@"[email protected]"];

arr=[NSArray arrayWithObjects:one,two,three, nil];

//创建控件

UITabBarController *tab=[[UITabBarController alloc]init];

tab.viewControllers=arr;

AppDelegate *app=[UIApplication sharedApplication].delegate;

app.window.rootViewController=tab;

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

NSInteger index=scroll.contentOffset.x / scroll.frame.size.width;

pag.currentPage=index;

}

-(void)pag

{

int index=(int)pag.currentPage;

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

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

你可能感兴趣的:(IOS轮播图以及跳转TabBar)