Kru*_*nal 5 iphone uiscrollview uiimage ipad
我是iPhone开发人员的新手,
我想在我的图片中滚动,
这是我的代码片段,
- (void)viewDidLoad {
UIImage *image1 = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"]];
self.imageView = [[UIImageView alloc] initWithImage:image1];
self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image1.size};
[self.scrollView addSubview:self.imageView];
self.scrollView.contentSize = image.size;
[self centerScrollViewContents];
}
- (void)centerScrollViewContents {
CGSize boundsSize = self.scrollView.bounds.size;
CGRect contentsFrame = self.imageView.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
self.imageView.frame = contentsFrame;
}
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
// Return the view that you want to zoom
return self.imageView;
}
Run Code Online (Sandbox Code Playgroud)
也bg.png没有适用于我的背景.
任何帮助将不胜感激.
答案实际上是许多其他答案和评论的组合,再加上一些其他调整。
.imageView.size和 your
设置.scrollView.contentsSize为完全相同的东西。您不需要设置.imageView.frame. 将作为 的一部分发生的
代码initWithImage:。这将使尺寸不同。.scrollView.contentSize设为image.size。在哪里执行此操作很重要,至少在设置缩放比例之前必须执行此操作。.userInteractionEnabled = YES,但一般来说,这些是在故事板/界面生成器中设置的默认值,所以我不会将它们包含在下面的代码更改中。centerScrollViewContents并没有真正使内容居中,而是重新塑造 imageView 框架。我只是注释掉了这个电话,因为我认为这是完全没有必要的。相反,我做了一个简单的调用以在左上角启动您的图像。zoomScale、minimumZoomScale和
。maximumZoomScale如果您还想进行捏缩放,那么您需要将这三个设置全部设置在适当的位置。这是应用这些项目后的代码。
(void)viewDidLoad {
self.scrollView.delegate = self; // can be set in storyboard
self.scrollView.scrollingEnabled = YES; // can be set in storyboard
NSString* bgPng
= [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"];
UIImage *image1 = [UIImage imageWithContentsOfFile:bgPng];
self.imageView = [[UIImageView alloc] initWithImage:image1];
// self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image1.size};
[self.scrollView addSubview:self.imageView];
self.scrollView.contentSize = image.size;
// set zoom scale here if desired.
// if zoom is implemented, the following call should be made
// after setting .zoomScale, .minimumZoomScale & .maximumZoomScale
self.scrollView.contentOffset = CGPointZero;
// [self centerScrollViewContents];
}
//- (void)centerScrollViewContents {
// CGSize boundsSize = self.scrollView.bounds.size;
// CGRect contentsFrame = self.imageView.frame;
//
// if (contentsFrame.size.width < boundsSize.width) {
// contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
// } else {
// contentsFrame.origin.x = 0.0f;
// }
//
// if (contentsFrame.size.height < boundsSize.height) {
// contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
// } else {
// contentsFrame.origin.y = 0.0f;
// }
//
// self.imageView.frame = contentsFrame;
//}
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
// Return the view that you want to zoom
return self.imageView;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1819 次 |
| 最近记录: |