Xibs和Scrollview

时间:2013-04-25 22:27:45

标签: ios

我还是很陌生,所以请耐心等待。我以为我可以在ScrollView中加载一个Xib,因为我看过似乎这样做的应用程序,但我们正在谈论两个不同的类。但是我还是会问 - 有没有任何实用的方法可以在顶部使用带有静态Xib的scrollView,其中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];
}

还有其他我想做错的事吗?看了这个之后,看起来确实可行。

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分: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. 

自定义滚动视图子类

如果你想要的话,应该可以通过创建一个cusom滚动视图子类并在那里指定一个插座来使用相同的方法。 。 。然后将UIView直接加载到子类上。 。 。 (你仍然需要将它添加为子视图)。

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