iOS将ScrollView中的View上的点击点移动到屏幕中心

时间:2015-04-09 03:55:15

标签: ios objective-c uiscrollview zoom contentoffset

我在UIScrollView(0,0,500,500)中有一个UIView(0,0,1000,1000)作为UIScrollView' s 内容,这是我的代码:

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

当UIScrollView缩放比例为1.0x时可以正常工作,但是当我将其缩放到0.5x时代码修改如下:

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

它没有像我预期的那样工作,请帮我搞清楚。

1 个答案:

答案 0 :(得分:0)

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

你按比例乘以而不是除以。