简体   繁体   中英

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:

//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.

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

您乘以比例,而不是除。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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