轮播图片——APP顶上头条

来源:互联网 发布:陕西师范网络教育 编辑:程序博客网 时间:2024/04/28 20:45


#import "ViewController.h"


#define imageCount 5


@interface ViewController ()<UIScrollViewDelegate>


@property (nonatomic,strong)UIScrollView *scrollView;


@property (nonatomic,strong)UIPageControl *pageControl;


@property (nonatomic,strong)NSTimer *timer;


@end


@implementation ViewController


-(UIScrollView *)scrollView

{

   if (_scrollView ==nil) {

        _scrollView = [[UIScrollViewalloc]initWithFrame:CGRectMake(10,20, 300, 130)];

        

        _scrollView.backgroundColor = [UIColorredColor];

        

        [self.viewaddSubview:_scrollView];

        

        

       // 要分页

       _scrollView.pagingEnabled =YES;

        

           // contentSize _scrollView设置可以滚动的局域

        _scrollView.contentSize =CGSizeMake(imageCount *_scrollView.bounds.size.width,0);

        

       // 设置代理

       _scrollView.delegate =self;

    }

    return_scrollView;

}



-(UIPageControl *)pageControl

{

   if (_pageControl ==nil) {

        

        //分页控件,本质上和scrollView没有任何关系,是两个独立的控件

       _pageControl = [[UIPageControlalloc] init];

        

        // 总页数

       _pageControl.numberOfPages =imageCount;

        

       // 控件尺寸

        

        //CGSize size = [_pageControl sizeForNumberOfPages:imageCount];

        

      //  _pageControl.bounds = CGRectMake(0, 0, size.width, size.height);

        

       _pageControl.center =CGPointMake(self.view.center.x,130);

        

        

       // 设置颜色

        _pageControl.pageIndicatorTintColor = [UIColorredColor];

        

        _pageControl.currentPageIndicatorTintColor = [UIColorblueColor];

        

        [self.viewaddSubview:_pageControl];

        

        // 添加监听方法

        /** OC中,绝大多数"控件",都可以监听UIControlEventValueChanged事件,button除外" */

       [_pageControladdTarget:selfaction:@selector(pagechanged:)forControlEvents:UIControlEventValueChanged];

        

        

        

        

    }

    return_pageControl;

}


// 分页控件的监听方法

-(void)pagechanged:(UIPageControl *)page

{

    

    // 根据页数,调整滚动视图中的图片位置 contentOffset

    CGFloat x = page.currentPage *self.scrollView.bounds.size.width;

    [self.scrollViewsetContentOffset:CGPointMake(x,0) animated:YES];

  


}


// 视图加载完成调用,通常用来设置数据

- (void)viewDidLoad {

    [superviewDidLoad];

    

    [selfscrollView];

    // 设置图片

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

       NSString *imageName = [NSStringstringWithFormat:@"img_%02d",i +1];

        

       UIImage *imge = [UIImageimageNamed:imageName];

        

      

       UIImageView *imageView = [[UIImageViewalloc] initWithFrame:self.scrollView.bounds];

        

        imageView.image = imge;

        

        [self.scrollViewaddSubview:imageView];

        

    }

    // 计算imageView的位置

    [self.scrollView.subviewsenumerateObjectsUsingBlock:^(UIImageView *imageView,NSUInteger idx, BOOL *stop) {

        

        CGRect frame = imageView.frame;

        

        

        frame.origin.x = idx * frame.size.width;

        imageView.frame = frame;

       NSLog(@"%@",self.scrollView.subviews);

        

        

    }];


    // 分页初始页数为0

    self.pageControl.currentPage =0;

    

    // 启动时钟

    [selfstartTime];

}


-(void)startTime

{

    self.timer = [NSTimertimerWithTimeInterval:2.5target:selfselector:@selector(updateTimer)userInfo:nilrepeats:YES];

    // 添加到运行循环

    [[NSRunLoopcurrentRunLoop]addTimer:self.timerforMode:NSRunLoopCommonModes];

    

}


-(void )updateTimer

{

    

    // 页号发生变化

    // (当前的页数 + 1) %总页数

   int page = (self.pageControl.currentPage +1) % imageCount;

   self.pageControl.currentPage = page;

    //调用监听方法,让滚动视图滚动

    [selfpagechanged:self.pageControl];

    

}

#pragma mark - ScrollView的代理方法

// 滚动视图停下来,修改页面控件的小点(页数)

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));

    

   int page = scrollView.contentOffset.x /scrollView.bounds.size.width;

   self.pageControl.currentPage = page;

    

}


-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    [self.timerinvalidate];

}


-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

{

    [selfstartTime];

}

@end


0 0
原创粉丝点击