ios中滚动页面

- (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++;
}
原文地址:https://www.cnblogs.com/gcb999/p/3239893.html