【问题标题】:Is this a zoom bug in UIScrollView?这是 UIScrollView 中的缩放错误吗?
【发布时间】:2009-09-15 22:17:25
【问题描述】:

在符合 UIApplicationDelegate 和 UIScrollViewDelegate 的类中,我做了以下操作:

- (void)applicationDidFinishLaunching:(UIApplication *)application {


// Created an instance of UIScrollView to house a horizontal strip - along the x-axis - of kNumberOfPages UIViews:

    scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];


// Make self the delegate

    scrollView.delegate = self;


// Set content size to the cumulative width of all UIViews contained

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);


// Zoom range is from a min of the width of the scrollView to a max of 2 * scrollView.contentSize

    scrollView.minimumZoomScale =  scrollView.frame.size.width / scrollView.contentSize.width;
    scrollView.maximumZoomScale =  2 * scrollView.contentSize;


// A subclass of UIView will be the container for the horizontal strip of UIViews

    containerView = [[ConstrainedView alloc] initWithFrame:CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)];


// The only difference betrween ConstrainedView and UIView is this overloaded method that constrains zooming to only the x-axis


- (void)setTransform:(CGAffineTransform)newValue {


    // Scale along the y-axis only
    CGAffineTransform constrainedTransform = CGAffineTransformScale(CGAffineTransformIdentity, newValue.a, 1.0);


     [super setTransform:constrainedTransform];


}


// Fill the container view with UIViews as subviews
CGFloat horizontalOffsetX = 0.0;
for (int i = 1; i <= kNumberOfPages; i++) {

    CGRect frame = CGRectMake(horizontalOffsetX, 0.0, scrollView.frame.size.width, scrollView.frame.size.height);
    UIView *v = [[[UIView alloc] initWithFrame:frame] autorelease];


// paint the background of each UIView a random color.

    v.backgroundColor = [UIColor colorWithRed:randomRed green:randomGreen blue:randomBlue alpha:1.0];
    [containerView addSubview:v];

    horizontalOffsetX += v.bounds.size.width;

} // for (kNumberOfPages)



// insert the container view as a subview of scrollView
    [scrollView addSubview:containerView];

    [window addSubview:scrollView];
    [window makeKeyAndVisible];

}

看起来很温顺,对吧?这就是奇怪之处。放大 - 放大 - 工作正常。然而,当我放大时,阻力、抖动和速度越来越慢,就好像我在尝试完全缩小时拖着硬化的糖蜜一样。压缩 N 个 UIView 以使它们都出现在 UIScrollView 框架的范围内几乎是不可能的。很奇怪。

有人能解释一下发生了什么吗?如果这实际上是一个错误?

干杯,

道格

【问题讨论】:

  • 我注意到scrollView.maximumZoomScale = 2 * scrollView.contentSize; 这一行似乎不正确?我觉得应该是scrollView.maximumZoomScale = 2;

标签: iphone uiscrollview


【解决方案1】:

更新

我的方法似乎确实适用于以下警告。这里似乎存在边界条件问题。

这行得通:

scrollView.minimumZoomScale = (scrollView.frame.size.width / scrollView.contentSize.width) / 0.99;

这挂起 - 崩溃 - 应用程序:

scrollView.minimumZoomScale = scrollView.frame.size.width / scrollView.contentSize.width;

只要 scrollView 的宽度超过内容的宽度 - 无论差分缩放如何 - 应用似乎都可以正常工作。

我可以轻松解决这个小限制。很酷。

干杯,

道格

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 2017-01-20
    • 1970-01-01
    • 2012-11-24
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多