美文网首页
轮播图点击跳转

轮播图点击跳转

作者: 朕i | 来源:发表于2018-09-25 21:18 被阅读0次

    #import "MyNewViewController.h"

    #import "MainViewController.h"

    @interface MyNewViewController ()<UIScrollViewDelegate>

    {

        NSTimer* timer ;

        UIScrollView* scroll ;

        intk ;

        inti ;

        UIPageControl * page ;

    }

    @end

    @implementationMyNewViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        // 新特性界面

        // 滚动视图

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

        scroll.backgroundColor = [UIColor redColor];

        // 滚动的范围

        scroll.contentSize = CGSizeMake(self.view.frame.size.width * 4 , self.view.frame.size.height);

        //  设置分页

        scroll.pagingEnabled = YES ;

        // 隐藏水平滚动条

        scroll.showsHorizontalScrollIndicator = NO ;

        // 取消弹簧效果

        scroll.bounces=NO;

        // 代理

        scroll.delegate=self;

        // 初始化图片框

        for(i=0;i<4;i++)

        {

            UIImageView  * img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d",i+1]]];

            img.frame = CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);

            if(i==3)

            {

                // 按钮

                UIButton* btn = [[UIButtonalloc]initWithFrame:CGRectMake((self.view.frame.size.width-100)/2,600,100,44)];

                // 按钮内容

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

                // 设置圆角

                btn.layer.cornerRadius=10;

                btn.layer.masksToBounds=YES;

                // 设置边框

                btn.layer.borderWidth=2;

                btn.layer.borderColor= [UIColorcyanColor].CGColor;

                [btnsetTitleColor:[UIColor greenColor] forState:UIControlStateNormal];

                // 设置事件

                [btnaddTarget:self action:@selector(tiao) forControlEvents:UIControlEventTouchUpInside];

                // 用户交互

                img.userInteractionEnabled = YES ;

                [imgaddSubview:btn];

            }

            // 图片加入滚动视图

            [scrolladdSubview:img];

        }

        // 添加到主视图

        [self.view addSubview:scroll];

        // 设置分页控制点

        page= [[UIPageControlalloc]initWithFrame:CGRectMake((self.view.frame.size.width-100) /2,650,100,30)];

        //分页点的个数

        page.numberOfPages = 4 ;

        // 设置页码的颜色

        page.pageIndicatorTintColor = [UIColor greenColor];

        // 选中点的颜色

        page.currentPageIndicatorTintColor = [UIColor yellowColor];

        // 禁用

        page.enabled=NO;

        // 加入

        [self.view addSubview:page];

        // 定时器初始化

        timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(DSQ) userInfo:nil repeats:YES];

    }

    // 时间控制器的方法

    -(void)DSQ

    {

        // k 的值 等于偏移量➗视图宽的值    page.currentPage 默认值

        k = scroll.contentOffset.x/self.view.frame.size.width ;

        //    每隔2.5秒 k加1

        k++ ;

        // 获取偏移量

        [scroll setContentOffset:CGPointMake(k * self.view.frame.size.width, 0) animated:YES];

        if(k>=3)

        {

            // 定时器关闭

            [timerinvalidate];

    //        k = scroll.contentOffset.x/self.view.frame.size.width ;

    //        [scroll setContentOffset:CGPointMake(0 * self.view.frame.size.width, 0) animated:YES];

    //        [timer fire];

        }

    }

    // 跳转方法

    -(void)tiao

    {

        // 初始化

        MainViewController * MainVc = [[MainViewController alloc]init];

        //跳转下一个界面

        [self presentViewController:MainVc animated:YES completion:nil];

    }

    // 当视图滚动后  进入此方法

    -(void)scrollViewDidScroll:(UIScrollView*)scrollView

    {

        page.currentPage = scroll.contentOffset.x/self.view.frame.size.width ;

    }

    @end

    相关文章

      网友评论

          本文标题:轮播图点击跳转

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