固定UIScrollView滑動的方向

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   color   

固定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(0, 50, 320, 100)];    _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(0, 50, 320, 100)];    _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 = 0.f;        scrollView.contentOffset = point;}@end

核心代碼:

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.