2015-03-04 8 views
0

Je tente de mettre à jour un projet xCode hérité. Essentiellement, je dessine un blanc pour attacher une action au geste de glissement que j'ai intégré dans le projet. Je l'ai seulement fait auparavant avec storyboard mais en raison de l'âge du projet ce n'est pas une option.Modification d'un UiView sans un viewcontroller via swipe

Actuellement, j'ai un contrôleur de vue qui abrite une fenêtre contextuelle d'un UIView distinct. L'utilisateur doit ensuite balayer d'avant en arrière à travers trois pages de contenu. Il y a aussi une autre fenêtre, mais celle-ci n'est qu'une page et ne nécessite donc pas la fonctionnalité. Je peux voir que l'appareil reconnaît mon balayage (à travers la sortie), mais je ne sais pas comment attacher une action pour naviguer vers les autres vues de l'interface utilisateur détenues dans le contrôleur ....... Ou si cela est encore possible et Je vais devoir repenser comment j'ai mis en œuvre cela. Toute aide est appréciée.

Merci

+0

Avez-vous déjà prendre un oeil à' UIPageViewController'? https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPageViewControllerClassReferenceClassRef/ – KIDdAe

+0

Il y a plusieurs bonnes références disponibles sur SO pour vous aider à poser une question. (Voir ici http://stackoverflow.com/help/mcve). – danh

Répondre

0

Vous devriez vraiment utiliser un UIPageViewController mais voici un exemple de code si vous n `utiliser l'une:

@interface ViewController() 

@property (strong, nonatomic) UIView *view1; 
@property (strong, nonatomic) UIView *view2; 
@property (strong, nonatomic) UIView *view3; 
@property (nonatomic) BOOL isSwipeEnabled; 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    self.isSwipeEnabled = YES; 

    self.view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    self.view1.backgroundColor = [UIColor redColor]; 
    [self.view addSubview:self.view1]; 

    self.view2 = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.view1.frame), 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    self.view2.backgroundColor = [UIColor yellowColor]; 
    [self.view addSubview:self.view2]; 

    self.view3 = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.view2.frame), 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    self.view3.backgroundColor = [UIColor blueColor]; 
    [self.view addSubview:self.view3]; 

    UISwipeGestureRecognizer *leftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler:)]; 
    leftGesture.direction = UISwipeGestureRecognizerDirectionLeft; 
    [self.view addGestureRecognizer:leftGesture]; 

    UISwipeGestureRecognizer *rightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler:)]; 
    rightGesture.direction = UISwipeGestureRecognizerDirectionRight; 
    [self.view addGestureRecognizer:rightGesture]; 
} 

-(void)swipeHandler:(UISwipeGestureRecognizer*)recognizer 
{ 
    self.isSwipeEnabled = NO; 
    if(recognizer.direction == UISwipeGestureRecognizerDirectionLeft) 
    { 
     if (self.view3.frame.origin.x > 0) { 
      [self updateMyViewsToXPosition:self.view1.frame.origin.x - self.view.frame.size.width]; 
     } 
    } 
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) 
    { 
     if (self.view1.frame.origin.x != 0) { 
       [self updateMyViewsToXPosition:self.view1.frame.origin.x + self.view.frame.size.width]; 
     } 

    } else { 
     self.isSwipeEnabled = YES; 
    } 
} 

- (void)updateMyViewsToXPosition:(CGFloat)xPos 
{ 
    [UIView animateWithDuration:0.3 animations:^{ 

     self.view1.frame = CGRectMake(xPos, 0, self.view1.frame.size.width, self.view1.frame.size.height); 
     self.view2.frame = CGRectMake(CGRectGetMaxX(self.view1.frame), 0, self.view.frame.size.width, self.view.frame.size.height); 
     self.view3.frame = CGRectMake(CGRectGetMaxX(self.view2.frame), 0, self.view.frame.size.width, self.view.frame.size.height); 

    }completion:^(BOOL finished) { 
     self.isSwipeEnabled = YES; 
    }]; 
}