如何将两个uiscrollviews添加为子视图

时间:2011-09-12 16:39:14

标签: iphone objective-c uiview uiscrollview uiimageview

经过大量研究和工作几天后,我真的需要一些指导。我正在尝试将uiscrollview添加到彼此之上。我有两个UIScrollViews:ImageScrollView和MarkScrollView。我想将MarkScrollView作为子视图添加到ImageScrollView。 ImageScrollView被添加到UIViewController MapImageViewController中。 在ImageScrollView中,它由tilesLayer中的图像组成。在MarkScrollView中,它由具有指针图像的imageview组成。但由于某种原因,它不能作为子视图添加到其他视图。我添加了我的代码。如果有人能帮助我找出错误,那将非常感激。

/ ** ImageScrollView.m它在ImageScrollView ***上添加了uiview /

UIView *imageView = [[TileView alloc] initWithImageName:imageName size:imageSize];
[(TileView *)imageView setAnnotates:NO]; // ** remove this line to remove the white tile grid **
[self addSubview:imageView];

/ ** MarkScrollView.m它将uiview添加到MarkScrollView **之上的uiimages ** /

UIImage *markerimage = [UIImage imageNamed:@"map.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: markerimage];
imageView.image = markerimage;
[imageView setFrame:CGRectMake(100,100,50,50)];
imageview = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
[imageview setBackgroundColor:[UIColor blackColor]];
[imageview addSubview: imageView];
[self addSubview:imageview];

/ ** MapImageViewController.m它将ImageScrollView添加到另一个UIScrollView pagingScrollView ** /

之上
 UIScrollView *pagingScrollView;
 ImageScrollView *page = [[[ImageScrollView alloc] init] autorelease];
 [pagingScrollView addSubview:page];

  MarkScrollView *markerScroll = [[[MarkScrollView alloc] init] autorelease];
  [page addSubview:markerScroll]; /**But it never adds the other scrollview here **/

/ ** MapImageViewController.h ** /

@interface MapImageViewController : UIViewController <UIScrollViewDelegate, UIGestureRecognizerDelegate> {

ImageScrollingView *imageScrollView;
MarkerIconsScrollView *markerScrollView;
}

1 个答案:

答案 0 :(得分:0)

UIScrollView *pagingScrollView;
 ImageScrollView *page = [[[ImageScrollView alloc] init] autorelease];
 [pagingScrollView addSubview:page];

  MarkScrollView *markerScroll = [[[MarkScrollView alloc] init] autorelease];
  [page addSubview:markerScroll]; /**But it never adds the other scrollview here **/

我注意到此代码段中的一些内容:

  1. pagingScrollView永远不会初始化
  2. 您要添加的视图(假设它实际上是使用有效帧初始化的),不会使用框架初始化。始终使用 - (id)initWithFrame:(CGRect)aRect来初始化你的UIViews
  3. 祝你好运!