UIButtons over a UIImageView Zooming

2019-05-13 19:27发布

hey guys, this is my first time post here, and im pretty new to iphone programming. i just started about a month ago and have only been tinkering with small tutorial type applications but anyways, heres my question

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.

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

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.

i would appreciate any help i can get

this is part of my 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];

}

3条回答
看我几分像从前
2楼-- · 2019-05-13 19:41

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.

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.

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

查看更多
小情绪 Triste *
3楼-- · 2019-05-13 19:45

This might not help and it is not what it appears that you want to do.

However, when I want buttons associated with a scrollview, I add a parent view, add the buttons to that view and also add the scrollview to that view. The button(s) and the scrollview would be siblings. This allows the user to scroll around the contents of the scrollview, while insuring that the buttons are always in view.

-isdi-

查看更多
闹够了就滚
4楼-- · 2019-05-13 19:49

By default, UIImageView has userInteraction disabled. Have you changed this?

Also, I would be really uneasy adding subviews to an UIImageView, it's really meant to hold one image. It would be better to create a normal UIView and add the UIButtons and UIImageView to that. It's very easy to control the ordering so that the buttons appears on top of the image.

P.S. Just to make your life easier, instead of :

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

You can use :

[scrollView2 setContentSize:scroll2ImageView.frame.size];
查看更多
登录 后发表回答