2 sinat 29814399 sinat_29814399 于 2016.09.13 09:21 提问

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个回答

sinat_29814399
sinat_29814399   2016.09.13 09:27

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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!