如何在iOS中找到缩放视图的可见部分的坐标以进行裁剪

时间:2015-04-22 13:41:17

标签: ios uiscrollview coordinates crop

我有一个名为ScrollView的UIScrollView,其中包含一个名为imageContainer的UIView,可以正确缩放。我有另一个名为croppingView的子视图,它比可见屏幕小一点。

StoryboardClip

我想捕捉可见部分的坐标。我认为imageContainer.frame.origin相对于它的SuperView,我相信它是ScrollView - 它们嵌套在故事板中。但是,在我缩放时记录imageContainer的原点显示它正在改变大小,但原点保持不变。我预计它会在放大时变为负片.crowpingView的原点会正确更改。

当我尝试使用croppingView的坐标抓取可见部分时,我得到了图片的错误部分。似乎我的抓取使用相对于真实缩放原点的正确坐标,但由于imageContainer.origin错误,我得到了下面我选择的图片的一部分。

这是日志输出的示例:

log output

这是willLayoutSubViews中的代码:

- (void)layoutSubviews{
[super layoutSubviews];
CGSize boundsSize = self.bounds.size;
CGRect croppingFrame = croppingView.frame;
croppingFrame.size.width = boundsSize.width - 40;
croppingFrame.size.height = boundsSize.height - 40;
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
if (currentOrientation==UIDeviceOrientationPortrait)
//Convert the bottom left corner in the visible window to the equivalent in the zoomed scrollView and set it as the origin of the cropping frame
    croppingFrame.origin = [self convertPoint:CGPointMake(20.0, 20.0) fromCoordinateSpace:(self.window.screen.fixedCoordinateSpace)];
else if (currentOrientation == UIDeviceOrientationLandscapeLeft){
    croppingFrame.origin = [self convertPoint:CGPointMake(self.bounds.size.height - 20.0, 20.0) fromCoordinateSpace:(self.window.screen.fixedCoordinateSpace)];
}
else if (currentOrientation == UIDeviceOrientationLandscapeRight){
    croppingFrame.origin = [self convertPoint:CGPointMake(20.0, self.bounds.size.width - 20.0) fromCoordinateSpace:(self.window.screen.fixedCoordinateSpace)];
}
croppingView.frame = croppingFrame;
cropButton.center = CGPointMake(croppingView.frame.origin.x+20, croppingView.frame.origin.y+20);
NSLog(@"imageContainer.frame.size: h, w = %f,%f",imageContainer.frame.size.height, imageContainer.frame.size.width);
NSLog(@"imageContainer.frame.origin: x,y = %f,%f", imageContainer.frame.origin.x,imageContainer.frame.origin.y);
NSLog(@"imageContainer.bounds.size: h, w = %f,%f",imageContainer.bounds.size.height, imageContainer.bounds.size.width);
NSLog(@"imageContainer.bounds.origin: x,y = %f,%f", imageContainer.bounds.origin.x,imageContainer.bounds.origin.y);
NSLog(@"Cropping frame size is h,w: %f , %f =",croppingView.frame.size.height, croppingView.frame.size.width);
NSLog(@"Cropping frame origin: x,y = %f,%f", croppingView.frame.origin.x,croppingView.frame.origin.y);
NSLog(@"Cropping frame bounds size: h, w = %f,%f",croppingView.bounds.size.height, croppingView.bounds.size.width);
NSLog(@"Cropping frame bounds origin: x,y = %f,%f", croppingView.bounds.origin.x,croppingView.bounds.origin.y);

}

很明显,我并不真正理解各种坐标系之间的关系以及旋转设备时会发生什么。子视图的起源是什么?当我从纵向旋转到横向时它是如何变化的?

我真正想要的是能够裁剪屏幕的可见区域!

感激不尽的任何帮助。

0 个答案:

没有答案