zoukankan      html  css  js  c++  java
  • UI 06 ScrollView 的手动循环播放 与 自己主动循环播放

    假设想要循环播放的话, scrollView的照片前要加上最后一张图片, 最后要加上第一张图片.

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    
        //继承于UIView,能够通过创建view的四步对ScrollView进行创建.
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        scrollView.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:scrollView];
        [scrollView release];
    
        //重要的属性,这个属性能够让scrollView滚动起来
        //contentSize 设置scrolloView的滚动范围:
        scrollView.contentSize = CGSizeMake(WIDTH* 12, HEIGHT );
        //按页来进行滚动
        scrollView.pagingEnabled = YES;
        // 显示图片
        UIImageView *imageview1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"10.jpg"]];
        imageview1.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
        [scrollView addSubview:imageview1];
        [imageview1 release];
        for (NSInteger i = 1 ; i < 11; i++) {
            NSString *picName = [NSString stringWithFormat:@"%ld.jpg",i];
            // 通过图片名创建UIImage
            UIImageView *imageView  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:picName]];
            imageView.frame  = CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT);
            // 把ImageView放到相应的scrollView上
            [scrollView addSubview:imageView];
            [imageView release];
        }
        UIImageView *imageViewLast = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
        [scrollView addSubview:imageViewLast];
        imageViewLast.frame = CGRectMake(WIDTH * 11,0, WIDTH, HEIGHT);
        [imageViewLast release];
    
        scrollView.delegate = self;
        scrollView.tag = 1000;
    
        scrollView.bounces = NO;
        scrollView.contentOffset = CGPointMake(WIDTH, 0);
    
        [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(change) userInfo:nil repeats:YES];
    
    }
    // 这个是与NSTimer 结合,直接自己主动循环播放.
    - (void)change{
        UIScrollView *scrollview = (UIScrollView *)[self.view viewWithTag:1000];
        [scrollview setContentOffset:CGPointMake(scrollview.contentOffset.x + WIDTH,0) animated:YES];
        if (scrollview.contentOffset.x
             == WIDTH * 11) {
            scrollview.contentOffset = CGPointMake(WIDTH, 0);
        }
    }

    若是想要滑动循环播放,须要用到协议方法, 不要忘了在上面签订协议并设定代理人.
    使用NSTimer 后,就不会再走协议中的方法啦.所以偏移量的设定,在NSTimer中再又一次做改动.

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    }
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
        // 偏移量.
        if (scrollView.contentOffset.x == 0) {
            scrollView.contentOffset = CGPointMake(WIDTH * 10,0);
        }else if(scrollView.contentOffset.x == WIDTH * 10){
            scrollView.contentOffset = CGPointMake(WIDTH, 0);
        }
    }
    
  • 相关阅读:
    Spring Security教程(一)
    java报错:The reference to entity "characterEncoding" must end with the ';' delimiter.
    SpringBoot定时任务升级篇(动态添加修改删除定时任务)
    SpringBoot几种定时任务的实现方式
    JDK中的Timer和TimerTask详解
    SpringMVC拦截器与SpringBoot自定义拦截器
    Java注解入门
    Servlet 4.0 入门
    spring邮件发送
    class path resource [spring/ApplicationContext-springmvc.xml] cannot be opened because it does not exist
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8316453.html
Copyright © 2011-2022 走看看