2009-05-14 31 views

Trả lời

17

Đầu tiên, đặt "contentSize" của UIScrollView để có chiều rộng bằng hoặc nhỏ hơn chiều rộng khung của UIScrollView.

Tiếp theo, đặt UIScrollView là "alwaysBounceHorizontal" thành NO. Điều này sẽ ngăn chế độ xem cuộn từ "dải cao su" mặc dù bạn đã nói rằng không có nội dung ngang nào để hiển thị.

UIScrollView *scrollView; 
CGSize size = scrollView.contentSize; 
size.width = CGRectGetWidth(scrollView.frame); 
scrollView.contentSize = size; 
scrollView.alwaysBounceHorizontal = NO; 

Không quan trọng những gì thực sự nằm trong chế độ xem cuộn.

+0

Điều này đã giúp tôi ngay cả trong Xcode 7. Tôi đã phải tắt thư bị trả lại trong bảng phân cảnh để có được khóa hướng hoạt động (mặc dù tôi đã bật chế độ khóa hướng). –

2

Bạn sẽ được phân lớp UIScrollView và ghi đè phương pháp touchesBegan:withEvent:, phương pháp touchesMoved:withEvent: và phương thức touchesEnded:withEvent:.

Bạn sẽ sử dụng các phương pháp đó, cùng với điểm bắt đầu và điểm kết thúc của một lần chạm để tính loại sự kiện chạm nào: đó là một cú chạm đơn giản hoặc vuốt ngang hoặc dọc?

Nếu đó là vuốt ngang, bạn hủy sự kiện chạm.

Hãy xem qua số source code here để tìm hiểu cách bạn có thể bắt đầu.

+0

Tốt hơn để thông báo cho chế độ xem cuộn về ý định của bạn, tôi nghĩ thay vì tự hủy chạm vào chính bạn. Với cách tiếp cận của bạn, nếu bạn bắt đầu kéo theo chiều ngang (ngay cả khi tình cờ), thì bạn không thể kéo theo chiều dọc và làm việc đó được. –

+0

Tôi sợ ai đó sẽ nói điều này ... Một cách chắc chắn để khóa một trục tôi chắc chắn, nhưng tôi nghĩ rằng tôi sẽ tiếp tục tìm kiếm một cách lười biếng hơn để làm điều này. Cảm ơn phản ứng của bạn mặc dù. Nick Farina đã giải quyết vấn đề của tôi bằng cách đề xuất: Trước tiên, hãy đặt "contentSize" của UIScrollView để có chiều rộng bằng hoặc nhỏ hơn chiều rộng khung của UIScrollView. Tiếp theo, đặt "alwaysBounceHorizontal" của UIScrollView thành NO. Điều này sẽ ngăn chế độ xem cuộn từ "dải cao su" mặc dù bạn đã nói rằng không có nội dung ngang nào để hiển thị. – RexOnRoids

0
#import <UIKit/UIKit.h> 


@interface DemoButtonViewController : UIViewController <UIScrollViewDelegate> 

@property (nonatomic, strong) UIScrollView *filterTypeScrollView; 
@property (nonatomic, strong) UIBarButtonItem *lockButton; 

- (void)lockFilterScroll:(id)sender; 

@end 

#import "DemoButtonViewController.h" 

@implementation DemoButtonViewController 

@synthesize filterTypeScrollView; 
@synthesize lockButton; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

    if (self) 
    { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.view.backgroundColor = [UIColor darkGrayColor]; 
    self.filterTypeScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 130, self.view.frame.size.width, 320)]; 
    filterTypeScrollView.contentSize = CGSizeMake(self.view.frame.size.width*4, 320); 
    filterTypeScrollView.pagingEnabled = YES; 
    filterTypeScrollView.delegate = self; 
    [self.view addSubview:filterTypeScrollView]; 

    UIToolbar *lockbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 450, self.view.frame.size.width, 30)]; 
    lockbar.barStyle = UIBarStyleBlackTranslucent; 
    self.lockButton = [[UIBarButtonItem alloc] initWithTitle:@"Lock Filter Scroll" style:UIBarButtonItemStylePlain target:self action:@selector(lockFilterScroll:)]; 
    [lockbar setItems:[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],lockButton,nil]]; 
    [self.view addSubview:lockbar]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (void)lockFilterScroll:(id)sender 
{ 
    filterTypeScrollView.scrollEnabled = !filterTypeScrollView.scrollEnabled; 

    if (filterTypeScrollView.scrollEnabled) 
    { 
     [lockButton setTitle:@"Lock Filter Scroll"]; 
    } 
    else { 
     [lockButton setTitle:@"Unlock Filter Scroll"]; 
    } 
} 

@end 
Các vấn đề liên quan