打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
用定时器控制滚动视图UIScrollView

#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;

}



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
UIScrollView的分页和使用
iOS开发UI篇
第七课
利用UIScrollView实现图片的放大、缩小和移动查看
滚动视图UIScrollView
实现UIScrollView循环滚动
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服