• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 11:43:30 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

您好,我正在为 iPhone 开发一个应用程序,但无法显示 2 个隐藏 View 。 我在这里发布我的 Storyboard:http://postimg.org/image/v6nhepqqd/ 我想显示 View - 当我从左向右滑动手指时显示全部显示,然后我想显示 View - 关于当我从右向左滑动手指时。滑动手势必须类似于 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)buttonAllShowid)sender;
- (IBAction)buttonInfoid)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)viewDidAppearBOOL)animated {
}


#pragma mark - actions -
- (IBAction)buttonAllShowid)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)buttonInfoid)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)touchesBeganNSSet *)touches withEventUIEvent *)event {
    CGPoint mainTouchPoint = [[touches anyObject] locationInView:self.viewMain];
    difference = mainTouchPoint.x;
}

- (void)touchesMovedNSSet *)touches withEventUIEvent *)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)touchesEndedNSSet *)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 。 我该如何解决这个问题?你能帮我剪下代码吗? 谢谢



Best Answer-推荐答案


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

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap