iOS切换视图功能

时间:2013-06-25 12:43:32

标签: ios animation uikit

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

@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 个答案:

答案 0 :(得分:0)

根据应用的状态覆盖-viewWillAppear以根据需要排列视图。如果用户没有刷过或者其他什么,请将第一个视图放在顶部。如果他们已经滑过或者其他什么,将视图移到一边。

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