滚动视图 定时器

首先 直接在vc.m里
定义 滚动视图 各属性

UIScrollView *scroll;       // 滚动视图
UIPageControl *page;        // 分页控件

// 初始化滚动式图
scro = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 200)];
// 设置滚动范围
scro.contentSize = CGSizeMake(3 * self.view.frame.size.width, 0);
// 禁用弹簧效果
scro.bounces = NO;
// 禁用水平滚动
scro.showsHorizontalScrollIndicator = NO;
// 设置整页滚动
scro.pagingEnabled = YES;
// 设置代理
scro.delegate = self;
// 设置滚动图片
for(int i = 0 ; i < 3 ; i++){
// 设置图片数组
NSArray *arr = @[@“1”,@“2”,@“3”];
UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, 200)];
// 加载图片
imgV.image = [UIImage imageNamed:arr[i]];
// 添加到滚动视图中
[scro addSubview:imgV];

    }
    // 添加到cell中
    [cell addSubview:scro];
    // 设置豆豆
    pag = [[UIPageControl alloc]initWithFrame:CGRectMake(90, 95, 150, 25)];
    // 设置豆豆的数量
    pag.numberOfPages = 3;
    // 设置豆豆的颜色
    pag.currentPageIndicatorTintColor = [UIColor orangeColor];
    pag.pageIndicatorTintColor = [UIColor whiteColor];
    // 添加到单元格中
    [cell addSubview:pag];
    // 创建定时器
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(aaa) userInfo:nil repeats:YES];

return cell;

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

// NSLog(@"%lf",scro.contentOffset.x);
pag.currentPage = scro.contentOffset.x/self.view.frame.size.width;

}
// 定时器
-(void)aaa{
[scro setContentOffset:CGPointMake(k * self.view.frame.size.width, 0)];
k ++;
if(k > 2){
k = 0;
}
}

猜你喜欢

转载自blog.csdn.net/WNEHIUZHNAG/article/details/84101608
今日推荐