zoukankan      html  css  js  c++  java
  • 同时控制多个UIScrollView对象

    效果:

    分别拖动下方和上方滚动视图

    用数组把所有的UIScrollView对象储存起来。

     1 #import "ViewController.h"
     2 
     3 @interface ViewController () <UIScrollViewDelegate>
     4 
     5 @end
     6 
     7 @implementation ViewController
     8 
     9 @synthesize scrollViews = _scrollViews;
    10 
    11 - (void)viewDidLoad
    12 {
    13     [super viewDidLoad];
    14     
    15     UIImageView *imgViewUp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2.png"]];
    16     UIScrollView *scrollViewUp = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, imgViewUp.frame.size.height)];
    17     scrollViewUp.contentSize = CGSizeMake(imgViewUp.frame.size.width, imgViewUp.frame.size.height);
    18     scrollViewUp.bounces = NO;
    19     scrollViewUp.clipsToBounds = YES;
    20     scrollViewUp.delegate = self;
    21     [scrollViewUp addSubview:imgViewUp];
    22     [self.view addSubview:scrollViewUp];
    23     /////////////////////////////////////////////////////////////////////////////////
    24     float height = imgViewUp.frame.size.height;
    25     UIImageView *imgViewCenter = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
    26     imgViewCenter.userInteractionEnabled = YES;
    27     UIScrollView *scrollViewCenter = [[UIScrollView alloc] initWithFrame:CGRectMake(0, height, self.view.frame.size.width, self.view.frame.size.height-height)];
    28     scrollViewCenter.contentSize = CGSizeMake(imgViewCenter.frame.size.width, imgViewCenter.frame.size.height);
    29     scrollViewCenter.bounces = NO;
    30     scrollViewCenter.clipsToBounds = YES;
    31     // scrollViewCenter的委托是本对象(ViewController)
    32     scrollViewCenter.delegate = self;
    33     [scrollViewCenter addSubview:imgViewCenter];
    34     [self.view addSubview:scrollViewCenter];
    35     
    36     self.scrollViews = [NSArray arrayWithObjects:scrollViewCenter, scrollViewUp, nil];
    37 }
    38 
    39 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    40 {
    41     // 枚举UIScrollView对象数组中每一个对象
    42     for (UIScrollView *view in self.scrollViews) {
    43         // 对非当前UIScrollView对象进行设置偏移量(contentOffset)
    44         if (scrollView != view) {
    45             [view setContentOffset:scrollView.contentOffset];
    46         }
    47     }
    48 }


    /**************************************************************************
                      原文来自博客园——Submarinex的博客: www.cnblogs.com/submarinex/               
      *************************************************************************/

  • 相关阅读:
    python之常用模块
    python 正则
    python 二分法例子及冒泡排序
    python 基础之第十二天(re正则,socket模块)
    python获取系统信息psutil
    python 基础之第十一天(面向对象)
    python 基础之第十天(闭包,装饰器,生成器,tarfile与hashlib模块使用)
    python 基础之第九天
    python 基础之第八天--字典相关
    Python 爬虫监控女神的QQ空间新的说说,实现秒赞,并发送说说内容到你的邮箱
  • 原文地址:https://www.cnblogs.com/submarinex/p/2810555.html
Copyright © 2011-2022 走看看