UIview动画将一个视图覆盖到另一个视图

时间:2012-07-19 13:15:52

标签: ios

我是新的iphone开发者我有ipad横向模式的问题uiview动画。我有两个视图view1(像分割视图)和view2(覆盖剩余窗口)。当我触摸从右到左移动view2它将覆盖view1与移动animation.at同时当我触摸从左到右移动view2来适应旧位置。请分享你的想法

关心, 拉杰什。

2 个答案:

答案 0 :(得分:3)

大家,我终于找到了答案,

- (void)viewDidLoad
{

    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [recognizer setNumberOfTouchesRequired:1];
    [secondView addGestureRecognizer:recognizer];
    [recognizer release];


    UISwipeGestureRecognizer *recognizerleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
    [recognizerleft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [recognizerleft setNumberOfTouchesRequired:1];
    [secondView addGestureRecognizer:recognizerleft];
    [recognizerleft release];
    [self.view addSubview:secondView];
    [super viewDidLoad];


}
//To set the frame position of the view

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
    NSLog(@"rightSwipeHandle");


    [UIView beginAnimations:@"viewanimations" context:nil];
    [UIView setAnimationDuration:0.8];
    [UIView setAnimationDelegate:self];

    secondView.frame=CGRectMake(360, 0, 660, 768);
    [UIView commitAnimations];
}

//To set the frame position of the view
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
    NSLog(@"leftSwipeHandle");

    [UIView beginAnimations:@"viewanimations" context:nil];
    [UIView setAnimationDuration:0.8];
    [UIView setAnimationDelegate:self];

    secondView.frame=CGRectMake(200, 0, 858, 768);
    [UIView commitAnimations];

}

答案 1 :(得分:1)

如果我理解正确,你想在2个视图之间进行滑动动画。如果是这样: 创建一个将保存这两个视图的ViewController。在此视图控制器的.m文件中定义一个包含以下内容的转换方法,并由您的按钮/其他操作触发器调用:

CGFloat windowWidth = self.mainView.frame.size.width;
CGFloat windowHeight = self.mainView.frame.size.height;
CGRect offScreenLeft = CGRectMake(-1*windowWidth, 0.0, windowWidth, windowHeight);
CGRect onScreen = self.mainView.frame;
CGRect offScreenRight =  CGRectMake(windowWidth, 0.0, windowWidth, windowHeight);

if (direction == rightToLeft)
{ 
    rightView.frame = offScreenRight; 
    [self.view addSubview:rightView];
    [UIView animateWithDuration:0.65
                     animations:^{
                         leftView.frame = offScreenLeft;       
                         rightView.frame = onScreen;
                     }
                     completion:^(BOOL finished){
                         [leftView removeFromSuperview];  
                     }];


}else if (direction == leftToRight){
    self.leftViewController.view.frame = offScreenLeft;
    [UIView animateWithDuration:0.65 
                     animations:^{
                         rightView.frame = offScreenRight;
                         leftView.frame = onScreen;
                     }
                     completion:^(BOOL finished){
                         [rightView removeFromSuperview]; 
                     }]; 
    } 
}

通常,请确保将子视图添加到当前屏幕上的视图中,并确保设置适当的边界。检查您的观点是否为零:

if(myView){
    //  not nil. thats good
}else {
    //  nil. this is bad. make sure myView is being initialized properly (if at all)
}

最后,确保子视图上的不透明度和alpha属性都不设置为0(如果您希望它们完全显示,则需要将它们设置为1,对于透明效果,您希望它们在0和1之间)。