边界中心 [英] Center of bounds

查看:151
本文介绍了边界中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个全屏幕UIScrollView来显示我的图像,它使用一些代码从PhotoScroller.app从苹果示例代码。我对于获取imageScrollView的边界的中心点感到困惑。



ImageScrollView.m

   - (CGPoint)pointToCenterAfterRotation 
{

NSLog(@imageScrollView框架X和Y是%f和%f self.frame.origin.x,self.frame.origin.y);
NSLog(@imageScrollView框架宽度和高度是%f和%f,self.frame.size.width,self.frame.size.height);
NSLog(@imageScrollView bounds origin X and Y are%f and%f,self.bounds.origin.x,self.bounds.origin.y);
NSLog(@imageScrollView bounds Width and Height are%f and%f,self.bounds.size.width,self.bounds.size.height);

NSLog(@imageView frame origin X and Y are%f and%f,imageView.frame.origin.x,imageView.frame.origin.y);
NSLog(@imageView框架大小宽度和高度是%f和%f,imageView.frame.size.width,imageView.frame.size.height);
NSLog(@imageView bounds origin X and Y are%f and%f,imageView.bounds.origin.x,imageView.bounds.origin.y);
NSLog(@imageView bounds size Width and Height are%f and%f,imageView.bounds.size.width,imageView.bounds.size.height);

CGPoint boundsCenter = CGPointMake(CGRectGetMidX(self.bounds),CGRectGetMidY(self.bounds));
CGPoint convertPoint = [self convertPoint:boundsCenter toView:imageView];

NSLog(@boundsCenter X和Y是%f和%f,boundsCenter.x,boundsCenter.y);
NSLog(@convertPoint origin X and Y are%f and%f,convertPoint.x,convertPoint.y);

return [self convertPoint:boundsCenter toView:imageView];
}

- (void)setMaxMinZoomScalesForCurrentBounds
{
CGSize boundsSize = self.bounds.size;
CGSize imageSize = imageView.bounds.size;

//计算最小/最大缩放比例
CGFloat xScale = boundsSize.width / imageSize.width; //缩放需要完美适合图像宽度
CGFloat yScale = boundsSize.height / imageSize.height; //比例需要完美适合图像高度
CGFloat minScale = MIN(xScale,yScale); //使用最少的这些来允许图像变得完全可见
CGFloat maxScale = 1.0;
//不要让minScale超过maxScale。 (如果图像小于屏幕,我们不想强制它被缩放。)
if(minScale> maxScale){
minScale = maxScale;
}

self.maximumZoomScale = maxScale;
self.minimumZoomScale = minScale;
}
- (void)restoreCenterPoint:(CGPoint)oldCenter scale:(CGFloat)oldScale
{
//步骤1:恢复缩放比例,首先确保它在允许范围。
self.zoomScale = MIN(self.maximumZoomScale,MAX(self.minimumZoomScale,oldScale));

//步骤2:恢复中心点,首先确保它在允许的范围内。

// 2a:将我们想要的中心点转换回我们自己的坐标空间
CGPoint boundsCenter = [self convertPoint:oldCenter fromView:imageView];
// 2b:计算将产生中心点的内容偏移量
CGPoint offset = CGPointMake(boundsCenter.x - self.bounds.size.width / 2.0,
boundsCenter.y - self .bounds.size.height / 2.0);
// 2c:restore offset,调整到允许的范围内
CGPoint maxOffset = [self maximumContentOffset];
CGPoint minOffset = [self minimumContentOffset];
offset.x = MAX(minOffset.x,MIN(maxOffset.x,offset.x));
offset.y = MAX(minOffset.y,MIN(maxOffset.y,offset.y));

NSLog(@offset x is%f,offset.x);
NSLog(@offset y is%f,offset.y);

self.contentOffset = offset;
}

pointToCenterAfterRotation在视图控制器中调用,

 

code> - (void)viewDidLoad
{
[super viewDidLoad];
self.wantsFullScreenLayout = YES;
[[UIApplication sharedApplication] setStatusBarHidden:YES];
actionSheetCoverView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
self.view = actionSheetCoverView;
self.imageScrollView = [[[ImageScrollView alloc] init] autorelease];
imageScrollView.frame = [self frameForScrollView];
[actionSheetCoverView addSubview:imageScrollView];
imageScrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
}

- (CGRect)frameForScrollView {
CGRect bounds = actionSheetCoverView.bounds;
CGRect frame = bounds;
return frame;
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
CGPoint restorePoint = [imageScrollView pointToCenterAfterRotation];
CGFloat restoreScale = [imageScrollView scaleToRestoreAfterRotation];
[imageScrollView setMaxMinZoomScalesForCurrentBounds];
[imageScrollView restoreCenterPoint:restorePoint scale:restoreScale];

}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
CGPoint restorePoint = [imageScrollView pointToCenterAfterRotation];
//调用这里只是为nslog框架和边界在旋转之前。

}

self.frame。 origin equals(0,0)和 self.frame.size = self.bounds.size ,我认为boundsCenter应该是self.bounds.size / 2。但它不是。为什么?非常困惑!



将屏幕从纵向旋转到横向



NSLog in call willRotateToInterfaceOrientation:duration:

  2012-05-12 11:35:45.666 imageScrollView Y是0.000000和0.000000 
2012-05-12 11:35:45.672 imageScrollView框架宽度和高度是320.000000和480.000000
2012-05-12 11:35:45.677 imageScrollView边界原点X和Y是0.000000和0.000000
2012-05-12 11:35:45.682 imageScrollView边界宽度和高度为320.000000和480.000000
2012-05-12 11:35:45.686 imageView框架原点X和Y分别为0.000005和0.374437
2012-05-12 11:35:45.689 imageView框架大小宽度和高度是320.000000和479.251129
2012-05-12 11:35:45.693 imageView边界原点X和Y是0.000000和0.000000
2012-05-12 11:35:45.697 imageView边界大小宽度和高度是641.000000和959.999939
2012-05-12 11:35:45.701 boundsCenter X和Y是160.000000和240.000000
2012-05 -12 11:35:45.705 convertPoint origin X and Y are 320.500000 and 479.999969

NSLog调用willAnimateRotationToInterfaceOrientation:duration:

  2012-05-12 11:36:05.975 imageScrollView框架X和Y是0.000000和0.000000 
2012-05-12 11:36:05.978 imageScrollView框架宽度和高度是480.000000和320.000000
2012-05-12 11:36:05.983 imageScrollView边界原点X和Y是0.000000和0.000000
2012-05-12 11:36:05.987 imageScrollView边界宽度和高度是480.000000和320.000000
2012-05-12 11:36:05.990 imageView框架原点X和Y是80.000008和0.000017
2012-05-12 11:36:05.994 imageView框架大小宽度和高度是320.000000和479.251099
2012-05-12 11:36:06.002 imageView边界原点X和Y是0.000000和0.000000
2012- 05-12 11:36:06.006 imageView边界大小宽度和高度是641.000000和959.999878
2012-05-12 11:36:06.009 boundsCenter X和Y是240.000000和160.000000
2012-05-12 11 :36:06.014 convertPoint origin X and Y are 320.500000 and 320.499969


解决方案

p>你能NSLog self.bounds.origin吗?我认为问题是,你使用的中点函数给你相对于原点的矩形的中心。如果原点不是(0,0),boundsCenter不会只是bounds.size / 2


I have a full screen UIScrollView to display my image, which employ some codes from PhotoScroller.app from apple sample code. I am little confused on the obtaining of the center point of the imageScrollView's bounds.

ImageScrollView.m

- (CGPoint)pointToCenterAfterRotation
{

NSLog(@"imageScrollView frame X and Y are %f and %f ",self.frame.origin.x, self.frame.origin.y);
NSLog(@"imageScrollView frame Width and Height are %f and %f ",self.frame.size.width, self.frame.size.height);
NSLog(@"imageScrollView bounds origin X and Y are %f and %f ",self.bounds.origin.x, self.bounds.origin.y);
NSLog(@"imageScrollView bounds Width and Height are %f and %f ",self.bounds.size.width, self.bounds.size.height);

NSLog(@"imageView frame origin X and Y are %f and %f", imageView.frame.origin.x, imageView.frame.origin.y);
NSLog(@"imageView frame size Width and Height are %f and %f", imageView.frame.size.width,imageView.frame.size.height);
NSLog(@"imageView bounds origin X and Y are %f and %f", imageView.bounds.origin.x,imageView.bounds.origin.y);
NSLog(@"imageView bounds size Width and Height are %f and %f", imageView.bounds.size.width,imageView.bounds.size.height);

CGPoint boundsCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
CGPoint convertPoint = [self convertPoint:boundsCenter toView:imageView];

NSLog(@"boundsCenter X and Y are %f and %f ",boundsCenter.x, boundsCenter.y);
NSLog(@"convertPoint origin X and Y are %f and %f", convertPoint.x, convertPoint.y);

return [self convertPoint:boundsCenter toView:imageView];
}

- (void)setMaxMinZoomScalesForCurrentBounds
{
CGSize boundsSize = self.bounds.size;
CGSize imageSize = imageView.bounds.size;

// calculate min/max zoomscale
CGFloat xScale = boundsSize.width / imageSize.width;    // the scale needed to perfectly fit the image width-wise
CGFloat yScale = boundsSize.height / imageSize.height;  // the scale needed to perfectly fit the image height-wise
CGFloat minScale = MIN(xScale, yScale);                 // use minimum of these to allow the image to become fully visible
CGFloat maxScale = 1.0;
// don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.) 
if (minScale > maxScale) {
    minScale = maxScale;
}

self.maximumZoomScale = maxScale;
self.minimumZoomScale = minScale;
}
- (void)restoreCenterPoint:(CGPoint)oldCenter scale:(CGFloat)oldScale
{    
// Step 1: restore zoom scale, first making sure it is within the allowable range.
self.zoomScale = MIN(self.maximumZoomScale, MAX(self.minimumZoomScale, oldScale));

// Step 2: restore center point, first making sure it is within the allowable range.

// 2a: convert our desired center point back to our own coordinate space
CGPoint boundsCenter = [self convertPoint:oldCenter fromView:imageView];
// 2b: calculate the content offset that would yield that center point
CGPoint offset = CGPointMake(boundsCenter.x - self.bounds.size.width / 2.0, 
                             boundsCenter.y - self.bounds.size.height / 2.0);
// 2c: restore offset, adjusted to be within the allowable range
CGPoint maxOffset = [self maximumContentOffset];
CGPoint minOffset = [self minimumContentOffset];
offset.x = MAX(minOffset.x, MIN(maxOffset.x, offset.x));
offset.y = MAX(minOffset.y, MIN(maxOffset.y, offset.y));

NSLog(@"offset x is %f",offset.x);
NSLog(@"offset y is %f",offset.y);

self.contentOffset = offset;
}

pointToCenterAfterRotation is called in the view controller who takes care of the scroll view.

ScrollViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
self.wantsFullScreenLayout = YES;
[[UIApplication sharedApplication] setStatusBarHidden:YES];
actionSheetCoverView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
self.view = actionSheetCoverView;
self.imageScrollView = [[[ImageScrollView alloc] init] autorelease];
imageScrollView.frame = [self frameForScrollView];
[actionSheetCoverView addSubview:imageScrollView];
imageScrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
}

- (CGRect)frameForScrollView {
CGRect bounds = actionSheetCoverView.bounds;
CGRect frame = bounds;
return frame;
}

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{   
CGPoint restorePoint = [imageScrollView pointToCenterAfterRotation];
CGFloat restoreScale = [imageScrollView scaleToRestoreAfterRotation];
[imageScrollView setMaxMinZoomScalesForCurrentBounds];
[imageScrollView restoreCenterPoint:restorePoint scale:restoreScale]; 

}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
CGPoint restorePoint = [imageScrollView pointToCenterAfterRotation];
//call here just for nslog the frame and bounds before rotation.

}

As the self.frame.origin equals (0, 0) and self.frame.size = self.bounds.size, I think the boundsCenter should be self.bounds.size/2. But it is not. Why? really confused!

Rotate the screen from portrait to landscape:

NSLog in call willRotateToInterfaceOrientation:duration:

2012-05-12 11:35:45.666 imageScrollView frame X and Y are 0.000000 and 0.000000 
2012-05-12 11:35:45.672 imageScrollView frame Width and Height are 320.000000 and 480.000000 
2012-05-12 11:35:45.677 imageScrollView bounds origin X and Y are 0.000000 and 0.000000 
2012-05-12 11:35:45.682 imageScrollView bounds Width and Height are 320.000000 and 480.000000 
2012-05-12 11:35:45.686 imageView frame origin X and Y are 0.000005 and 0.374437
2012-05-12 11:35:45.689 imageView frame size Width and Height are 320.000000 and 479.251129
2012-05-12 11:35:45.693 imageView bounds origin X and Y are 0.000000 and 0.000000
2012-05-12 11:35:45.697 imageView bounds size Width and Height are 641.000000 and 959.999939
2012-05-12 11:35:45.701 boundsCenter X and Y are 160.000000 and 240.000000 
2012-05-12 11:35:45.705 convertPoint origin X and Y are 320.500000 and 479.999969

NSLog in call willAnimateRotationToInterfaceOrientation:duration:

2012-05-12 11:36:05.975 imageScrollView frame X and Y are 0.000000 and 0.000000 
2012-05-12 11:36:05.978 imageScrollView frame Width and Height are 480.000000 and 320.000000 
2012-05-12 11:36:05.983 imageScrollView bounds origin X and Y are 0.000000 and 0.000000 
2012-05-12 11:36:05.987 imageScrollView bounds Width and Height are 480.000000 and 320.000000 
2012-05-12 11:36:05.990 imageView frame origin X and Y are 80.000008 and 0.000017
2012-05-12 11:36:05.994 imageView frame size Width and Height are 320.000000 and 479.251099
2012-05-12 11:36:06.002 imageView bounds origin X and Y are 0.000000 and 0.000000
2012-05-12 11:36:06.006 imageView bounds size Width and Height are 641.000000 and 959.999878
2012-05-12 11:36:06.009 boundsCenter X and Y are 240.000000 and 160.000000 
2012-05-12 11:36:06.014 convertPoint origin X and Y are 320.500000 and 320.499969

解决方案

can you NSLog self.bounds.origin? I think the issue is that the midpoint functions you are using give you the center of the rectangle in relation to the origin. If the origin is not (0,0), the boundsCenter will not just be bounds.size/2

这篇关于边界中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆