0

我有一个应用程序,它在一个视图控制器上有两个视图。一种观点高于另一种观点。当有人滑动或按下按钮时,顶部的视图会移动到侧面以显示底部视图。当有人打开一个新视图并想要返回具有两个视图的视图控制器时,我想让视图在顶部以自动显示底部视图。这是我的代码:

@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
4

1 回答 1

0

覆盖-viewWillAppear以根据应用程序的状态以您想要的方式排列视图。如果用户没有刷卡或其他什么,将第一个视图放在顶部。如果他们滑动过或其他什么,请将视图移到一边。

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

于 2013-06-25T16:13:55.447 回答