滚动视图

ViewController.m

#import "ViewController.h"
#define HCWidth self.view.frame.size.width
#define HCHeigth self.view.frame.size.height

@interface ViewController ()<UIScrollViewDelegate>{
    UIScrollView *sc; //滚动式图
    UIPageControl *pageC; //分页控制符
    NSArray *imgArr; //图片数组
    NSTimer *timer; //时间定时器
    int k; //计数
}

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化定时器
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(qqq) userInfo:nil repeats:YES];
    
    //定时器开启
    [timer fire];
    
    //滚动式图
    sc = [[UIScrollView alloc] initWithFrame:self.view.frame];
    //设置滚动式图的代理
    sc.delegate = self;
    //滚动范围
    sc.contentSize = CGSizeMake(HCWidth*4, HCHeigth);
    //分页
    sc.pagingEnabled = YES;
    //弹簧
    sc.bounces = NO;
    //禁用水平滚动条
    sc.showsHorizontalScrollIndicator = NO;
    //添加图片
    for (int i = 0; i < 4; i++) {
        //图片
        UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(HCWidth*i, 0, HCWidth, HCHeigth)];
        //图片数组
        imgArr = @[@"1",@"2",@"3",@"4"];
        //设置图片交互
        imgV.userInteractionEnabled = YES;
        //添加图片
        imgV.image = [UIImage imageNamed:imgArr[i]];
        //设置跳转按钮
        if (i == 3) {
            //创建按钮
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(200, 550, 100, 50)];
            //给按钮设置一个背景颜色
            btn.backgroundColor = [UIColor redColor];
            //添加事件
            [btn addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside];
            //添加到图片上
            [imgV addSubview:btn];
        }
        
        //添加到滚动式图
        [sc addSubview:imgV];
        
        
        
        
    }
    [self.view addSubview:sc];
    
    //豆豆
    pageC = [[UIPageControl alloc] initWithFrame:CGRectMake((HCWidth-200)/2, 660, 200, 40)];
    //豆豆的数量
    pageC.numberOfPages = imgArr.count;
    //豆豆颜色
    pageC.pageIndicatorTintColor = [UIColor blackColor];
    pageC.currentPageIndicatorTintColor = [UIColor orangeColor];
    //默认选中
    pageC.currentPage = 0;
    //将豆豆网滚动视图里加
    [self.view addSubview:pageC];
    
    
    
}
//视图已经滚动的方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    pageC.currentPage = sc.contentOffset.x/HCWidth;
    NSLog(@"%lf",sc.contentOffset.x);
}
//按钮点击方法
-(void)abc{
    
}
//定时器 (1s -> 进入一次)
-(void)qqq{
    
    NSLog(@"%d",k);
    //set 方法 -> 设置 滚动式图的偏移量
    [sc setContentOffset:CGPointMake(k*HCWidth, 0)];
    k++;
    if (k > 3) {
        k=0;
        //定时器停止
//        [timer invalidate];
//        sc.scrollEnabled = NO;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作者gltwy,代码LTScrollView-master,LTScrollView-master能够无限滚动,可以设定视图出现的数量,总数量,实现无限滚动。通过协议方法和数据源定制自己的滚动视图。 Demo文件路径以及说明 LTScrollView / Example : 为 Swift 使用示例. LTScrollView / OCExample : 为 OC 使用示例. 支持的子View为UIScrollView、UICollectionView、UITableView. CocoaPods安装 安装CocoaPods 使用以下命令: $ gem install cocoapods Podfile 在你的 Podfile中添加LTScrollView source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' target 'TargetName' do #注意此处需要添加use_frameworks! use_frameworks! pod 'LTScrollView', '~> 0.1.7' end 然后,使用以下命令安装 $ pod install 提示错误 [!] Unable to find a specification for LTScrollView 解决办法: $ pod repo remove master $ pod setup Swift使用说明 Swift.LTSimple使用说明 创建LTSimpleManager实例对象 @objc public init(frame: CGRect, viewControllers: [UIViewController], titles: [String], currentViewController:UIViewController, layout: LTLayout) 设置headerView @objc public func configHeaderView(_ handle: (() -> UIView?)?) 子控制器中glt_scrollView进行赋值 self.glt_scrollView = self.tableView(self.scrollView / self.collectionView) 更多使用说明请参考Demo(LTScrollView / Example) Swift.LTAdvanced使用说明 创建LTAdvancedManager实例对象、并设置headerView @objc public init(frame: CGRect, viewControllers: [UIViewController], titles: [String], currentViewController:UIViewController, layout: LTLayout, headerViewHandle handle: () -> UIView) 子控制器中glt_scrollView进行赋值 self.glt_scrollView = self.tableView(self.scrollView / self.collectionView) 更多使用说明请参考Demo(LTScrollView / Example) OC使用说明 OC.LTSimple使用说明 创建LTSimpleManager实例对象 [[LTSimpleManager alloc] initWithFrame:frame viewControllers:self.viewControllers titles:self.titles currentViewController:self layout:self.layout] 设置headerView [self.managerView configHeaderView:^UIView * _Nullable{ }] 子控制器中glt_scrollView进行赋值 self.glt_scrollView = self.tableView(self.scrollView / self.collectionView) 更多使用说明请参考Demo(LTScrollView / OCExample) OC.LTAdvanced使用说明 创建LTAdvancedManager实例对象、并设置headerView [[LTAdvancedManager alloc] initWithFrame:frame viewControllers:self.viewControllers titles:self.titles

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值