2

我对iphone编程很陌生。我大约一个月前才开始,只在修补小型教程类型的应用程序,但无论如何,这是我的问题。

我目前有一个可滚动和可缩放的 UIScrollView,它加载一个 UIImageView 子视图,我想在图像视图上添加一些控件(UIButtons)但是当我放大和缩小整个组(按钮和图像)时,一起缩放.

如果我将 UIButtons 添加到我的 UIScrollView 并缩放,则图像会缩放并且按钮会留在原始位置

如果我将 UIButtons 添加到我的 UIImageView 中,它们会正确缩放,但不再是按钮。即他们失去了交互性。

后来生病将许多按钮添加到数组中并添加它们。

我会很感激我能得到的任何帮助

这是我的 mainViewController.m 的一部分:

- (void)viewDidLoad

{

[scrollView2 setBackgroundColor:[UIColor blackColor]];

[scrollView2 setCanCancelContentTouches:NO];

scrollView2.clipsToBounds = YES;    // default is NO, we want to restrict drawing within our scrollview
scrollView2.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scroll2ImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bigNH.jpg"]];
[scrollView2 addSubview:scroll2ImageView];
[scrollView2 setContentSize:CGSizeMake(scroll2ImageView.frame.size.width, scroll2ImageView.frame.size.height)];
scrollView2.minimumZoomScale = .5;
scrollView2.maximumZoomScale = 3;
scrollView2.delegate = self;
[scrollView2 setScrollEnabled:YES];




UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

CGRect newSize = CGRectMake(658, 435, 50, 50); // position in the parent view and set the size of the button
myButton.frame = newSize;
[myButton setImage:[UIImage imageNamed:@"redStop.png"] forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[scroll2ImageView addSubview:myButton];

[redLineArray addObject:myButton];


[scroll2ImageView release];

}

4

3 回答 3

0

制作一个视图以包含所有可缩放的内容(它会出现的所有内容),并将其称为 contentView。现在将您想要的所有内容放在该 contentView、imageView 和所有按钮等中。

在您的 UIScrollView 委托中放置:(如果您还没有设置滚动视图的委托,请务必设置)

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.contentView;
}

只允许缩放一个视图,但是一个视图可以包含其他可以缩放的视图,因为它们是子视图。

还要确保将 scrollView 的 contentSize 值设置为可缩放 contentView 的确切大小。

于 2011-01-16T02:42:47.880 回答
0

这可能无济于事,而且这不是您想要做的。

但是,当我想要与滚动视图关联的按钮时,我会添加一个父视图,将按钮添加到该视图,并将滚动视图添加到该视图。按钮和滚动视图将是兄弟姐妹。这允许用户滚动滚动视图的内容,同时确保按钮始终在视图中。

-isdi-

于 2009-12-24T16:20:29.223 回答
0

默认情况下,UIImageView 禁用了 userInteraction。你改变了这个吗?

另外,将子视图添加到 UIImageView 中我会非常不安,它实际上是为了保存一个图像。最好创建一个普通的 UIView 并将 UIButtons 和 UIImageView 添加到其中。控制顺序非常容易,以便按钮显示在图像顶部。

PS只是为了让您的生活更轻松,而不是:

[scrollView2 setContentSize:CGSizeMake(scroll2ImageView.frame.size.width, scroll2ImageView.frame.size.height)];

您可以使用 :

[scrollView2 setContentSize:scroll2ImageView.frame.size];
于 2010-01-16T20:19:55.950 回答