【问题标题】:iOS moving tapped point on View within ScrollView to the center of screeniOS将ScrollView中View上的点击点移动到屏幕中心
【发布时间】:2015-06-14 06:49:48
【问题描述】:

我在 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];

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

它没有按我的预期工作,请帮我解决一下。

【问题讨论】:

    标签: ios objective-c uiscrollview zooming contentoffset


    【解决方案1】:

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

    你乘以比例,而不是除。

    【讨论】:

    • 不工作,点击点移到远离屏幕中心的左上角
    • @sharky101 能否发送有关如何获取触摸的代码?
    • 我在 View 上添加了 UITapGestureRecognizer ,每次点击时都会调用CGPoint tapped = [ScrollView locationInView:View];。检查我的代码。
    • [ScrollView locationInView:View] 给出了 ScrollView 在 View 中的位置。
    • @sharky101 也使用 thisNamingConvention 因为ScrollView 是一个变量,而不是一个类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    相关资源
    最近更新 更多