2

こんにちは、iPhone 用のアプリを開発していますが、2 つの非表示ビューを表示するのに問題があります。私はここに自分のストーリーボードを投稿します: http://postimg.org/image/v6nhepqqd/ ビューを表示したかった - 左から右に指をスワイプするとすべて表示され、次にビューを表示したい - スワイプしたときについて指を右から左へ。スワイプ ジェスチャは YouTube メニューに似ている必要があります。ViewController を 1 つだけ使用しており、ここにコードを投稿します。

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 UIView *viewAbout;

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

ViewController.m

#import "ViewController.h"

@interface ViewController ()
@property BOOL viewMainShowed;
@end

@implementation ViewController

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

- (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)];
                     }
     ];
    self.viewMainShowed = NO;
}

- (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)];
                     }
     ];
    self.viewMainShowed = YES;
}

- (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)];
                     }
     ];
    [UIView animateWithDuration:0.5
                     animations:^{
                         [self.viewAllShow setFrame:CGRectOffset(self.viewAllShow.frame, -270, 0)];
                     }];
    self.viewMainShowed = NO;

}

- (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)];
                     }
     ];
    [UIView animateWithDuration:0.5
                     animations:^{
                         [self.viewAllShow setFrame:CGRectOffset(self.viewAllShow.frame, 270, 0)];
                     }];
    self.viewMainShowed = YES;

}

#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 {

    CGPoint pointInView = [[touches anyObject] locationInView:self.view];

    float xTarget = pointInView.x - difference;

    if (xTarget > self.viewAllShow.frame.size.width) {
        xTarget = self.viewAllShow.frame.size.width;
    } else if (xTarget < 0) {
        if (!self.viewMainShowed) {
            xTarget = 0;
            [UIView animateWithDuration:0.5
                             animations:^{
                                 [self.viewAllShow setFrame:CGRectOffset(self.viewAllShow.frame, 270, 0)];
                             }
             ];
            [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)];
                             }
             ];
            self.viewMainShowed = YES;
        }
        else {
            xTarget = self.viewAbout.frame.size.width * -1;
            [UIView animateWithDuration:0.5
                             animations:^{
                                 [self.viewAllShow setFrame:CGRectOffset(self.viewAllShow.frame, -270, 0)];
                             }
             ];
            [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)];
                             }
             ];
            self.viewMainShowed = NO;
        }
    } else if (xTarget > 0 && xTarget < self.viewAllShow.frame.size.width) {
        xTarget = self.viewAllShow.frame.size.width;
        self.viewMainShowed = NO;
        [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)];
                         }
         ];
    }
//    [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)];
//                     }
//     ];
    //self.viewMainShowed = NO;

}

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

    CGPoint endPoint = [[touches anyObject]locationInView:self.view];

    float xTarget = endPoint.x - difference;

    if (xTarget < self.viewAllShow.frame.size.width/2) {
        if (!self.viewMainShowed) {
            xTarget = 0;
            [UIView animateWithDuration:0.5
                             animations:^{
                                 [self.viewAllShow setFrame:CGRectOffset(self.viewAllShow.frame, 270, 0)];
                             }
             ];
        } else {
            xTarget = self.viewAbout.frame.size.width * -1;
            [UIView animateWithDuration:0.5
                             animations:^{
                                 [self.viewAllShow setFrame:CGRectOffset(self.viewAllShow.frame, -270, 0)];
                             }
             ];
            self.viewMainShowed = NO;
        }

    } //else
    if (xTarget > 0 && xTarget < self.viewAllShow.frame.size.width){

        xTarget = self.viewAllShow.frame.size.width;
        [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)];
                         }
         ];
        self.viewMainShowed = NO;
    }
    if (xTarget < 0) {
        xTarget = 0;
        [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)];
                         }
         ];
        self.viewMainShowed = YES;
    }

//    [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)];
//                     }
//     ];
    //self.viewMainShowed = NO;
}

@end

BOOL 変数を使用して、メイン ビューがアクティブかどうかを確認しました。このサイトで移動してタッチを検出するコードを作成しました : http://divcode.blogspot.it/2012/09/hidden-menu-part-2-following-finger.htmlここに投稿した方法を使用したい場合は、どうすればよいですか? 私の問題は、シミュレーター/実際のiPhoneでジェスチャーを作成しようとすると、間違ったビューが表示されるか、作成したジェスチャーに対して修正する必要があるビューが移動することです。どうすればこの問題を解決できますか? 切り取られたコードを手伝ってもらえますか? ありがとうございました

4

1 に答える 1

1

この問題を解決するには、別の方法を使用する必要があると思います。UIPanGestureRecognizer を使用すると、ジェスチャの方向を速度で検出できます。さよなら

于 2013-06-13T14:08:57.837 に答える