Hi I'm developing an app for iPhone and I'm having trouble to show 2 hidden view. I post here my storyboard: http://postimg.org/image/v6nhepqqd/ I wanted to show the View - All Show when I swipe a finger from left to right, then I want to show the View - About when I swipe a finger from right to left. The swipe gesture must be similar to the youtube menu. I'm using just one ViewController and I will post here my code:
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
I used a BOOL variable to check if the main view is active or not. I founded the code to move and detect a touch on this site: http://divcode.blogspot.it/2012/09/hidden-menu-part-2-following-finger.html Other people said me to use UIPanGestureRecognizer, but if I want to use the method i post here what I should do? My problem is that when I try to make some gesture on my simulator/real iPhone it shows me the wrong view or it moves view that should be fixed for the gesture i made. How I can solve this problem? Can you help me with a code snipped? Thank you