简体   繁体   English

iOS将ScrollView内View上的分接点移动到屏幕中心

[英]iOS moving tapped point on View within ScrollView to the center of screen

I have an UIView(0,0,1000,1000) within an UIScrollView(0,0,500,500) as the UIScrollView's content, here's my code: 我在UIScrollView(0,0,500,500)中有一个UIView(0,0,1000,1000)作为UIScrollView的内容,这是我的代码:

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

It didn't work as I expected, please help me figure it out. 它没有按我预期的那样工作,请帮我弄清楚。

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

您乘以比例,而不是除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM