どのように私はUIScrollViewのための方向にロックを有効にするには?
-
21-08-2019 - |
質問
私は内部UIScrollView
とUIView
を持っています。私はビューが垂直方向にのみスクロールされるように、x軸をロックします。どのように私は方向ロックを有効にするには?
解決
まず、UIScrollViewののフレームの幅以下の幅を有するようにUIScrollView
のcontentSize
を設定します。
次に、NOにUIScrollView's
のalwaysBounceHorizontal
を設定します。これは、あなたが表示するもう横コンテンツはありません、それを言ったにも関わらず、「ラバーバンディング」からスクロールビューを防止します。
UIScrollView *scrollView;
CGSize size = scrollView.contentSize;
size.width = CGRectGetWidth(scrollView.frame);
scrollView.contentSize = size;
scrollView.alwaysBounceHorizontal = NO;
スクロールビュー内で実際に何があるかは関係ありません。
他のヒント
あなたはUIScrollView
をサブクラス化し、touchesBegan:withEvent:
方法、touchesMoved:withEvent:
方法、およびtouchesEnded:withEvent:
メソッドをオーバーライドすることがあります。
あなたはタッチイベントの種類を計算するために、タッチの始点と終点と一緒に、これらのメソッドを使用します開催されました:それは、単純なタップ、または水平または垂直スワイプでしたか?
それは横スワイプである場合は、、あなたはタッチイベントをキャンセルします。
ここではどのように学習するをソースコードを見てみましょうあなたは始めるかもしれません。
#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
所属していません StackOverflow