轮播图

作者: 偏执_cbbe | 来源:发表于2017-07-27 21:10 被阅读0次

    #import "ViewController.h"

    #import "zongViewController.h"

    @interface ViewController ()

    {

    UIScrollView * theScroll;

    NSArray * theArr;

    UIPageControl * thePage;

    }

    @end

    @implementation ViewController

    - (void)viewDidLoad {

    [super viewDidLoad];

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

    self.view.backgroundColor=[UIColor whiteColor];

    //初始化一个滚动视图

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

    //设置代理

    theScroll.delegate = self;

    //设置按页滚动

    theScroll.pagingEnabled = YES;

    //设置是否显示滚动条

    theScroll.showsHorizontalScrollIndicator = NO;

    //加载

    [self.view addSubview:theScroll];

    theArr = @[@"shiwu1.jpg",@"shiwu2.jpg",@"shiwu3.jpg"];

    CGFloat x = 0.0;

    for (int i = 0; i < theArr.count; i++) {

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

    theImageView.image = [UIImage imageNamed:theArr[i]];

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

    [theScroll addSubview:theImageView];

    if(i==theArr.count -1)

    {

    UIButton *but=[[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width -100)/2, 550, 100, 40)];

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

    [but setBackgroundColor:[UIColor colorWithRed:29/250.0 green:170/250.0 blue:230/250.0 alpha:1.0]];

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

    but.layer.cornerRadius=10;

    but.layer.masksToBounds=YES;

    theImageView.userInteractionEnabled=YES;

    [theImageView addSubview:but];

    }

    }

    theScroll.contentSize = CGSizeMake(x, self.view.frame.size.height);

    //初始化一个分页控制器

    thePage = [[UIPageControl alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-30, self.view.frame.size.height/5*4, 60, 20)];

    //设置分页控制器的个数

    thePage.numberOfPages = theArr.count;

    //设置分页控制器的颜色

    thePage.pageIndicatorTintColor = [UIColor grayColor];

    //设置分页控制器选中时的颜色

    thePage.currentPageIndicatorTintColor = [UIColor redColor];

    //加载

    [self.view addSubview:thePage];

    //----------------------------------

    }

    //实现分页控制器与滚动视图的关联

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

    thePage.currentPage = theScroll.contentOffset.x/self.view.frame.size.width;

    }

    -(void)tz

    {

    zongViewController *v1=[zongViewController new];

    [self presentViewController:v1 animated:YES completion:^{}];

    }

    相关文章

      网友评论

          本文标题:轮播图

          本文链接:https://www.haomeiwen.com/subject/cjrnkxtx.html