2

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

问候,拉杰什。

4

2 回答 2

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];

}
于 2012-07-20T07:31:45.360 回答
1

如果我理解正确,你想要两个视图之间的滑动动画。如果是这样:创建一个 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)
}

最后,确保子视图上的 opacity 和 alpha 属性都没有设置为 0(如果您希望它完全显示,则需要将它们设置为 1,并且在 0 和 1 之间以获得透明效果)。

于 2012-07-19T21:35:15.943 回答