2016-08-17 193 views
0

在這裏我有三個視圖,一個是左,中,右,所以我想添加滑動功能,我嘗試了以下方法,但仍然無法使它。如何做到這一點。添加滑動手勢,無法滑動

CODE:

- (void)viewDidLoad { 
     [super viewDidLoad]; 
     // Do any additional setup after loading the view, typically from a nib. 
    } 


    - (void)viewWillAppear:(BOOL)animated{ 
     [super viewWillAppear:animated]; 

     UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerRight:)]; 
     [gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; 
     [self.view addGestureRecognizer:gestureRecognizer]; 
     //Left Swipe 

     UISwipeGestureRecognizer *gestureRecognizer2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerLeft:)]; 
     [gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; 
     [self.view addGestureRecognizer:gestureRecognizer2]; 

    } 

轉型爲右

-(void)swipeHandlerRight:(id)sender 
    { 

     RightViewController *videoScreen=[self.storyboard instantiateViewControllerWithIdentifier:@"RightViewController"]; 


CATransition* transition = [CATransition animation]; 
    transition.duration = 0; 
    transition.type = kCATransitionFromRight; 
    // transition.subtype = kCATransitionFromRight; 
    [self.view.window.layer addAnimation:transition forKey:kCATransition]; 
    [self presentViewController:videoScreen animated:NO completion:nil]; 


    } 

轉變爲左

-(void)swipeHandlerLeft:(id)sender 
    { 
     LeftViewController *videoScreen=[self.storyboard instantiateViewControllerWithIdentifier:@"LeftViewController"]; 
     CATransition* transition = [CATransition animation]; 
    transition.duration = 0; 
    transition.type = kCATransitionFromLeft; 
    // transition.subtype = kCATransitionFromRight; 
    [self.view.window.layer addAnimation:transition forKey:kCATransition]; 


    } 
+0

你想達到什麼,我認爲你應該使用頁面視圖控制器或在滾動視圖中放置3個視圖並啓用分頁。根據您的要求,讓scrollview的寬度儘可能大。 –

+0

那些方法是不是一次性調用? – Nilesh

+0

@NileshJha當我加入一個斷點,並運行該程序的視圖會出現被調用,當我向左滑動該斷點轉到leftViewController和同爲右,但仍中間標籤是出現在那裏,當我應該表現出左側標籤向左滑動,因爲我在各自的屏幕上添加了標籤 – virat

回答

0

替換此

[self.navigationController pushViewController:videoScreen animated:YES]; 

通過這個

[self presentViewController:videoScreen animated:YES completion:NULL]; 

它會使你的視圖控制器出現。但是,由於左側或右側視圖控制器沒有手勢識別器,所以您不會從這些視圖控制器返回。爲此,您已使它們喜歡滑動。

+0

雅這是工作,但我想要的動畫像它應該根據刷卡出現,但它是從底部呈現留下的viewController,但它應該來自左 – virat

+0

@virat做你添加UINavigationControl LER或不? –

+0

@iRaju不,我沒加 – virat

0

我得到了解決方案brother.I嘗試示例項目爲您的問題。現在我得到了解決方案。

首先,我在故事板中設置ViewControllers。請參閱下面屏幕截圖中具有UINavigationController,ViewController,LeftViewController,RightViewController的故事板。

enter image description here

然後在ViewController.m

#import "ViewController.h" 
#import "RightViewController.h" 
#import "LeftViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    //swipe right 
    UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerRight:)]; 
    [gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; 
    [self.view addGestureRecognizer:gestureRecognizer]; 

    //swipe left 
    UISwipeGestureRecognizer *gestureRecognizer2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerLeft:)]; 
    [gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; 
    [self.view addGestureRecognizer:gestureRecognizer2]; 

} 

#pragma mark - swipe right function 
-(void)swipeHandlerRight:(id)sender 
{ 
    RightViewController *videoScreen=[self.storyboard instantiateViewControllerWithIdentifier:@"RightViewController"]; 

    CATransition *animationRight = [CATransition animation]; 
    [animationRight setDuration:0]; 
    [animationRight setType:kCATransitionFromRight]; 
    [animationRight setSubtype:kCATransitionFromRight]; 
    [animationRight setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 
    [self.view.window.layer addAnimation:animationRight forKey:kCATransition]; 
    [self presentViewController:videoScreen animated:NO completion:nil]; 
} 

#pragma mark - swipe left function 
-(void)swipeHandlerLeft:(id)sender 
{ 
    LeftViewController *videoScreen=[self.storyboard instantiateViewControllerWithIdentifier:@"LeftViewController"]; 
    CATransition *animationLeft = [CATransition animation]; 
    [animationLeft setDuration:0]; 
    [animationLeft setType:kCATransitionFromLeft]; 
    [animationLeft setSubtype:kCATransitionFromLeft]; 
    [animationLeft setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 
    [self.view.window.layer addAnimation:animationLeft forKey:kCATransition]; 
    [self presentViewController:videoScreen animated:NO completion:nil]; 
} 

RightViewController.m

#import "RightViewController.h" 

@interface RightViewController() 

@end 

@implementation RightViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Back to MiddleView 
- (IBAction)actionBackToMiddleView:(id)sender 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 
@end 

LeftViewController.m

#import "LeftViewController.h" 

@interface LeftViewController() 

@end 

@implementation LeftViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Back to MiddleView 
- (IBAction)actionBackToMiddleViewFromLeftView:(id)sender 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 
@end