代码之家  ›  专栏  ›  技术社区  ›  coneybeare

证明iPhone上的uiview的合理性:算法帮助

  •  0
  • coneybeare  · 技术社区  · 15 年前

    我一直在想一种方法来证明在包含视图中对齐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
    
    2 回复  |  直到 14 年前
        1
  •  0
  •   coneybeare    15 年前

    我能让它自己工作!我对元素应用了错误的边距。问题是我需要在考虑前面的元素来源和宽度的同时应用边界。

    @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
  •  0
  •   Jeff Mascia    14 年前

    康尼博瑞,谢谢你的理解,但是你的解决方案并没有如预期的那样有效。它更改了选项卡在栏上的位置,但间距不正确。这对我来说似乎更好:

    #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。

    推荐文章