【问题标题】:zooming animation problem in ScrollView - IpadScrollView 中的缩放动画问题 - Ipad
【发布时间】:2011-08-05 20:11:49
【问题描述】:

在我的应用程序中,我有一个分屏,其中详细视图是滚动视图。我有 5 个表,它们是我的滚动视图的子视图,其中 3 个表视图在顶部并排,2 个表视图在底部并排

我已经实现了一种方法,当我单击滚动视图中任何表格的任何行时,该视图消失并且另一个视图放大到它的位置。

我在中间表格子视图的didSelectRowAtIndexPath中写了如下代码,

CGFloat xpos = self.view.frame.origin.x;
    CGFloat ypos = self.view.frame.origin.y;
    self.view.frame = CGRectMake(xpos+100,ypos+150,5,5);
    [UIView beginAnimations:@"Zoom" context:NULL];
    [UIView setAnimationDuration:2];
    self.view.frame = CGRectMake(xpos,ypos,220,310);
    [UIView commitAnimations];
    [self.view addSubview:popContents.view];

popContents 是我需要放大到该特定表视图先前占用的视图的视图,并且该视图正确发生。

但是我面临的问题是,由于侧面有另一个表格子视图,如果我将帧大小增加到 250 左右,则放大视图的部分会被侧面的表格视图隐藏(就好像放大视图的一部分位于侧面的表格视图下方)。

有没有办法纠正这个问题,这样我放大的视图就不会被它两侧的 tableviews 隐藏?

希望我已经正确解释了我的问题...

更新:

这是我用来为滚动视图添加子视图的代码

// Scroll view
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, 1000, 740)];
scrollView.contentSize = CGSizeMake(1000, 700);
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
[self.view addSubview:scrollView];


aView = [[aViewController alloc] initWithNibName:@"aViewController" bundle:nil];
aView.view.frame = CGRectMake(10, 25, 220, 310);
[aView loadList:objPatients];
[scrollView addSubview:aView.view];

bView = [[bViewController alloc] initWithNibName:@"bViewController" bundle:nil];
bView.view.frame = CGRectMake(10, 350, 220, 310);
[bView loadList:objPatients];
[scrollView addSubview:bView.view];

cView = [[cViewController alloc] initWithNibName:@"cViewController" bundle:nil];
cView.view.frame = CGRectMake(240, 25, 220, 310);
[cView loadList:objPatients];
[scrollView addSubview:cView.view];

dView = [[dViewController alloc] initWithNibName:@"dViewController" bundle:nil];
enView.view.frame = CGRectMake(240, 350, 220, 310);
[enView loadList:objPatients];
[scrollView addSubview:dView.view];

eView = [[eViewController alloc] initWithNibName:@"eViewController" bundle:nil];
eView.view.frame = CGRectMake(470, 25, 220, 310);
[eView loadList:objPatients];
[scrollView addSubview:eView.view];

比如说,我在 cViewController 子视图中添加了 didSelectRowAtIndexPath 的代码...

【问题讨论】:

    标签: ipad uiscrollview core-animation zooming uisplitviewcontroller


    【解决方案1】:

    这是一个猜测,因为我需要知道您的表格视图是如何添加到滚动视图中的,但中间的表格视图可能是在侧面的之前添加的。视图按照添加的顺序“堆叠”,最后一个在顶部。您需要使用此方法获取滚动视图以将中间视图移到前面

    - (void)bringSubviewToFront:(UIView *)view
    

    最好的方法是为表格视图创建一个协议并让滚动视图成为委托。方法是这样的

    - (void) moveAViewToFront: (MyTableView *) aTableView
    {
        [self.view bringSubviewToFront: aTableView.view];
    }
    

    然后您将在设置动画之前调用委托方法。

    已编辑

    经过深思熟虑后,我意识到子视图引用了它们的父视图,因此这段代码应该提供有关如何解决问题的想法。我创建了一个测试应用程序,它有一个添加两个子视图的视图控制器。视图控制器头文件是 MoveSubviewViewController.h

    #import <UIKit/UIKit.h>
    
    @interface MoveSubviewViewController : UIViewController
    {
    }
    
    @end
    

    它的实现是

    #import "MoveSubviewViewController.h"
    #import "MoveableSubview.h"
    
    @implementation MoveSubviewViewController
    
        - (void)viewDidLoad
        {
            [super viewDidLoad];
    
            // Create two overlapping subviews. The blue subview will start at the top of
            //  the frame and extend down two thirds of the frame.
            CGRect superviewFrame = self.view.frame;
            CGRect view1Frame = CGRectMake( superviewFrame.origin.x, superviewFrame.origin.y,
                                           superviewFrame.size.width, superviewFrame.size.height * 2 / 3);
            MoveableSubview *view1 = [[MoveableSubview alloc] initWithFrame: view1Frame];
            view1.backgroundColor = [UIColor blueColor];
            [self.view addSubview: view1];
            [view1 release];
    
            // The green subview will start one third of the way down the frame and
            //  extend all the to the bottom.
            CGRect view2Frame = CGRectMake( superviewFrame.origin.x,
                                           superviewFrame.origin.y + superviewFrame.size.height / 3,
                                           superviewFrame.size.width, superviewFrame.size.height * 2 / 3);
            MoveableSubview *view2 = [[MoveableSubview alloc] initWithFrame: view2Frame];
            view2.backgroundColor = [UIColor greenColor];
            [self.view addSubview: view2];
            [view2 release];
        }
    
        @end
    

    子视图类是MoveableSubview,带有另一个简单的标题

    #import <UIKit/UIKit.h>
    
    @interface MoveableSubview : UIView
    {
    }
    @end
    

    和实施

    #import "MoveableSubview.h"
    
    @implementation MoveableSubview
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        // Move this view to the front in the superview.
        [self.superview bringSubviewToFront: self];
    }
    
    @end
    

    要做的是添加

    [self.superview bringSubviewToFront: self];
    

    设置动画之前的行。

    【讨论】:

    • 感谢您指出..这正是正在发生的事情..我已经编辑了我的问题以包含代码..所以我在 cViewController 中包含了 didSelectRowAtIndexPath 的代码,其中 dViewController 和 eViewController在 cViewController 之后添加,因此它们隐藏了缩放视图。我想知道我应该在哪里添加您提到的代码以及如何调用它?应该写在cViewController还是分屏视图的detailViewController?
    • 其实比我原先想象的要容易。我忘记了子视图会自动引用它的超级视图,因此您不需要使用协议和委托。我已经编辑了我的原始答案,以提供一些示例代码,应该可以为您提供解决方案。
    • 再次感谢您努力解决我的问题..我是一个基于窗口的项目,因此我无法使用 self.superview.. 在我的情况下还有其他方法可以做到这一点吗?
    • 试一试,因为在您的情况下,滚动视图是超级视图,而 [a-d]ViewController 是子视图。
    • 我想我在回答中看到了错误: self 指的是视图控制器,而 self.view 指的是它的视图。试试 [self.view.superview bringSubviewToFront: self];。很抱歉弄乱了这个。我在测试中使用了视图,而不是视图控制器。
    猜你喜欢
    • 2019-05-24
    • 2015-02-21
    • 2011-05-19
    • 1970-01-01
    • 2011-12-09
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多