固定UIScrollView滑动的方向

一般而言,我们通过这两个参数CGRectMake以及contentSize就可以自动的让UIScrollView只往一个方向滚动.但我遇到过非常奇葩的情况,那就是即使设置的CGRectMake以及contentSize没有一点点问题,这个UIScrollView也能够上下左右滚动-_-!!.

为了不依赖于CGRectMake以及contentSize,我们可以通过在代理方法scrollViewDidScroll:中进行限制即可.

没有限制之前的效果:

源码:

  1. //
  2. // RootViewController.m
  3. // BUG
  4. //
  5. // Copyright (c) 2014年 Y.X. All rights reserved.
  6. //
  7.  
  8. #import "RootViewController.h"
  9.  
  10. @interface RootViewController ()<UIScrollViewDelegate>
  11.  
  12. {
  13. UIScrollView *_showView;
  14. }
  15.  
  16. @end
  17.  
  18. @implementation RootViewController
  19.  
  20. - (void)viewDidLoad
  21. {
  22. [super viewDidLoad];
  23.  
  24. UIImageView *showImageView = \
  25. [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"长图.jpg"]];
  26.  
  27. _showView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , , )];
  28. _showView.delegate = self;
  29. [_showView addSubview:showImageView];
  30. _showView.contentSize = showImageView.frame.size;
  31. [self.view addSubview:_showView];
  32. }
  33.  
  34. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  35. {
  36. CGPoint point = scrollView.contentOffset;
  37. // point.y = 0.f;
  38. scrollView.contentOffset = point;
  39. }
  40.  
  41. @end

限制后效果:

  1. //
  2. // RootViewController.m
  3. // BUG
  4. //
  5. // Copyright (c) 2014年 Y.X. All rights reserved.
  6. //
  7.  
  8. #import "RootViewController.h"
  9.  
  10. @interface RootViewController ()<UIScrollViewDelegate>
  11.  
  12. {
  13. UIScrollView *_showView;
  14. }
  15.  
  16. @end
  17.  
  18. @implementation RootViewController
  19.  
  20. - (void)viewDidLoad
  21. {
  22. [super viewDidLoad];
  23.  
  24. UIImageView *showImageView = \
  25. [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"长图.jpg"]];
  26.  
  27. _showView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , , )];
  28. _showView.delegate = self;
  29. [_showView addSubview:showImageView];
  30. _showView.contentSize = showImageView.frame.size;
  31. [self.view addSubview:_showView];
  32. }
  33.  
  34. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  35. {
  36. CGPoint point = scrollView.contentOffset;
  37.  
  38. // 限制y轴不动
  39. point.y = .f;
  40.  
  41. scrollView.contentOffset = point;
  42. }
  43.  
  44. @end

核心代码:

固定UIScrollView滑动的方向的更多相关文章

  1. 移动应用滑动屏幕方向判断解决方案,JS判断手势方向

    问题分类 滑动屏幕打开相应功能操作. 问题描述 1.用户手动滑动屏幕,根据滑动的方向,打开相应的功能(如:向上滑摇钱树经验明细,向下滑打开任务明细,向左滑打开聚宝盆物品查看等功能),滑动事件捕获问题. ...

  2. UIScrollView 滑动复位

    需求 在每次打开界面滑动列表都是复位状态(未滑动). 分析 在制作滑动列表时常常会结合UIPanel和UIScrollView 要让滑动列表回到未滑动时的位置,那么就需要改变Panel的Clippin ...

  3. 头部固定下面滑动&&获取手机屏高

    height(){ //获取屏高 let phone_height = document.documentElement.clientHeight; let group = this.refs.sea ...

  4. UIScrollView 滑动试图

    UIScrollView --->UIView //创建UIScrollView testScrollView=[[UIScrollView alloc]init]; testScrollVie ...

  5. iOS判断UIScrollView的滚动方向

    - (void) scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat newY = scrollView.contentOffset.y; ...

  6. 判断tableVIew滑动的方向

    首先设置一个旧的偏移量为0; self.oldContent = 0; - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scr ...

  7. UIPanGestureRecognizer判断滑动的方向

    .h文件 CGFloat const gestureMinimumTranslation = 20.0 ; typedef enum : NSInteger { kCameraMoveDirectio ...

  8. iOS中如何使定时器NSTimer不受UIScrollView滑动所影响

    以下是使用 scheduledTimerWithTimeInterval 方法来实现定时器 - (void)addTimer { NSTimer scheduledTimerWithTimeInter ...

  9. 用HTML和javascript(JS)计算触屏手机手指滑动方向的演示

    移动终端的流行,程序员希望通过HTML+JS完成触屏动作的识别.下面给出具体实现的例子,供大家参考. 将下面的代码复制并保存,用手机访问,现在的手机浏览器一般都支持触屏,针对本演示来讲就是支持三个js ...

随机推荐

  1. 初识Docker和安装

    什么是Docker Docker的构想是要实现“Build,Ship and Run Any App,Anywhere”,即通过对应用的封装(Packaging).分发(Distribution).部 ...

  2. Low Power之CPF/UPF

    1 CPF The Common Power Format is a standard promoted by the Low Power Coalition at Si2. CPF is also ...

  3. json 只能用 for-in 遍历

    [JS] var json1 = { 'name' : 'yy' , 'age' : 11 , 'fun' : '前端开发' }; for( var attr in json1 ) { alert( ...

  4. 原生js实现拖动滑块验证

    拖动滑块验证是现在的网站随处可见的,各式各样的拖动法都有. 下面实现的是某宝的拖动滑块验证: <!DOCTYPE html> <html lang="en"> ...

  5. Deep Residual Learning for Image Recognition(残差网络)

    深度在神经网络中有及其重要的作用,但越深的网络越难训练. 随着深度的增加,从训练一开始,梯度消失或梯度爆炸就会阻止收敛,normalized initialization和intermediate n ...

  6. VB如何连接访问数据库Access

    VB如何连接访问数据库Access 听语音 | 浏览:10675 | 更新:2015-05-05 11:26 | 标签:连接 access 1 2 3 4 5 6 7 分步阅读 VB即Visual B ...

  7. C# 小软件部分(二)

     此次又新增了一些新的功能,直接接着上次的介绍吧 上次博客介绍地址:http://www.cnblogs.com/Liyuting/p/8540592.html 这次新增了三个功能,具体如下: 一.网 ...

  8. bonecp回缩功能实现

    起因 bonecp不具备回缩功能,即连接池持有连接之后,不会主动去释放这些连接(即使这些连接始终处于空闲状态),因此在使用一段时间之后,连接池会达到配置的最大值. 这种方式一定程度上造成了资源的浪费. ...

  9. Leetcode 计划

    如何正确高效地使用LeetCode? LeetCode按照怎样的顺序来刷题比较好? LeetCode 题目总结/分类 Leetcode 简略题解 - 共567题 500. Keyboard Row [ ...

  10. 【5】Builder模式(构建者模式)

    一.引言 在软件系统中,有时需要创建一个复杂对象,并且这个复杂对象由其各部分子对象通过一定的步骤组合而成.例如一个采购系统中,如果需要采购员去采购一批电脑时,在这个实际需求中,电脑就是一个复杂的对象, ...