UIScrollView的自动滑动,使用NSTimer定时器实现

@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)UIPageControl *pageControl;

@property(nonatomic,strong)NSTimer *timer;

@property(nonatomic,assign)int timeIndex;


- (void)viewDidLoad {

    [super viewDidLoad];

    [

    _timeIndex=0;

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

  [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSDefaultRunLoopMode];


    _scrollView = [UIScrollView new];

    _scrollView.showsVerticalScrollIndicator = NO;

    _scrollView.delegate = self;

    _scrollView.pagingEnabled = YES;

    _scrollView.frame=CGRectMake(0, 64, self.view.bounds.size.width, 136);

    self.automaticallyAdjustsScrollViewInsets=NO;

    _scrollView.showsHorizontalScrollIndicator = NO;

    CGFloat imageW = self.scrollView.frame.size.width;

    CGFloat imageH = self.scrollView.frame.size.height;

    CGFloat imageInitialY = 0;

    CGFloat imageInitialX = 0;

    NSArray *arr=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4", nil];

/*添加按钮

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

        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];

        [button setBackgroundImage:[UIImage imageNamed:[arr objectAtIndex:i]] forState:UIControlStateNormal];

        button.tag=i;

        CGFloat imageX = i*imageW + imageInitialX;

        button.frame=CGRectMake(imageX, imageInitialY, imageW, imageH);

        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

        [_scrollView addSubview:button];

    }

*/

    CGFloat contentw=4*imageW;

    self.scrollView.contentSize=CGSizeMake(contentw, 0);

    self.scrollView.pagingEnabled = YES;

    [self.view addSubview:self.scrollView];

    

    

    _pageControl = [UIPageControl new];

    _pageControl.frame=CGRectMake(0, 200,WEIDTH , 5);

    _pageControl.accessibilityNavigationStyle =

    UIAccessibilityNavigationStyleAutomatic;

    _pageControl.numberOfPages=4;

    _pageControl.pageIndicatorTintColor = [UIColor redColor];// 设置非选中页的圆点颜色

    _pageControl.currentPageIndicatorTintColor = [UIColor blueColor]; // 设置选中页的圆点颜色

    [self.view addSubview:self.pageControl];


 }


-(void)scrollChange{

    NSLog(@"123");

    _timeIndex++; 

    if (_timeIndex == 4) {

        _timeIndex = 0; 

        _scrollView.contentOffset = CGPointMake(WEIDTH * _timeIndex, 0);

    }

    else  {

        _scrollView.contentOffset = CGPointMake(_timeIndex *WEIDTH,0);

    }

}

//手动滑动时候,定时器关闭

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

    NSLog(@"1");

    [_timer invalidate];

    _timer = nil;

}


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

{


    _timeIndex = scrollView.contentOffset.x/WEIDTH ;

    NSLog(@"_timeIndex = %d",_timeIndex);

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

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

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

    _pageControl.currentPage = page;

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值