我在这方面还是个新手,所以请原谅我。我认为我可以在ScrollView中加载Xib,我看到的应用似乎是这样做的,但我们讨论的是两个不同的类。但我还是会问-是否有任何实际的方法有一个scrollView与静态Xib在顶部,其中按钮定义在UI不移动,而下面的视图做。我相信这在cocos2d中很容易实现,但对于我想要做的事情来说,这有点过头了。
—Edit—
冒着让自己尴尬的风险,我尝试了两种可能的解决方案。在语法上添加按钮会添加一个在滚动时移动的按钮。添加笔尖似乎可以防止滚动屏幕滚动。下面是代码,没有尝试添加任何按钮,一切都很好。- (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];
}
我还做错了什么吗?在看了更多之后,这似乎是可行的。
你应该像这样设置层次结构。
ui- 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];
}
诀窍是在加载XIB时指定所有者。例如:
-
在UIViewController上定义一个IBOutlet
@property (nonatomic, strong) 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整齐地安排事情也是可能的。