【问题标题】:Flip SubView on UIScrollView not workingUIScrollView 上的翻转子视图不起作用
【发布时间】:2012-08-02 13:23:39
【问题描述】:

我正在使用 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 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];
}

找到了问题,但不是答案在一个事件中,它向我展示了没有翻转的第二个(翻转)视图

【问题讨论】:

    标签: iphone ios5 uiview uiscrollview uiviewanimation


    【解决方案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];
    }
    

    【讨论】:

    • 我刚刚尝试在动画块中像您的示例和旧代码格式一样添加到我的问题中'仍然无法工作它会更改视图但不会翻转,我尝试更改不透明度和它工作
    • @ShimonWiener 该代码对我有用。你按照我说的设置了吗?包含 backView 的 containerView。 containerView 添加到您的超级视图中
    • 这是我在最后 3 行中单击时使用的功能,我将其添加到 FlipView(容器)中,然后我将 FlipView 添加到 mainScroll 视图,然后我去执行动画在我从您的示例中复制的 animateFlip 中,我将函数添加到问题的末尾
    • 我试过你的代码,它确实有效,我的层次结构很复杂,但我认为按顺序构建,正如你从检查我在问题中提供的代码中看到的那样'谢谢你的时间并提供
    • 找到了问题,但不是答案,如果它直接从 ViewDidLoad 工作并且第一个视图在屏幕上一切都很好,并且翻转工作'如果我改变它以便想要添加一个视图并在一个事件中翻转它,它向我显示了没有翻转的第二个(翻转)视图
    猜你喜欢
    • 2013-07-17
    • 2019-08-02
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    相关资源
    最近更新 更多