Hu_噼里啪啦敲代码 2016-09-13 01:21 采纳率: 50%
浏览 925

table列表第cell里套了一个scroller幻灯片,自动轮播可以,手动滑动有问题,求解

从接口里取了5条数据在幻灯片,定义了一个time,3秒轮播,这个功能没有问题,但是手动滑动幻灯片的时候一次能滑三四张图片,明明给了320的宽度,为什么还能滑几张图片?不知道为什么,粘上代码,求解!

// 取数据
newsArray =(NSArray*) _dict;

NSArray *subViews = [self.topNewsScrollView subviews];
for (UIView *view in subViews) {
    [view removeFromSuperview];
}
float x = 0.0;
float y = 0;
float w = 320;
float h = 160;
for (int i=0; i < 5; i++) {
    UIImageView * viewImage = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, w, h)];
    NSURL *url = [NSURL URLWithString:[newsArray[i] objectForKey:@"picture"]];
    [viewImage setImage:[UIImage imageNamed:@"home_back.jpg"]];
    [viewImage setImageWithURL:url];
    [self.topNewsScrollView addSubview:viewImage];

    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
    but.frame = CGRectMake(x, y, w, h);
    but.tag = i;
    [but addTarget:self action:@selector(topBannerButAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.topNewsScrollView addSubview:but];
    UIImageView * LabImg = [[UIImageView alloc]initWithFrame:CGRectMake(x, 140, 320, 20)];
    [LabImg setImage:[UIImage imageNamed:@"toptext_bg.png"]];
    [self.topNewsScrollView addSubview:LabImg];

    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(x + 5, 140, 250, 20)];
    lab.textAlignment = NSTextAlignmentLeft;
    lab.font=[UIFont systemFontOfSize:12];
    lab.textColor = [UIColor whiteColor];
    lab.backgroundColor = [UIColor clearColor];
    NSString *topTitle = [newsArray[i] objectForKey:@"title"];
    lab.text = topTitle;
    [self.topNewsScrollView addSubview:lab];
    x += w;

}

self.topNewsScrollView.contentSize = CGSizeMake(x, y);
if(self.time != nil){
    [self.time invalidate];
    self.time = nil;
}
CGPoint offset = self.topNewsScrollView.contentOffset;
offset.x = 0;
self.topNewsScrollView.contentOffset = offset;
self.time = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(topBannerAutoScroll) userInfo:nil repeats:YES];

}

// scroller view轮播

  • (void)topBannerAutoScroll{

    CGPoint offset = self.topNewsScrollView.contentOffset;
    CGSize size = self.topNewsScrollView.contentSize;
    if (offset.x == (size.width - 320)) {

    CATransition *animation = [CATransition animation];
    [animation setDelegate:self];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromRight];
    [animation setDuration:0.5];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    offset.x = 0;
    
    self.topNewsScrollView.contentOffset = offset;
    self.TopNewsPageControl.currentPage = 0;
    [[self.topNewsScrollView layer]addAnimation:animation forKey:@"pageTurnAnimation" ];
    

    }else{
    offset.x += 320;
    [UIView animateWithDuration:0.5 animations:^{
    self.topNewsScrollView.contentOffset = offset;
    }completion:^(BOOL finish){

        int page =  ((self.topNewsScrollView.contentOffset.x - 320/2)/320)+1;
        self.TopNewsPageControl.currentPage = page;
    
    }];
    

    }
    }

  • 写回答

1条回答 默认 最新

  • Hu_噼里啪啦敲代码 2016-09-13 01:27
    关注

    在线求解,困扰了几天的问题,等大神答疑解惑

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置