2013-06-25 170 views
0

我有一个应用程序在一个视图控制器上有两个视图。一种观点在另一种观点之上。当有人滑动或按下按钮时,顶部的视图移动到侧面以显示底部视图。当有人打开一个新的视图并想用两个视图返回到视图控制器时,我希望顶部的视图自动显示底部视图。 这是我的代码:iOS切换视图功能

@interface ViewController() 

@end 
@implementation ViewController 



@synthesize topLayer = _topLayer; 
@synthesize layerPosition = _layerPosition; 

- (void)viewDidLoad 
{ 
[super viewDidLoad] 

self.topLayer.layer.shadowOffset = CGSizeMake(-1,0); 
self.topLayer.layer.shadowOpacity = .9; 

self.layerPosition = self.topLayer.frame.origin.x; 
} 

#define VIEW_HIDDEN 264 

-(void) animateLayerToPoint:(CGFloat)x 
{ 
[UIView animateWithDuration:0.3 
         delay:0 
        options:UIViewAnimationCurveEaseOut 
       animations:^{ 
        CGRect frame = self.topLayer.frame; 
        frame.origin.x = x; 
        self.topLayer.frame = frame; 
       } 
       completion:^(BOOL finished){ 
        self.layerPosition =self.topLayer.frame.origin.x; 

       }]; 
} 

- (IBAction)toggleLayer:(id)sender { 


    if (self.layerPosition == VIEW_HIDDEN) { 
     [self animateLayerToPoint:0]; 
    } else { 
     [self animateLayerToPoint:VIEW_HIDDEN]; 
    } 



} 

- (IBAction)panLayer:(UIPanGestureRecognizer*)pan { 
if (pan.state == UIGestureRecognizerStateChanged) { 
    CGPoint point = [pan translationInView:self.topLayer]; 
    CGRect frame = self.topLayer.frame; 
    frame.origin.x = self.layerPosition + point.x; 
    if (frame.origin.x < 0) frame.origin.x = 0; 
    self.topLayer.frame = frame; 
} 
if (pan.state == UIGestureRecognizerStateEnded) { 
    if (self.topLayer.frame.origin.x <= 160) { 
     [self animateLayerToPoint:0]; 
    } else { 
     [self animateLayerToPoint: VIEW_HIDDEN]; 
    } 
} 
} 

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


@end 
+1

问题是什么? – allprog

+0

我需要知道,如何将视图加载到顶视图移到一边,以便在有人按下另一个视图上的后退按钮返回到此视图时显示第二个视图。 –

+1

如果你正在使用应该已经发生的导航控制器 – micantox

回答

0

覆盖-viewWillAppear,但是你想他们基于应用程序的状态来安排的意见。如果用户没有刷过或者其他东西,把第一个视图放在最上面。如果他们已经滑过或者其他什么,将视图移到一边。

-viewDidLoad是执行视图第一次加载时需要发生的任何设置的正确位置。 -viewWillAppear是在视图控制器视图显示之前应该发生的事情的地方,每次发生时都会调用它。

+0

你能告诉我该写什么吗?谢谢。 –

+0

我以为我只是做了。你似乎知道如何移动顶视图。据推测,你也知道用户何时执行了任何需要移动视图的操作。似乎唯一剩下的就是让视图控制器记住用户是否已经移动了视图,以便下次显示视图时再次移动视图。你可能只需要一个'BOOL'实例变量或属性。 – Caleb

+0

我懂了,谢谢。虽然我在应用程序打开时遇到问题。我不希望顶级视图在第一次打开时移动到一侧 –