2009-09-15 33 views
1

在符合UIApplicationDelegate和UIScrollViewDelegate類我做了如下的:這是UIScrollView中的縮放bug嗎?

- (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個UIViews,使它們都出現在UIScrollView框架的範圍內。很奇怪。

有人可以請解釋發生了什麼,如果這實際上是一個錯誤?

乾杯,

道格

+0

我注意到這條線'scrollView.maximumZoomScale = 2 * scrollView.contentSize;'似乎不正確?我認爲它應該是'scrollView.maximumZoomScale = 2;' –

回答

1

UPDATE

我的做法確實出現有以下告誡工作。這裏似乎存在邊界條件問題。

這工作:

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

這掛起 - 崩潰 - 該應用:

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

只要scrollView的寬度超過內容的寬度 - 無論差異縮放 - 應用程序似乎工作得很好。

我可以輕鬆解決這個小問題。涼。

乾杯,

道格