固定UIScrollView滑动的方向

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

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

没有限制之前的效果:

源码:

//
// RootViewController.m
// BUG
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h" @interface RootViewController ()<UIScrollViewDelegate> {
UIScrollView *_showView;
} @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; UIImageView *showImageView = \
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"长图.jpg"]]; _showView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , , )];
_showView.delegate = self;
[_showView addSubview:showImageView];
_showView.contentSize = showImageView.frame.size;
[self.view addSubview:_showView];
} - (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint point = scrollView.contentOffset;
// point.y = 0.f;
scrollView.contentOffset = point;
} @end

限制后效果:

//
// RootViewController.m
// BUG
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h" @interface RootViewController ()<UIScrollViewDelegate> {
UIScrollView *_showView;
} @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; UIImageView *showImageView = \
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"长图.jpg"]]; _showView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , , )];
_showView.delegate = self;
[_showView addSubview:showImageView];
_showView.contentSize = showImageView.frame.size;
[self.view addSubview:_showView];
} - (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint point = scrollView.contentOffset; // 限制y轴不动
point.y = .f; scrollView.contentOffset = point;
} @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. CentOS Maven 删除 *.lastUpdated文件

    find . -name "*.lastUpdated" -exec rm -rf {} \;

  2. springMVC对于Controller返回值的可选类型

    2018-01-11 对于springMVC处理方法支持支持一系列的返回方式:  (1)ModelAndView (2)Model (3)ModelMap (4)Map (5)View (6)Stri ...

  3. angularjs 依赖注入原理与实现

    在用angular依赖注入时,感觉很好用,他的出现是 为了“削减计算机程序的耦合问题” ,我怀着敬畏与好奇的心情,轻轻的走进了angular源码,看看他到底是怎么实现的,我也想写个这么牛逼的功能.于是 ...

  4. Javascript一个在页面内追加元素的小例子

    如下: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8 ...

  5. bonecp回缩功能实现

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

  6. Java代理(一)

    先来看看Java的静态代理,假设有如下接口和实现方法: package proxy; public interface Subject { public void request(); public ...

  7. 数据库连接池使用(一):使用C#数据库连接池

         一.导读      使用C#数据库连接池,连接到数据库服务器通常由几个需要软长时间的步骤组成:      1.必须与服务器进行初次连接:      2.必须分析连接字符串信息:      3 ...

  8. FZU2150(KB1-I)

    Fire Game Accept: 1955    Submit: 6880Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Descr ...

  9. STL:vector<bool> 和bitset

    今天某个地方要用到很多位标记于是想着可以用下bitset,不过发现居然是编译时确定空间的,不能动态分配.那就只能用vector来代替一下了,不过发现居然有vector<bool>这个特化模 ...

  10. 用hmac验证客户端的合法性

    服务器端程序 import os import hmac import socket def auth(conn): secret_key = b'rock' rand_b = os.urandom( ...