在UIScrollView上翻转SubView无法正常工作

时间:2012-08-02 13:23:40

标签: iphone ios5 uiview uiscrollview uiviewanimation

我正在使用UiNavigation编写iPhone TabBar项目,第一个视图包含UIScrollView,scrollview包含视图,每个子视图包含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 subview flip between 2 views它对我不起作用

这是我在最后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工作,第一个视图在屏幕上一切都很好,翻转工作'如果我改变它,以便想要添加一个视图并翻转它在一个事件中,它向我显示没有翻转的第二个(翻转)视图

1 个答案:

答案 0 :(得分: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];
}