2012-03-14 55 views
1

是否可以使用UIScrollView來查看大約800px長的圖像?我嘗試使用UIScrollView和UIImageView,但它不滾動。任何人都可以幫忙嗎?UIScrollView與圖像

+0

發佈您的代碼。 – jcm 2012-03-14 07:45:43

回答

-1
@interface ZoomViewController : UIViewController <uiscrollviewdelegate> { 

    IBOutlet UIScrollView *scroll; 
    UIImageView *image; 
} 

@property (nonatomic, retain) UIScrollView *scroll; 

@end 
</uiscrollviewdelegate></uikit> 

一旦檢查您的Viewdidload方法。

- (void)viewDidLoad { 

     image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_body.png"]]; 

     [super viewDidLoad]; 

    image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_body.png"]]; 

    scroll.contentSize = image.frame.size; 

    [scroll addSubview:image]; 

    scroll.minimumZoomScale = 0.4; 

    scroll.maximumZoomScale = 4.0; 

    scroll.delegate = self; 
    [scroll setZoomScale:scroll.minimumZoomScale]; 
    [super viewDidLoad]; 

} 
+0

謝謝,但我一直在這個頁面上得到一個錯誤: image = [[UIImage View alloc] initWithImage:[UIImage imageNamed:@「test.png」]]; – XpApp 2012-03-14 08:49:35

+0

你正在得到什麼錯誤?你能在這裏粘貼錯誤嗎? – 2012-03-15 05:08:50

+0

不應該有一個空格:[UIImage View alloc]應該說[UIImageView alloc] – 2013-05-16 17:10:17

0

用途:

UIScrollView *yourScrollview = [[UIScrollView alloc] initWithFrame:(CGRect){{0,0}, {320,480}}]; 

UIImageView *yourImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourImageHere.png"]]; 
yourImageView.frame = yourScrollview.bounds; 

yourScrollview.contentSize = yourImageView.frame.size; 
yourScrollview.minimumZoomScale = 0.4; 
yourScrollview.maximumZoomScale = 4.0; 
[yourScrollview setZoomScale:yourScrollview.minimumZoomScale]; 
yourScrollview.delegate = self; 
[self.view addSubview:yourScrollview]; 

不要忘記添加UIScrollViewDelegate在.h文件中

注:ARC代碼

0

1.創建一個新的項目 - >單查看應用程序
2.將以下代碼放入:

「ViewController.m」

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

-(void) viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; 
    [scrollView setContentSize:CGSizeMake(320, 800)]; 

    UIView * myView = [[UIView alloc] initWithFrame : CGRectMake(0, 0, 320, 800)]; 
    UIImageView * myImage = [[UIImageView alloc]initWithImage : [UIImage imageNamed:@"Test.png"]]; 
    [myView addSubview: myImage]; 
    [myImage release]; 
    [scrollView addSubview: myView]; 
    [myView release]; 

    [self.view addSubview: scrollView]; 
    [scrollView release]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 
@end 

3.將圖像 「Test.png」 到你的項目(拖放到Xcode中 - >文件夾:支持文件)

0

如果不是平移它意味着你忘記了設置contentSize屬性。我在我的網站上有一個教程,演示如何在UIScrollView中嵌入UIImageView並將其設置爲平移和縮放。它包含完整的可下載源代碼。請參閱Panning and Zooming with UIScrollView