1

I have an UIView(0,0,1000,1000) within an UIScrollView(0,0,500,500) as the UIScrollView's content, here's my code:

//ScrollView setting
ScrollView.minimumZoomScale = 0.5;
ScrollView.maximumZoomScale = 1.0;
ScrollView.contentSize = View.frame.size
ScrollView.delegate = self;

//Get the tapped point & Place a mark on it
//already add an UITapGestureRecognizer on View
CGPoint tapped = [tapRecognizer locationInView:View];
UIView mark = [[UIView alloc] initWithFrame:CGRectMake(tapped.x-25,tapped.y-25,50,50)];
[View addSubview:mark];    

//Centering the tapped point (1.0x)
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGPoint screenCenter = CGPointMake(screenRect.size.width/2,screenRect.size.height/2);
[ScrollView setContentOffset:(tapped-screenCenter.x, tapped-screenCenter.y) animated:YES];

This works fine when the UIScrollView zoom scale is 1.0x, but when I zoom it to 0.5x with code modified as below:

//Get zoom scale
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
CGfloat zoomScale = scale;
}
//Centering the tapped point (0.5x)
[ScrollView setContentOffSet:(tapped-screenCenter.x/zoomScale, tapped-screenCenter.y/zoomScale) animated:YES];

It didn't work as I expected, please help me figure it out.

bluenowhere
  • 2,683
  • 5
  • 24
  • 37

1 Answers1

0

[View setContentOffSet:(tapped-screenCenter.x * zoomScale, tapped-screenCenter.y * zoomScale) animated:YES];

You multiply by scale, not divide.

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
  • not working, tapped point moved to the left top far away from the screen center – bluenowhere Apr 09 '15 at 05:09
  • @sharky101 can you send the code on how you fetch the touch? – Schemetrical Apr 09 '15 at 05:21
  • I add UITapGestureRecognizer on the View , `CGPoint tapped = [ScrollView locationInView:View];` would be called every time you tap. check my code. – bluenowhere Apr 09 '15 at 05:28
  • `[ScrollView locationInView:View]` gives the location of the ScrollView in View. – Schemetrical Apr 09 '15 at 05:31
  • @sharky101 Also use thisNamingConvention because `ScrollView` is a variable, not a class. – Schemetrical Apr 09 '15 at 05:37
  • sorry for the typo, it's actually `[tapRecognizer locationInView:View]` for getting the tap location, also edited my code. I think the problem is bout the changing of contentOffset after zooming, and I probably use the wrong multiplier. – bluenowhere Apr 09 '15 at 05:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74787/discussion-between-schemetrical-and-sharky101). – Schemetrical Apr 09 '15 at 05:54