1

我对此还是很陌生,所以请耐心等待。我认为我可以在 ScrollView 中加载 Xib,因为我已经看到似乎可以执行此操作的应用程序,但我们正在谈论两个不同的类。但无论如何我会问 - 是否有任何实用的方法可以让滚动视图在顶部带有静态 Xib,其中 UI 中定义的按钮不会移动,而下面的视图会移动。我确信它在 cocos2d 中很容易实现,但是对于我想做的事情来说,这有点矫枉过正。

- - 编辑 - -

冒着让自己尴尬的风险,我尝试了两种可能的解决方案。添加一个按钮在语法上添加了一个在我滚动时移动的按钮。添加笔尖似乎可以防止滚动屏幕滚动。这是代码,无需尝试添加任何按钮,一切正常。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"View Loaded");


    [mdm setMapSetupInfoWithRows:60 columns:90 cellSize:32];
    [mdm initMapDataWithOriginsUsingCenter:TRUE];

    NSLog(@"MapViewContoller.mapArrayCount = %d",[[mdm mapArray]count]);

    // create the MapView with the screen size create by MapDataManager
     mapView = [[MapView alloc] initWithFrame:[mdm mapRect]];

    // Create the UIScrollView to have the size of the window, matching the window (screen) size
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[mdm windowRect]];
    [scrollView setBounds:[mdm windowRect]];

    // Tell the scrollview how big it is and set other options
    [scrollView setContentSize:[mdm mapRect].size];
    [scrollView setBounces:NO];
    [scrollView setMinimumZoomScale:.5];
    [scrollView setMaximumZoomScale:10];
    [scrollView setDelegate:self];
    [scrollView setBackgroundColor:[UIColor darkGrayColor]];


    //add the MapView as a subview of the scrollView
    [scrollView addSubview:mapView];

    //add the scrollView to the current one....
    [[self view] addSubview:scrollView];

    [[NSBundle mainBundle] loadNibNamed:@"MapViewController" owner:self options:nil];


    [self generNewMap];
}

还有什么我想做错的吗?在看了这个之后,它似乎确实可行。

4

2 回答 2

0

诀窍是在加载 XIB 时指定所有者。例如:

  • 在你的 UIViewController 上定义一个 IBOutlet

    @property(非原子,强)IBOutlet UIView* myScrollViewContent

  • 创建一个 XIB,将所有者指定为您的 UIViewController 类

  • 将 XIB 中定义的组件连接到 UIViewController 类中定义的插座

然后在代码中这样做:

//Because the owner is 'self' the bundle loader will inject any properties defined . . 
//. . . . and wired in the XIB to the owner's outlets
[[NSBundle mainBundle] loadNibNamed:@"MyXibName" owner:self options:nil];
//Now do something with self.myScrollViewContent - ie add it to the scroll view. 

自定义滚动视图子类

如果你想应该能够通过创建一个自定义滚动视图子类并在那里指定一个出口来使用相同的方法。. . 然后,UIView 将直接加载到子类中。. . (您仍然必须将其添加为子视图)。

对于更复杂的需求,我个人喜欢用纯代码构建我的视图,但可以使用 XIB 整齐地安排事物。

于 2013-04-25T23:55:01.670 回答
0

您应该使用这样的层次结构进行设置。

UIViewController

  • UIScrollView
  • 静态按钮等

然后在界面生成器或代码中,只需将静态按钮等添加到 self.view。

我在代码中做所有事情,所以它看起来像

-(void)viewDidLoad {
    //add scrollview
    appScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    appScroll.pagingEnabled = YES;
    [appScroll setCanCancelContentTouches:NO];
    appScroll.bounds = CGRectMake(0, 0, 320, 480);
    appScroll.contentSize = CGSizeMake(1600, 480);
    [appScroll setScrollEnabled:YES];
    appScroll.bounces = NO;
    appScroll.showsHorizontalScrollIndicator = NO;
    appScroll.showsVerticalScrollIndicator = NO;
    appScroll.clipsToBounds = YES;
    appScroll.delaysContentTouches = YES;
    appScroll.center = (CGPoint){ 160, 240 };
    [appScroll setBackgroundColor:[UIColor darkGrayColor]];
    [self.view addSubview:appScroll];


    //back
    backBut = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back.png"]];
    backBut.center = (CGPoint){ 40, 430 };
    backBut.transform = CGAffineTransformMakeScale(0.3,0.3);
    [backBut setUserInteractionEnabled: YES];
    [self.view addSubview: backBut];
}
于 2013-04-25T23:06:25.947 回答