2010-08-12 91 views
2

我似乎在這裏實現了所有正確的代碼。我的代碼中是否存在某些內容?UIScrollView無法在縮放後平移

// 
// ScrollyiPadViewController.m 
// ScrollyiPad 
// 
// Created by Sidwyn Koh on 5/19/10. 
// Copyright __MyCompanyName__ 2010. All rights reserved. 
// 

#import "ScrollyiPadViewController.h" 

@implementation ScrollyiPadViewController 

-(void)viewDidLoad{ 
    [self.view addSubview:scrollView]; 
    [scrollView addSubview:imageView]; 

    UIImage *image = [UIImage imageNamed:@"hardware-01-20100127.jpg"]; 
    scrollView.contentSize = [image size]; 
    scrollView.maximumZoomScale = 2.0; 
// scrollView.delegate = self; 
} 

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

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ 
    return imageView; 
} 



/* 
// The designated initializer. Override to perform setup that is required before the view is loaded. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 
} 
*/ 


/* 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
*/ 


/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

@end 

回答

-1

找到了,我忘了把clipstoBounds = YES

0

我會

scrollView.contentSize = imageView.frame.size; 

而不是

scrollView.contentSize = [image size]; 
+0

沒有這樣的屬性的ImageView :( 我也試過imageView.bounds.size和imageView.frame.size,既沒有工作(你甚至不能滾動/縮放) – 2010-08-12 13:07:03

+0

對不起,固定我的答案,應該確實是imageView.frame.size – mvds 2010-08-12 13:41:50

1

也許你需要設置的ImageView的框架內UIScrollView;

imageView.frame = CGRectMake(0, 0, imageViewer.image.size.width, imageViewer.image.size.height);

11

我正在經歷類似的東西;放大後平移將停止工作。找出委託函數'scrollViewDidEndZooming:withView:atScale:'我需要根據傳遞的'atScale'更新ScrollView的contentize屬性。

類似於: self.scrollView.contentSize = CGSizeMake(self.imageView.image.size.width * scale,self.imageView.image.size.height * scale);

self.imageView.frame相對於縮放比例自動更新;所以你不需要根據比例調整框架。

如果您沒有明確設置scrollView的內容大小,那麼它只會被賦予imageView的框架大小......這是imageView無法解析的原因。