带有图像的 UIScrollView
Posted
技术标签:
【中文标题】带有图像的 UIScrollView【英文标题】:UIScrollView with image 【发布时间】:2012-03-14 07:42:58 【问题描述】:是否可以使用 UIScrollView 来查看大约 800px 长的图像?我尝试使用 UIScrollView 和 UIImageView 但它没有滚动。有人可以帮忙吗?
【问题讨论】:
【参考方案1】:用途:
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 代码
【讨论】:
【参考方案2】: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 -> 文件夹:支持文件)
【讨论】:
【参考方案3】:如果它没有平移,则意味着您忘记设置contentSize
属性。我的网站上有一个教程,展示了如何在 UIScrollView
中嵌入 UIImageView
并将其设置为平移和缩放。它包括完整的可下载源代码。见Panning and Zooming with UIScrollView
【讨论】:
【参考方案4】:@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];
【讨论】:
谢谢,但我一直在此页面上收到错误消息:image =[[UIImage View alloc] initWithImage:[UIImage imageNamed:@"test.png"]]; 不应该有空格:[UIImage View alloc] 应该说 [UIImageView alloc]以上是关于带有图像的 UIScrollView的主要内容,如果未能解决你的问题,请参考以下文章