本文介绍了UIImageView上的UIButtons缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对iPhone编程很陌生.我才刚开始一个月左右,并且只在研究小型教程类型的应用程序,但是无论如何,这是我的问题.

I am pretty new to iphone programming. I just started about a month ago and have only been tinkering with small tutorial type applications but anyways, here is my question.

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

I currently have a UIScrollView thats scrollable and zoomable, that loads a UIImageView subview, and i want to add in some controls(UIButtons) over the image view but when i zoom in and out the whole group(the buttons and the image) zoom togeather.

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

If I add the UIButtons to my UIScrollView and zoom, the image zooms and the buttons stay in origional place

如果我将UIButton添加到我的UIImageView中,它们将正确缩放,但不再有按钮.IE他们失去了互动性.

If I add the UIButtons to my UIImageView they zoom correctly but arent buttons anymore. IE they lose their interactivity.

稍后将大量按钮添加到数组中并将其添加.

Later ill add lots of buttons into an array and add them.

我将不胜感激

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

This is part of my mainViewController.m:

- (void)viewDidLoad

{

[scrollView2 setBackgroundColor:[UIColor blackColor]];

[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];

}

推荐答案

制作一个视图以包含所有可缩放内容(所有内容都会出现),并称其为contentView.现在,将所需的所有内容放入该contentView,imageView和所有按钮等中.

Make a single view to contain all your zoomable content (everything it would appear), and call it say contentView. Now place everything you want in that contentView, the imageView and all your buttons etc.

在您的UIScrollView委托中放置以下内容:(如果尚未设置,请确保设置scrollView的委托)

In your UIScrollView delegate place this: (Be sure to set the scrollView's delegate if you haven't already)

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

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

Only one view is allowed to zoom, but that one view can however contain other views that will scale because they're child views.

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

Also be sure to set the scrollView's contentSize value to the exact size of your zoomable contentView.

这篇关于UIImageView上的UIButtons缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:03