2013-04-25 25 views
1

我对这件事还很新,所以请耐心等待。我以为我可以在ScrollView中加载一个Xib,因为我已经看到似乎这样做的应用程序,但我们正在谈论两个不同的类。但我会问 - 是否有任何实际的方法来使用顶部的静态Xib的scrollView,其中UI中定义的按钮不会在底部视图中移动。我确信它在cocos2d中很容易实现,但对于我想要做的事情来说,这有点矫枉过正。Xibs和滚动视图

---编辑---

在自己尴尬的风险,我想这两个可能的解决方案。语法添加按钮添加一个按钮,当我滚动时会移动。添加笔尖似乎保持滚动屏幕不滚动。这里的代码,没有试图添加任何按钮一切正常。

- (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]; 
} 

还有什么我试图做错?看了这些之后,它看起来确实可行。

回答

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]; 
} 
0

关键是要指定所有者装载XIB时。例如:

  • 在UIViewController中定义一个IBOutlet

    @属性(非原子,强)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. 

自定义滚动视图子类

如果你想应该能够通过创建cusom滚动视图子类,并指定使用相同的方法那里的出路。 。 。那么UIView将直接加载到子类上。 。 。 (您仍然必须将其添加为子视图)。

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