1

我正在使用 UiNavigation 编写 iPhone TabBar 项目,第一个视图包含 UIScrollView,滚动视图包含视图,每个子视图都包含 imageview、anUiImage 和 UiButton,下面是构建第一个屏幕的功能

//*******************************************************
//*            Build the main screen                    *
//*******************************************************
-(void) buildScreen{
    sing.viewsArray = [[NSMutableArray alloc]init];

    int Vve = 10;
    int Vho = 10;
    int wi = 95;
    int hi = 95;
    int ArrayIndex = 0;
    for (int i = 0; i < 5; i++) {

        for (int j = 0; j < 3; j++) {

            staticView = [[UIView alloc] initWithFrame:CGRectMake(Vve, Vho, 100, 100)];
            [staticView setTag:ArrayIndex];

            staticImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[sing.stdPicArray objectAtIndex:ArrayIndex]]];
            [staticImage setFrame:CGRectMake(0, 0, wi, hi)];
            [staticView addSubview:staticImage];

            viewButton = [UIButton buttonWithType:UIButtonTypeCustom];
            viewButton.frame = CGRectMake(0, 0, wi, hi);
            [viewButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [viewButton setTag:ArrayIndex];
            [sing.buttonsArray addObject:viewButton];

            [staticView addSubview:viewButton];
            [sing.viewsArray addObject:staticView];

            [MainScroll addSubview:[sing.viewsArray objectAtIndex:ArrayIndex]];

            Vve += 100;
            ArrayIndex++;
        }
        Vve = 10;
        Vho += 100;
    }
}

当我单击 UiButton 时,我想向 UIScrollView 添加另一个 UIView 并在将视图添加到滚动视图时创建一个“UIViewAnimationTransitionFlipFromLeft”尝试以下代码

-(void)animateFlip{    
    [UIView transitionWithView:flipViewBack
                      duration:1.0
                       options:UIViewAnimationTransitionFlipFromLeft
                    animations:^{ [self.view addSubview:flipViewBack]; }
                    completion:^(BOOL f){ }];
}

没用刚刚添加了flipViewBack

-(void)animateFlip{     
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft        
                           forView:self.flipViewFront 
                             cache:YES];
    [MainScroll addSubview:flipViewFront];
    [UIView setAnimationDidStopSelector:@selector(flipAnimationDidStop:finished:context:)];
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
}

不像另一个那样工作,下一个代码确实用我想要的视图翻转了主视图,但它并没有翻转 我想要的视图

-(void)animateFlip{
    [UIView beginAnimations:nil 
                    context:nil];
    [UIView setAnimationDuration:1.0];

    [UIView transitionFromView:self.view 
                        toView:flipViewBack 
                      duration:2 
                       options:UIViewAnimationOptionTransitionFlipFromBottom 
                    completion:NULL];
    [UIView setAnimationDidStopSelector:@selector(flipAnimationDidStop:finished:context:)];
    [UIView setAnimationDelegate:self];

    //set transformation

    [UIView commitAnimations];

}

有没有人知道怎么做?我到处找,我发现的所有代码都对我不起作用

谢谢

我尝试了此链接中建议的另一个选项 iPhone 子视图在 2 个视图之间翻转,但它对我不起作用

这是我在最后 3 行中单击时使用的函数,我将 flipViewFront 视图添加到 FlipView(容器)中,然后我将 FlipView 添加到 mainScroll 视图,然后我去执行我从您复制的 animateFlip 中的动画样本

    //*******************************************************
//*            Click on the picture as button           *
//*******************************************************
-(void) buttonClick:(id) sender{
    UIButton *button = (UIButton *)sender;
    int row = [button superview].tag;
    NSLog(@"Button pressed tag: %i",row);
    self.flipView = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
    self.flipView.backgroundColor = [UIColor clearColor];

    self.flipViewBack = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];

    self.flipViewFront = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
    self.flipViewFront.backgroundColor = [UIColor whiteColor];

    self.flipViewImage = [UIImage imageNamed:[sing.stdPicArray objectAtIndex:row]];
    self.filpImage = [[UIImageView alloc]initWithImage:self.flipViewImage];
    [self.flipViewBack addSubview:self.filpImage];

    self.flipCloseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.flipCloseButton setFrame:CGRectMake(0, 0, 24, 24)];
    [self.flipCloseButton setImage:[UIImage imageNamed:@"close_button.png"] forState:UIControlStateNormal];
    [self.flipCloseButton addTarget:self action:@selector(closwFlip:) forControlEvents:UIControlEventTouchUpInside];
    [self.flipViewBack addSubview:flipCloseButton];

    self.flipOkButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 0, 190, 190)];
    [self.flipCloseButton addTarget:self action:@selector(continueTo:) forControlEvents:UIControlEventTouchUpInside];
    [self.flipViewBack addSubview:flipOkButton];
    [flipView addSubview:flipViewFront];
    [MainScroll addSubview:flipView];
    [self animateFlip];
}

找到了问题,但不是答案,如果它直接从 ViewDidLoad 工作并且第一个视图在屏幕上,一切都很好,并且翻转工作'如果我更改它以便想要添加一个视图并在一个事件中翻转它它向我展示了没有翻转的第二个(翻转)视图

4

1 回答 1

1

您的问题似乎是您试图在将视图添加到视图层次结构之前对视图进行动画处理。

相反,您应该将后视图添加到公共容器视图(稍后将包含前视图),然后在添加前视图和删除后视图的同时翻转容器视图,如下所示:

// Container view is a view that is already added to your view hierarchy.
// It contains the back view. The front view will be added to it.
[UIView transitionWithView:containerView
                  duration:1.0
                   options:UIViewAnimationOptionTransitionFlipFromLeft
                animations:^{
                    [backView removeFromSuperview]; // This is already added to the container
                    [containerView addSubview:frontView]; // Not yet added ...
                }
                completion:NULL];

更新

这是我在回答这个问题时写的所有代码。这个对我有用。如果它仍然没有为您设置动画,那么您可能没有以相同的方式设置您的视图层次结构。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    containerView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
    [self.view addSubview:containerView];

    frontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [frontView setBackgroundColor:[UIColor orangeColor]];
    backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [backView setBackgroundColor:[UIColor redColor]];
    [containerView addSubview:backView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(flip)];
    [self.view addGestureRecognizer:tap];
}

- (void)flip {
    [UIView transitionWithView:containerView
                      duration:1.0
                       options:UIViewAnimationOptionTransitionFlipFromLeft
                    animations:^{
                        [backView removeFromSuperview];
                        [containerView addSubview:frontView];
                    }
                    completion:NULL];
}
于 2012-08-02T13:50:00.440 回答