Question

J'ai un avec un UIScrollView intérieur UIView. Je veux verrouiller l'axe x de sorte que la vue est seulement de défilement vertical. Comment puis-je activer le verrouillage directionnel?

Était-ce utile?

La solution

Tout d'abord, mis à avoir une largeur "s UIScrollView la contentSize qui est égale ou inférieure à la largeur du cadre de la UIScrollView.

Ensuite, réglez sur NO UIScrollView's alwaysBounceHorizontal. Cela permettra d'éviter la vue de défilement de « bandes de caoutchouc », même si vous avez dit qu'il n'y a pas de contenu plus horizontal pour afficher.

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

Peu importe ce qui est en fait dans la vue de défilement.

Autres conseils

Vous serez sous-classement et UIScrollView redéfinissant la méthode touchesBegan:withEvent:, méthode touchesMoved:withEvent: et la méthode touchesEnded:withEvent:.

Vous allez utiliser ces méthodes, ainsi que les points de début et de fin d'une touche, pour calculer ce genre d'événement tactile a eu lieu: était-ce une simple pression, ou un simple glissement horizontal ou vertical?

Si elle est un simple glissement horizontal, vous annulez l'événement touche.

Jetez un coup d'oeil au code source ici pour apprendre vous pourriez commencer.

#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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top