您好,我正在为 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 。 我该如何解决这个问题?你能帮我剪下代码吗? 谢谢
我认为您应该使用另一种方法来解决此问题:使用 UIPanGestureRecognizer 并通过速度检测手势的方向。 再见
关于ios - 使用跟随手指ios显示两个隐藏 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17069367/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |