ios - 使用跟随手指 ios 显示两个隐藏 View

标签 ios objective-c gesture touch-event

您好,我正在为 iPhone 开发一个应用程序,但我无法显示 2 个隐藏 View 。 我在这里发布我的 Storyboard:http://postimg.org/image/v6nhepqqd/ 当我从左向右滑动手指时,我想显示 View - All Show,然后当我从右向左滑动手指时,我想显示 View - About。滑动手势必须类似于 youtube 菜单。 我只使用一个 ViewController,我将在此处发布我的代码:

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 其他人说我使用 UIPanGestureRecognizer,但如果我想使用我在此处发布的方法,我应该怎么做?我的问题是,当我尝试在我的模拟器/真实 iPhone 上做一些手势时,它向我显示了错误的 View ,或者它移动了应该为我所做的手势修复的 View 。 我该如何解决这个问题?你能帮我剪下代码吗? 谢谢

最佳答案

我认为您应该使用另一种方法来解决此问题:使用 UIPanGestureRecognizer 并通过速度检测手势的方向。 再见

关于ios - 使用跟随手指 ios 显示两个隐藏 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17069367/

相关文章:

ios - 如何在 iOS/Objective C 中 DRY 代码?

ios - presentationNode 的位置正在重置

ios - 将应用提交到使用iCloud的App Store,无效的权利

ios - 在 ios6 和 ios7 中的 MPMoviePlayerViewController 中将横向旋转为纵向

ios - 关于BoutTime游戏swift 3 Next Round的一些问题

ios - 当位置数发生变化时,无法为 CAGradientLayer 的位置属性设置动画

objective-c - Swift CoreData DictionaryResultType 转换

ios - 如何手动将后退按钮添加到我的 NavigationBar?

swiftui - 有人可以使用 SwiftUI+MapKit+LongPress 吗?

android - 带有手势和项目点击监听器的 GridView