UIView可怕的表现

时间:2013-07-08 17:39:13

标签: ios optimization uiview autolayout

我已经构建了一个幻灯片导航控制器,可以快速平滑地(Facebook风格)滑动UIViewController。我已经添加了一个带有子视图控制器的UIViewController,它带有它自己的子视图控制器,以及一个表视图。最低的子视图控制器还具有视图子视图。这个UIViewController的视图在顶视图滑开时可见。

我已经使用和不使用此视图控制器测试了平滑度。没有它,幻灯片以每秒60帧的速度运行,但有了它,我的设备只能处理20到30 fps的波动。

我也尝试删除似乎略有改进的子视图,但没有我想象的那么多。即使用另一个相当复杂的视图控制器切换(虽然没有子视图控件),我的性能会更好。

我在整个应用程序中使用自动布局,并且开始相信这是问题所在。在发生这种不良滑动时,我的代码都没有被执行,因此我认为它与计算的约束有关,因为更多的视图被显示,但无法确定。

为了避免包含整个类,这里​​是视图控制器和子视图中的updateViewConstraint方法:

TOP VIEW CONTROLLER

/**
 *  Called when the view controller’s view needs to update its constraints.
 */
- (void)updateViewConstraints
{
    [super updateViewConstraints];

    //  remove all constraints
    [self.view removeConstraints:self.view.constraints];

    NSArray *constraints;

    //  add the table view to the top of the view and the parameters view to the bottom
    constraints                         = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(Panel)-[tableView]|"
                                                                options:kNilOptions
                                                                metrics:@{@"Panel": @(kPanelWidth)}
                                                                  views:self.viewsDictionary];

    [self.view addConstraints:constraints];

    constraints                         = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView]-|"
                                                                options:kNilOptions
                                                                metrics:nil
                                                                  views:self.viewsDictionary];
        [self.view addConstraints:constraints];

    constraints                         = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(PanelPlus)-[recipeParameters]-|"
                                                                options:kNilOptions
                                                                metrics:@{@"PanelPlus": @(kPanelWidth + 20)}
                                                                  views:self.viewsDictionary];
    [self.view addConstraints:constraints];

    constraints                         = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[recipeParameters(==Height)]|"
                                                                options:kNilOptions
                                                                metrics:@{  @"Height"   : @(kParametersControllerHeight)}
                                                                  views:self.viewsDictionary];
    [self.view addConstraints:constraints];
}

第一个儿童视图控制器

/**
 *  Called when the view controller’s view needs to update its constraints.
 */
- (void)updateViewConstraints
{
    [super updateViewConstraints];

    //  remove all constraints
    [self.view removeConstraints:self.view.constraints];

    NSArray *constraints                = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[pageView]|" options:kNilOptions
                                                                      metrics:nil views:self.viewsDictionary];
    [self.view addConstraints:constraints];

    constraints                         = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[pageView]|" options:kNilOptions
                                                                metrics:nil views:self.viewsDictionary];
    [self.view addConstraints:constraints];
}

第二个儿童视图控制器

- (void)updateViewConstraints
{
    [super updateViewConstraints];

    NSArray *constraints;

    //  remove all constraints
    [self.view removeConstraints:self.view.constraints];

    if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        constraints                         = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(8)-[optionLabel]-[selectionWheel]-[includeExclude(==32)]-|"
                                                                    options:NSLayoutFormatAlignAllCenterX
                                                                    metrics:nil
                                                                      views:self.viewsDictionary];
        [self.view addConstraints:constraints];

        constraints                         = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[selectionWheel]-|"
                                                                    options:kNilOptions
                                                                    metrics:nil
                                                                      views:self.viewsDictionary];
        [self.view addConstraints:constraints];

        constraints                         = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[includeExclude]-|"
                                                                    options:kNilOptions
                                                                    metrics:nil
                                                                      views:self.viewsDictionary];
        [self.view addConstraints:constraints];
    }
    else
    {
        constraints                         = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[selectionWheel]-|" options:kNilOptions
                                                                    metrics:nil views:self.viewsDictionary];
        [self.view addConstraints:constraints];

        constraints                         = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[optionLabel]-[selectionWheel]-(Balance)-|"
                                                                    options:NSLayoutFormatAlignAllCenterY
                                                                    metrics:@{@"Balance": @(self.optionLabel.bounds.size.width + 40)}
                                                                      views:self.viewsDictionary];
        [self.view addConstraints:constraints];
    }
}

CUSTOM UICONTROL(第二个儿童视图控制器的视图)

/**
 *  Update constraints for the view.
 */
- (void)updateConstraints
{
    [super updateConstraints];

    [self removeConstraints:self.constraints];
    [self.excludeButton removeConstraints:self.excludeButton.constraints];
    [self.includeButton removeConstraints:self.includeButton.constraints];

    NSArray *constraints;
    NSLayoutConstraint *constraint;

    //  add everything to fill the bounds
    constraints                         = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(==6)-[excludeButton]-(==6)-|"
                                                                options:kNilOptions
                                                                metrics:nil
                                                                  views:self.viewsDictionary];
    [self addConstraints:constraints];

    constraints                         = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[excludeButton][optionLabel][includeButton]|"
                                                                options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom
                                                                metrics:nil
                                                                  views:self.viewsDictionary];
    [self addConstraints:constraints];


    //  make the exclude and include buttons square
    constraint                          = [NSLayoutConstraint constraintWithItem:self.excludeButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.excludeButton attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f];
    [self.excludeButton addConstraint:constraint];
    constraint                          = [NSLayoutConstraint constraintWithItem:self.includeButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.includeButton attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f];
    [self.includeButton addConstraint:constraint];
}

The implementation of the top UIViewController is here.

0 个答案:

没有答案