【非凡程序员】UIScrollView、UIPageControl的练习

代码功能,实现图的自动播放~

 

.h

#import <UIKit/UIKit.h>


@interface  ViewController : UIViewController<UIScrollViewDelegate>

@property(nonatomic,strong)UIImageView *imageView;

@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)UIPageControl *PageControl;

@property(nonatomic,strong)NSTimer *timer;

@end

.m


#import "ViewController.h"


@interfaceViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    _timer=[NSTimer  scheduledTimerWithTimeInterval:2.0ftarget:selfselector:@selector(autoBegin) userInfo:nilrepeats:nil];


    _scrollView=[[UIScrollViewalloc]initWithFrame:CGRectMake(32.5, 100, 300,300)];

    for (int i=0; i<8; i++) {

        _imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(i*300, 0, 300,300)];

        NSString *str=[NSStringstringWithFormat:@"%i.jpg",i+1];

        _imageView.image=[UIImageimageNamed:str];

       [_scrollViewaddSubview:_imageView];


    }

    

    _scrollView.contentSize=CGSizeMake( _scrollView.frame.size.width * 8 , 0);

    _PageControl=[[UIPageControlalloc]initWithFrame:CGRectMake(32.5, 330, 300, 100)];

    _PageControl.currentPage=0;

    _PageControl.numberOfPages=8;

    _PageControl.currentPageIndicatorTintColor=[UIColorblueColor];

    _PageControl.pageIndicatorTintColor=[UIColorblackColor];

    _scrollView.delegate=self;

    [self.viewaddSubview:_PageControl];

    [self.viewaddSubview:_scrollView];

    [self.viewbringSubviewToFront:_PageControl];

    _scrollView.pagingEnabled = YES;

    

    

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

       return_imageView;

}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    CGFloat FloatX= _scrollView.contentOffset.x;

    _PageControl.currentPage = FloatX / _scrollView.frame.size.width;

}


-(void)autoBegin

{

    [UIViewanimateWithDuration:1.0fanimations:^{

        CGFloat offsetX =  _scrollView.contentOffset.x;

        offsetX += 300;

        if( offsetX == _scrollView.contentSize.width ){

            offsetX = 0;

        }

               _scrollView.contentOffset = CGPointMake( offsetX, 0);

    }];

    _timer=[NSTimer  scheduledTimerWithTimeInterval:2.0ftarget:selfselector:@selector(autoBegin) userInfo:nilrepeats:nil];

}


你可能感兴趣的:(【非凡程序员】UIScrollView、UIPageControl的练习)