从接口里取了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; }];
}
}