显示隐藏的视图

时间:2013-06-12 09:53:55

标签: objective-c animation ios6 uiview gesture

我会问你是否可以帮我展示一些隐藏的观点。另外在我的应用程序中我有2个隐藏视图:一个在左边,第二个在右边。 我想通过使用2按钮和2次滑动来显示这个隐藏的视图(主视图应该跟随手指)。我在网站上建立了(网站是:http://divcode.blogspot.it/2012/09/hidden-menu-part-2-following-finger.html)一个示例代码,只显示一个隐藏的视图。这段代码向我展示了左侧的隐藏视图(我将主视图移动到右侧),如果我想在右侧看到隐藏的视图,我该怎么办? 我会在这里发布我的代码:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *viewAllShow;
@property (weak, nonatomic) IBOutlet UIView *viewMain;
@property (weak, nonatomic) IBOutlet UIScrollView *viewScroll;
@property (weak, nonatomic) IBOutlet UIView *viewAbout;

- (IBAction)buttonAllShow:(id)sender;
- (IBAction)buttonInfo:(id)sender;
@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.viewScroll.contentSize = self.viewAbout.frame.size;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated {
}


#pragma mark - actions -
- (IBAction)buttonAllShow:(id)sender {
    // Controllo se la vista nascosta non è già visibile
    if (self.viewMain.frame.origin.x == 0) {
        // Chiamata alla funzione per la visualizzazione della vista nascosta
        [self showAllShowView];
    } else {
        // Chiamata alla funzione per nascondere la vista nascosta
        [self hideAllShowView];
    }
}

- (IBAction)buttonInfo:(id)sender {
    if (self.viewMain.frame.origin.x == 0) {
        [self showAboutView];
    } else {
        [self hideAboutView];
    }
}

#pragma mark - animations -
- (void)showAllShowView {
    // Faccio partire l'animazione
    [UIView animateWithDuration:0.5
                     animations:^{
                         [self.viewMain setFrame:CGRectMake(self.viewAllShow.frame.size.width, self.viewMain.frame.origin.y, self.viewMain.frame.size.width, self.viewMain.frame.size.height)];
                     }
     ];
}

- (void)hideAllShowView {
    [UIView animateWithDuration:0.5
                     animations:^ {
                         [self.viewMain setFrame:CGRectMake(0, self.viewMain.frame.origin.y, self.viewMain.frame.size.width, self.viewMain.frame.size.height)];
                     }
     ];
}

- (void)showAboutView {
    [UIView animateWithDuration:0.5
                     animations:^{
                         [self.viewMain setFrame:CGRectMake(self.viewAbout.frame.size.width, self.viewMain.frame.origin.y, self.viewMain.frame.size.width, self.viewMain.frame.size.height)];
                     }
     ];

}

- (void)hideAboutView {
    [UIView animateWithDuration:0.5
                     animations:^{
                         [self.viewMain setFrame:CGRectMake(0, self.viewMain.frame.origin.y, self.viewMain.frame.size.width, self.viewMain.frame.size.height)];
                     }
     ];

}

#pragma mark - touch event -
float difference;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint mainTouchPoint = [[touches anyObject] locationInView:self.viewMain];
    difference = mainTouchPoint.x;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    // Ottengo le coordinate del punto dove ho toccato il display
    CGPoint pointInView = [[touches anyObject] locationInView:self.view];

    // Calcolo la differenza tra il punto toccato e la vista principale (viewMain)
    float xTarget = pointInView.x - difference;

    // Se la differenza è maggiore della dimensione x della vista nascosta => imposto il valore della differenza pari alla dimensione x della vista nascosta
    if (xTarget > self.viewAllShow.frame.size.width) {
        xTarget = self.viewAllShow.frame.size.width;
    } else if (xTarget < 0) {
        // Se la differenza è minore di zero => imposto la differenza a zero e faccio l'animazione
        xTarget = 0;
    }
    // Eseguo l'animazione
    [UIView animateWithDuration:0.5
                     animations:^{
                         [self.viewMain setFrame:CGRectMake(xTarget, self.viewMain.frame.origin.y, self.viewMain.frame.size.width, self.viewMain.frame.size.height)];
                     }
     ];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    // Ottengo le coordinate del punto toccato sullo schermo
    CGPoint endPoint = [[touches anyObject]locationInView:self.view];
    // Calcolo la differenza tra il punto toccato e la vista principale (viewMain)
    float xTarget = endPoint.x - difference;
    // Se la differenza è maggiore della metà della dimensione x della vista nascosta imposto la differenza pari a zero
    if (xTarget < self.viewAllShow.frame.size.width/2) {
        xTarget = 0;
    } else {
        // Altrimenti imposto la differenza pari alla dimensione x della vista nascosta
        xTarget = self.viewAllShow.frame.size.width;
    }
    // Eseguo l'animazione
    [UIView animateWithDuration:0.5
                     animations:^{
                         [self.viewMain setFrame:CGRectMake(xTarget, self.viewMain.frame.origin.y, self.viewMain.frame.size.width, self.viewMain.frame.size.height)];
                     }
     ];
}

@end

如果您转到此链接,您会看到我的故事板的屏幕截图:http://postimg.org/image/m0001j00t/

我希望你能帮助我解决这个问题。 谢谢

1 个答案:

答案 0 :(得分:1)

使用UIPanGestureRecoginizer并使用手势的速度来决定滑动是从左到右还是从右到左,并显示相应的隐藏视图。