分享

用定时器控制滚动视图UIScrollView

 久辰堡 2015-07-24

#import "SaleView.h"

#import "UIImageView+AFNetworking.h"

#define  SALEVIEWWIDTH  self.frame.size.width

#define  SALEVIEWHIGHT  self.frame.size.height

@interface SaleView()<UIScrollViewDelegate>

{

    //滚动视图

    UIScrollView * scrollView;

    //页码指示器

    UIPageControl * pageControl;

    NSString*imageStr;

    UIImageView*imageView;

    int timecount;

}

@end


@implementation SaleView

-(void)prepareScrollView;

{

    

    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,SALEVIEWWIDTH,SALEVIEWHIGHT)];

      //设置默认的第一张图片

    imageStr=self.dataArr[0][@"ImgPath"];

    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, SALEVIEWWIDTH,SALEVIEWHIGHT)];

    [imageView setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:nil];

    [scrollView addSubview:imageView];

    

    //设置滚动区域

    scrollView.contentSize = CGSizeMake(self.frame.size.width,SALEVIEWHIGHT);

    //设置分页,默认为NO

    scrollView.pagingEnabled = YES;

    //关闭回弹

    scrollView.bounces = NO;

    //关闭滚动条

    scrollView.showsHorizontalScrollIndicator = NO;

    scrollView.showsVerticalScrollIndicator=NO;

    //设置代理

    scrollView.delegate = self;

    //将滚动视图加到视图上

    [self addSubview:scrollView];

    

    //这个控件叫做面码指示器,用来提示页码

    pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(self.frame.size.width/2, 120,self.frame.size.width/2-80,30)];

    //设置总页码数,也就告诉指示器有多少页,显示多少个点

    pageControl.numberOfPages =self.dataArr.count;

    //设置当前页

    pageControl.currentPage =0;

    //设置指示器的颜色

    pageControl.pageIndicatorTintColor = [UIColor grayColor];

    pageControl.currentPageIndicatorTintColor = [UIColor redColor];

    //给页码指示器加响应事件

    [pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];

     [self addSubview:pageControl];

    

    //使用NSTimer实现定时触发滚动控件滚动的动作。

    timecount=0;

    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];

}

//滚图的动画效果

-(void)pageTurn:(UIPageControl *)aPageControl{

   long int whichPage = aPageControl.currentPage;

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0.3f];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    [scrollView setContentOffset:CGPointMake(self.frame.size.width * whichPage, 0.0f) animated:YES];

    [UIView commitAnimations];

}


//定时滚动

-(void)scrollTimer

{

    self.flag=timecount+1;

   timecount++;

    if ( timecount==self.dataArr.count) {

                timecount = 0;

    }

    imageStr=_dataArr[timecount][@"ImgPath"];

    [imageView setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:nil];

    [scrollView reloadInputViews];

    pageControl.currentPage=timecount;

}



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多