【问题标题】:Simplest way to support interactive mockup prototype in iOS?在 iOS 中支持交互式模型原型的最简单方法是什么?
【发布时间】:2011-04-17 14:01:59
【问题描述】:

我有 40 张代表 iOS 应用程序屏幕模型的图像。我会将每一个全屏显示为UIImageView。我想通过每个屏幕上的按钮支持从 1 到 40 的导航。我将在我想要支持此导航的地方添加透明的UIButtons。按钮在每个屏幕上的位置不同。

我唯一的选择是创建 40 个笔尖并在我想要这些按钮的地方手工绘制吗?我在这里错过了一些基类的机会吗?我的图像的命名是可以预见的,所以我计划在基类中使用简单的导航方法。

【问题讨论】:

    标签: ios interface-builder uibutton nib


    【解决方案1】:

    我会做这样的事情:拥有一个 UIImageView 和一个视图控制器。视图控制器有一个 NSArray,比如buttonsOnPage。在数组内部,您将存储更多 NSArrays 存储按钮。这些按钮都指向同一个操作,例如buttonPressed:。请记住,您可以分配tags,您可以使用这些来确定要转到哪个“页面”。由于加载您的图像似乎很简单,我在这里跳过。

    这是一个小伪代码:

    - (void)initButtons
    {
        NSMutableArray *page;
        UIButton *button;
    
        buttonsOnPage = [[NSMutableArray alloc] init];
    
        // Page 0
        page = [NSMutableArray array];
    
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvent:UIEventTouchUpInside];
        [button setTag:5]; // When pressed, go to page 5.
        [button setFrame:...];
        [page addObject:button];
        ...
        [buttonsOnPage addObject:page];
    
        ...
    }
    
    - (void)setUpPage:(NSUInteger)page
    {
        // Remove all previous buttons
        for (UIView *view in [myImageView subviews]) {
            [view removeFromSuperview];
        }
    
        for (UIButton *button in [buttonsOnPage objectAtIndex:page]) {
           [myImageView addSubview:button];
        }
    
        // Also set the correct image.
    }
    
    
    - (void)buttonPressed:(id)sender
    {
        [self setUpPage:[(UIView *)sender tag]];
    }
    

    您可以通过定义宏使您的按钮设置方法更容易/更快地键入:

    #define NEW_BUTTON(page, x, y, width, height) \
        do {
            button = [UIButton buttonWithType:UIButtonTypeCustom]; \
            [button addTarget:self action:@selector(buttonPressed:) forControlEvent:UIEventTouchUpInside];\
            [button setTag:page]; \
            [button setFrame:CGRectMake(x, y, width, height)]; \
            [page addObject:button];
        } while (0);
    

    那么做起来还是挺快的:

    // Page 0
    page = [NSMutableArray array];
    NEW_BUTTON(1, 10, 20, 20, 20);
    NEW_BUTTON(2, 10, 60, 20, 20);
    [buttonsOnPage addObject:page];
    
    // Page 1
    page = [NSMutableArray array];
    NEW_BUTTON(3, 70, 20, 20, 20);
    NEW_BUTTON(4, 10, 160, 20, 20);
    [buttonsOnPage addObject:page];
    
    ...
    

    【讨论】:

    • 但是你假设我知道我的按钮坐标,但我不知道。我仍然认为我必须手动放下按钮。
    • 当然我假设您知道按钮坐标,它们不会在每个页面上的固定位置上吗?如果不是,它们是随机定位的吗?
    • 嗯,每个画面都不一样,都是样机。该按钮在每个屏幕上的不同位置(或者足够多,无论如何)。
    • @pkananen:在每个页面上,您都知道按钮的位置,因为您已经有了模型,不是吗?您可以使用 Gimp 或类似工具测量每个按钮的位置,对吗?
    • 我可能可以,但在这一点上,我认为我并没有节省多少时间。我有一种感觉,这里没有太多可以优化的地方。
    猜你喜欢
    • 1970-01-01
    • 2011-06-09
    • 2013-07-20
    • 2013-04-06
    • 2012-01-25
    • 2011-03-19
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多