您好我正在为iPhone开发应用程序,但我无法显示2个隐藏视图。 我在这里发布我的故事板: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上做出一些手势时,它会向我显示错误的视图,或者它会移动视图,应该为我所做的手势修复。 我怎么能解决这个问题?你可以帮我修剪一下代码吗? 谢谢
答案 0 :(得分:1)
我认为您应该使用另一种方法来解决此问题:使用UIPanGestureRecognizer并使用velocity可以检测手势的方向。 再见