【问题标题】:Justifying UIVIews on the iPhone: Algorithm Help在 iPhone 上证明 UIVIews:算法帮助
【发布时间】:2011-01-08 19:44:06
【问题描述】:

我一直在寻找一种方法来证明在包含视图中对齐 UIView 子类的集合。我的算法有点麻烦,希望有人能帮助发现我的错误。这是我现在所在位置的伪代码:

// 1 see how many items there are
int count = [items count];

// 2 figure out how much white space is left in the containing view
float whitespace = [containingView width] - [items totalWidth];

// 3 Figure out the extra left margin to be applied to items[1] through items[count-1]
float margin = whitespace/(count-1);

// 4 Figure out the size of every subcontainer if it was evenly split
float subcontainerWidth = [containingView width]/count;

// 5 Apply the margin, starting at the second item
for (int i = 1; i < [items count]; i++) {
    UIView *item = [items objectAtIndex:i];
    [item setLeftMargin:(margin + i*subcontainerWidth)];
}

这里的项目似乎没有均匀分布。差远了。我哪里错了?

以下是该算法的实际效果: alt text http://grab.by/1Wcg

编辑:上面的代码是伪代码。我在此处添加了实际代码,但如果您不熟悉three20 项目,则可能没有意义。

@implementation TTTabStrip (JustifiedBarCategory)
- (CGSize)layoutTabs {
    CGSize size = [super layoutTabs];

    CGPoint contentOffset = _scrollView.contentOffset;
    _scrollView.frame = self.bounds;
    _scrollView.contentSize = CGSizeMake(size.width + kTabMargin, self.height);

    CGFloat contentWidth = size.width + kTabMargin;
    if (contentWidth < _scrollView.size.width) {
        // do the justify logic

        // see how many items there are
        int count = [_tabViews count];

        // 2 figure out how much white space is left
        float whitespace = _scrollView.size.width - contentWidth;

        // 3 increase the margin on those items somehow to reflect.  it should be (whitespace) / count-1
        float margin = whitespace/(count-1);

        // 4 figure out starting point
        float itemWidth = (_scrollView.size.width-kTabMargin)/count;
        // apply the margin
        for (int i = 1; i < [_tabViews count]; i++) {
            TTTab *tab = [_tabViews objectAtIndex:i];
            [tab setLeft:(margin + i*itemWidth)];
        }

    } else {
        // do the normal, scrollbar logic
        _scrollView.contentOffset = contentOffset;
    }
    return size;
}
@end

【问题讨论】:

  • 嗨。由于您的所有值(margin 和 subcontainerWidth)都是在循环之前计算的,并且不会随着循环的运行而改变,因此它会将 setLeftMargin 调用视为可能的罪魁祸首。 setLeftMargin 是如何工作的? (我找不到任何文档!)
  • containerView和containingView有什么区别?看起来 item 是一个自定义视图,因为 setLeftMargin 不在 Apple 的文档中。你能再贴一些代码,尤其是那个方法吗?
  • containingView 和 containerView 是一样的。这是一个错字。请记住,这只是伪代码。我将发布实际代码,但这可能没有意义,因为我使用的是three20。
  • 这些不是实际调用。 set left margin 伪代码方法设置与前一个元素左边缘的差异。我正在使用three20框架

标签: iphone objective-c algorithm justify


【解决方案1】:

我能够自己让它工作!我对元素应用了错误的边距。问题是我需要在考虑之前元素的原点和宽度时应用边距。

@implementation TTTabStrip (JustifiedBarCategory)
- (CGSize)layoutTabs {
    CGSize size = [super layoutTabs];

    CGPoint contentOffset = _scrollView.contentOffset;
    _scrollView.frame = self.bounds;
    _scrollView.contentSize = CGSizeMake(size.width + kTabMargin, self.height);

    CGFloat contentWidth = size.width + kTabMargin;
    if (contentWidth < _scrollView.size.width) {
        // do the justify logic

        // see how many items there are
        int count = [_tabViews count];

        // 2 figure out how much white space is left
        float whitespace = _scrollView.size.width - contentWidth;

        // 3 increase the margin on those items somehow to reflect.  it should be (whitespace) / count-1
        float margin = whitespace/(count-1);

        // apply the margin
        for (int i = 1; i < [_tabViews count]; i++) {
            // 4 figure out width from the left edge to the right of the 1st element
            float start = [[_tabViews objectAtIndex:i-1] frame].origin.x + [[_tabViews objectAtIndex:i-1] frame].size.width;

            TTTab *tab = [_tabViews objectAtIndex:i];
            [tab setLeft:(start + margin)];
        }
    } else {
        // do the normal, scrollbar logic
        _scrollView.contentOffset = contentOffset;
    }
    return size;
}
@end

【讨论】:

    【解决方案2】:

    coneybeare,感谢您解决这个问题,但您的解决方案并没有按预期工作。它改变了栏上标签的位置,但间距不正确。这似乎对我更有效:

    #import "TTTabStrip+Justify.h"
    #import <Three20UI/UIViewAdditions.h>
    
    // Width returned by [super layoutTabs] is always 10 px more than sum of tab widths
    static CGFloat const kContentWidthPadding = 10;
    // Adds fixed margin to left of 1st tab, right of last tab
    static CGFloat const kHorizontalMargin = 5;
    
    @implementation TTTabStrip (JustifyCategory)
    
    - (CGSize)layoutTabs {
        CGSize size = [(TTTabStrip*)super layoutTabs];
    
        CGPoint contentOffset = _scrollView.contentOffset;
        _scrollView.frame = self.bounds;
        _scrollView.contentSize = CGSizeMake(size.width, self.height);
    
        CGFloat contentWidth = size.width - kContentWidthPadding + 2 * kHorizontalMargin;
        if (contentWidth < _scrollView.size.width) {
            // do the justify logic
    
            // see how many items there are
            int count = [_tabViews count];
    
            // calculate remaining white space
            float whitespace = _scrollView.size.width - contentWidth;
    
            // calculate necessary spacing between tabs
            float spacing = whitespace / (count + 1);       
    
            // apply the spacing
            for (int i = 0; i < count; i++) {
                CGFloat lastTabRight = kHorizontalMargin;
    
                if (i > 0) {
                    TTTab *lastTab = [_tabViews objectAtIndex:i-1];
                    lastTabRight = [lastTab right];
                }
    
                TTTab *tab = [_tabViews objectAtIndex:i];
                [tab setLeft:(lastTabRight + spacing)];
            }
    
        } else {
            // do the normal, scrollbar logic
            _scrollView.contentOffset = contentOffset;
        }
        return size;
    }
    
    @end
    

    Morgz,我也遇到了几个编译器错误。我需要导入 UIViewAdditions.h 并告诉它 super 是一个 TTTabStrip。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      • 2011-03-17
      相关资源
      最近更新 更多