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

来源:互联网 发布:中小企业员工流失数据 编辑:程序博客网 时间:2024/06/06 07:53

@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)UIPageControl *pageControl;

@property(nonatomic,strong)NSTimer *timer;

@property(nonatomic,assign)int timeIndex;


- (void)viewDidLoad {

    [superviewDidLoad];

    [

    _timeIndex=0;

    _timer = [NSTimerscheduledTimerWithTimeInterval:3target:selfselector:@selector(scrollChange)userInfo:nilrepeats:YES];

  [[NSRunLoopcurrentRunLoop]addTimer:_timerforMode:NSDefaultRunLoopMode];


    _scrollView = [UIScrollViewnew];

    _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=[NSArrayarrayWithObjects:@"1",@"2",@"3",@"4",nil];

/*添加按钮

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

        UIButton *button=[UIButtonbuttonWithType:UIButtonTypeCustom];

        [button setBackgroundImage:[UIImageimageNamed:[arr objectAtIndex:i]]forState:UIControlStateNormal];

        button.tag=i;

        CGFloat imageX = i*imageW + imageInitialX;

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

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

        [_scrollViewaddSubview:button];

    }

*/

    CGFloat contentw=4*imageW;

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

    self.scrollView.pagingEnabled = YES;

    [self.viewaddSubview:self.scrollView];

    

    

    _pageControl = [UIPageControlnew];

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

    _pageControl.accessibilityNavigationStyle =

    UIAccessibilityNavigationStyleAutomatic;

    _pageControl.numberOfPages=4;

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

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

    [self.viewaddSubview: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");

    [_timerinvalidate];

    _timer =nil;

}


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

{


    _timeIndex = scrollView.contentOffset.x/WEIDTH ;

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

    _timer = [NSTimerscheduledTimerWithTimeInterval:3target:selfselector:@selector(scrollChange)userInfo:nilrepeats:YES];

}

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

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

    _pageControl.currentPage = page;

}


阅读全文
1 0