我有UIScrollView
一个UIView
里面。我想锁定 x 轴,以便视图仅垂直滚动。如何启用定向锁定?
问问题
19139 次
3 回答
16
首先,将UIScrollView
'scontentSize
的宽度设置为等于或小于 UIScrollView 框架的宽度。
接下来,设置UIScrollView's
alwaysBounceHorizontal
为 NO。即使您告诉它没有更多的水平内容要显示,这也将防止滚动视图出现“橡皮筋”。
UIScrollView *scrollView;
CGSize size = scrollView.contentSize;
size.width = CGRectGetWidth(scrollView.frame);
scrollView.contentSize = size;
scrollView.alwaysBounceHorizontal = NO;
滚动视图中的实际内容并不重要。
斯威夫特 5.0
let scrollView = UIScrollView() // Or however you want to initialize it
var size = scrollView.contentSize
size.width = scrollView.frame.width
scrollView.contentSize = size
scrollView.alwaysBounceHorizontal = false
于 2009-05-14T07:42:46.493 回答
2
您将继承UIScrollView
和覆盖touchesBegan:withEvent:
方法、touchesMoved:withEvent:
方法和touchesEnded:withEvent:
方法。
您将使用这些方法以及触摸的起点和终点来计算发生了什么样的触摸事件:是简单的点击,还是水平或垂直滑动?
如果是水平滑动,则取消触摸事件。
看看这里的源代码,了解如何开始。
于 2009-05-14T07:41:55.200 回答
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
于 2015-03-08T10:15:26.857 回答