带图像的UIScrollView

时间:2012-03-14 07:42:58

标签: ios uiscrollview uiimageview

是否可以使用UIScrollView查看大约800像素长的图像?我尝试使用UIScrollView和UIImageView,但它不滚动。有人可以帮忙吗?

4 个答案:

答案 0 :(得分: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];

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

注意:ARC代码

答案 1 :(得分:0)

<强> 1。创建一个新项目 - &gt;单一视图应用
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中 - &gt;文件夹:支持文件)

答案 2 :(得分:0)

如果没有平移,则表示您忘记设置contentSize属性。我的网站上有一个教程,介绍如何在UIImageView中嵌入UIScrollView并将其设置为平移和缩放。它包括完整的可下载源代码。见Panning and Zooming with UIScrollView

答案 3 :(得分:-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];

}