iPhone iOS使用缩放计算UIScrollView中的位置

时间:2012-04-13 16:44:12

标签: iphone objective-c ios uiscrollview coordinates

我在UIScrollView中有一个颜色贴图,我正在尝试对该贴图的像素颜色进行采样。样本掩模版位于滚动视图上方,而用户将滚动视图的内容移动到掩模版下方。

用户可以通过轻击手势放下标线,但我想提供一个额外的选项,即在标线下移动视图。

我正在尝试找出我如何理解缩放视图当前位于光罩下的x,y坐标。到目前为止,这个逻辑使我无法理解,特别是因为涉及到放大/缩小。

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    CGPoint mapLocation = [tapGestureRecognizer locationInView:self.surfaceMap];

     NSLog(@"mapLocation (x,y) %.0f,%.0f",mapLocation.x,mapLocation.y);

    NSLog(@"contentOffset (x,y) %.0f,%.0f",self.scrollView.contentOffset.x,self.scrollView.contentOffset.y);

    //calculate where the marker is pointing to in the surface map while the scrollview is scrolling

    int frameWidth = self.surfaceMap.frame.size.width;
    int frameHeight = self.surfaceMap.frame.size.height;

//this is what I'm trying to calculate
    CGPoint trueLocation = CGPointMake(self.scrollView.contentOffset.x+frameWidth-self.surfaceMap.frame.origin.x, self.scrollView.contentOffset.y-self.surfaceMap.frame.origin.y);
    NSLog(@"trueLocation (x,y) %.0f,%.0f",trueLocation.x,trueLocation.y);

    [self colorOfPixelAtPoint:trueLocation];

}

感谢任何输入!

1 个答案:

答案 0 :(得分:1)

您可能希望在UIView中查看这两个方法:

- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;