• IOS基础控件--滚动视图 UIScrollView和UIScrollViewDelegate协议


    UIScrollView 滚动视图

     1 #import "ViewController.h"
     2 
     3 #import "GlobalDefine.h"
     4 
     5 @interface ViewController ()
     6 
     7 @end
     8 
     9 @implementation ViewController
    10 
    11 - (void)viewDidLoad
    12 {
    13     [super viewDidLoad];
    14     //滚动视图
    15     UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
    16     //图片内容的宽高
    17     scrollView.contentSize = CGSizeMake(5 * SCREENWIDTH, SCREENHEIGHT);
    18     //启用分页
    19     scrollView.pagingEnabled = YES;
    20     //弹动
    21     scrollView.bounces = NO;
    22     
    23     [self.view addSubview:scrollView];
    24     
    25     //添加5张图片
    26     for (int i = 0; i < 5; ++i)
    27     {
    28         UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGHT)];
    29         imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i]];
    30         [scrollView addSubview:imageView];
    31     }
    32 }

    UIScrollViewdDelegate协议

     1 - (void)viewDidLoad
     2 {
     3     [super viewDidLoad];
     4 
     5     //写相关代码
     6 
     7     //遵循协议代理
     8     scrollView.delegate = self;
     9 }
    10 
    11 
    12 //常用方法
    13 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    14 {
    15     NSLog(@"滑动时执行");
    16 }
    17 
    18 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    19 {
    20     NSLog(@"结束拖拽时执行");
    21 }
    22 
    23 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    24 {
    25     NSLog(@"单个滑动结束时执行");
    26     //scrollView.contentOffset.x和scrollView.contentOffser.y计算内容偏移的坐标
    27 }
  • 相关阅读:
    跨域请求剖析
    MongoDB 创建索引及其他
    MongoDB的常用操作总结
    Vue学习【第四篇】:Vue 之webpack打包工具的使用
    转载:官方Caffe-windows 配置与示例运行
    转载:基于HALCON的模板匹配方法总结
    机器视觉硬件相关
    opencv画出轮廓外接矩形
    转载:approxPolyDP函数
    转载:return *this和return this
  • 原文地址:https://www.cnblogs.com/songlei0601/p/5767372.html
Copyright © 2020-2023  润新知