ios中滚动页面
2013-08-06 09:39 甘超波 阅读(1477) 评论(0) 编辑 收藏 举报- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { int width=frame.size.width; int height=frame.size.height; scrollview=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, height)]; [self addSubview:scrollview]; for (int i=0; i<3; i++) { UIImageView *imageview=[[UIImageView alloc] initWithFrame:CGRectMake(i*width, 0, width, height)]; imageview.image=[UIImage imageNamed:[NSString stringWithFormat:@"image%zi@2x.jpg",(i+1)]]; [scrollview addSubview:imageview]; } scrollview.contentSize=CGSizeMake(3*width, height); scrollview.pagingEnabled=YES; scrollview.showsVerticalScrollIndicator=NO; scrollview.showsHorizontalScrollIndicator=NO; scrollview.delegate=self; scrollview.bounces=NO; pagecontroller=[[UIPageControl alloc] initWithFrame:CGRectMake((width-200)*0.5f, height-50-10, 200, 50)]; [self addSubview:pagecontroller]; pagecontroller.currentPage=0; pagecontroller.numberOfPages=3; currentPage=0; self.timer=[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timer:) userInfo:nil repeats:YES]; [self.timer fire]; } return self; } -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ CGFloat width=self.bounds.size.width; CGPoint point=scrollview.contentOffset; CGFloat offenx=point.x; pagecontroller.currentPage=offenx/width; } -(void)timer:(NSTimer *)time{ if (currentPage>2) { currentPage=0; } CGFloat width=self.bounds.size.width; [UIView beginAnimations:nil context:nil]; scrollview.contentOffset=CGPointMake(currentPage*width, 0); pagecontroller.currentPage=currentPage; [UIView commitAnimations]; currentPage++; }
目前我正在专注NLP,请立刻加微信/QQ号 546611623, 免费送你原创《NLP高级执行师》高清视频