user892028
user892028

Reputation: 305

Keeping UIButton relative when zooming

I have a UIScrollview with a UIImageview inside it showing a floor plan. I also have a UIButton within the scrollview that acts as a marker/pin.

I have implemented zooming with pinching/double tapping on the image, but the UIButton element obviously doesn't move when the image scrolls and/or zooms.

I have tried the following code to try and resposition the button when a zoom is completed:

[myButton setFrame:CGRectMake(
                          (myButton.frame.origin.x - myButton.frame.size.width / 2) * _scrollView.zoomScale,
                          (myButton.frame.origin.y - myButton.frame.size.height / 2) * _scrollView.zoomScale,
                          myButton.frame.size.width, myButton.frame.size.height)];

This moves the button to the top left hand corner.

Has anyone got any idea of what I should be doing to keep the button relative to the image (the same way a marker does on a Google map).

Upvotes: 1

Views: 875

Answers (2)

user892028
user892028

Reputation: 305

Ok well this was solved by the following:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"Marker.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(640.0, 230.0, 30.0, 46.0);
[button addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchUpInside];
[self.imageView addSubview: button];
self.imageView.userInteractionEnabled = YES;

Basically by adding the button to the imageView directly and then enabling userInteraction, I was able to get all the functionality needed.

Upvotes: 0

Dustin
Dustin

Reputation: 6803

I answered a question very similar to this recently. If you don't need realtime updating (only update when a zoom is completed), then you should be able to use the method outlined here.

Upvotes: 1

Related Questions