zoukankan      html  css  js  c++  java
  • 左右滑动加点击跳转

    类似于答题功能,可以实现左右滑动到下一页面,或者能过点击跳转到下一题

    直接贴代码

    - (void)viewDidLoad {

        [superviewDidLoad];

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

        self.title = @"左右滑动+Btn滑动";

        [selfcreatSubview];

    }

     

    //创建子view

    - (void)creatSubview

    {

     

        _mArray = [NSMutableArraynew];

        NSInteger heigth = self.view.frame.size.height;

        NSInteger width = self.view.frame.size.width;

        _scrollview = [[UIScrollViewalloc] initWithFrame:CGRectMake(0, 0, width, heigth)];

        _scrollview.backgroundColor = [UIColorwhiteColor];

        _scrollview.pagingEnabled = YES;

        _scrollview.scrollEnabled=YES;

        _scrollview.showsHorizontalScrollIndicator=NO;

        _scrollview.showsVerticalScrollIndicator=NO;

        _scrollview.delegate=self;

        [self.viewaddSubview:_scrollview];

        

        _array=[NSArrayarrayWithObjects:[UIColororangeColor],[UIColorpurpleColor], [UIColorwhiteColor],[UIColorredColor],[UIColorgreenColor],[UIColorgrayColor],nil];

        

        UIView * view =[[UIViewalloc] initWithFrame:CGRectMake(_pageNumber*_scrollview.frame.size.width,0,_scrollview.frame.size.width,_scrollview.frame.size.height)];

        [_scrollviewaddSubview:view];

     

        _scrollview.contentSize=CGSizeMake(width * 7, 0);

        

        _btn = [UIButtonbuttonWithType:(UIButtonTypeCustom)];

        _btn.frame = CGRectMake(width / 3, heigth / 3, width / 3, 30);

        _btn.backgroundColor = [UIColorblueColor];

        [_btnsetTitle:@"0"forState:(UIControlStateNormal)];

        [_btnaddTarget:selfaction:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];

        [self.viewaddSubview:_btn];

        

    }

     

    //button事件

    - (void)btnAction:(UIButton * )btn

    {

        if (_pageNumber != _array.count - 1) {

            _pageNumber++;

     

        }else{

        

            NSLog(@"The last one");

        }

        //动态创建view

        [selfcreatView:_pageNumber];

        

    //    NSLog(@"click");

        [btn setTitle:[NSStringstringWithFormat:@"%ld",(long)_pageNumber] forState:(UIControlStateNormal)];

        CGFloat x = _pageNumber * self.scrollview.frame.size.width;

        _scrollview.contentOffset = CGPointMake(x,0);

    }

     

    #pragma mark - scrollView的代理方法

     -(void)scrollViewDidScroll:(UIScrollView *)scrollView

     {

         

           //计算相应地页(滚动视图可以滚动的总宽度/单个滚动视图的宽度=滚动视图的页数)

             NSInteger currentPage = (int)(scrollView.contentOffset.x) / (int)(self.view.frame.size.width);

         

         _pageNumber = currentPage;//将上述的滚动视图页数重新赋给当前视图页数,进行分页

        

        [selfcreatView:_pageNumber];

       

         

         [_btnsetTitle:[NSStringstringWithFormat:@"%ld",_pageNumber] forState:(UIControlStateNormal)];

         

    //     NSLog(@"%ld",_pageNumber);

      

         

    }

     

    //动态创建view

    - (void)creatView:(NSInteger)number

    {

         BOOL isbool = [_mArraycontainsObject: [NSStringstringWithFormat:@"%ld",(long)number]];

        if (isbool == 0) {

            [_mArrayaddObject:[NSStringstringWithFormat:@"%ld",(long)number]];

            UIView * view =[[UIViewalloc] initWithFrame:CGRectMake(number*_scrollview.frame.size.width,0,_scrollview.frame.size.width,_scrollview.frame.size.height)];

            NSLog(@"creatSubv%@",_mArray);

            [_scrollviewaddSubview:view];

        }else{

        

            NSLog(@"Digital already exists");

        }

        

    }

    工作小记,欢迎共享
  • 相关阅读:
    线段树扫描线求矩形面积并
    BZOJ-1103 [POI2007]大都市meg 【DFS序+树状数组】
    安徽大学第九届程序设计竞赛决赛题解
    如何解决Vue.js里面noVNC的截图问题(2)——蛋疼的cookies验证和node.js的websocket代理
    如何解决Vue.js里面noVNC的截图问题(1)——论可以跨域的webSocket
    从技术人视角看闪电网络之作用和局限性,以及一些问题的回答
    从技术人视角看闪电网络之闪电路由
    从技术人视角看闪电网络之微支付通道
    微服务化的大坑之一:当dubbo神器碰上共用注册中心和错误的暴露接口
    Fun论设计模式之1:简单工厂模式(Factory Pattern)
  • 原文地址:https://www.cnblogs.com/fannyLi/p/6867598.html
Copyright © 2011-2022 走看看