我是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;
}
bg.png
也未应用于我的背景。
我们将不胜感激。
答案 0 :(得分:4)
答案实际上是许多其他答案的组合。评论,以及其他一些调整。
.imageView.size
和您的。{1}}
.scrollView.contentsSize
完全相同的事情。你不需要
设置.imageView.frame.
的代码将作为其中一部分发生
initWithImage:
。这将使尺寸不同。.scrollView.contentSize
设置为image.size
。
你做到这一点至关重要,至少就必须如此
在设置缩放比例之前发生。.userInteractionEnabled = YES
通常,这些是在storyboard / Interface Builder中设置的默认值,
所以我不打算将它们包含在下面的代码更改中。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;
}
答案 1 :(得分:1)
好吧,试试这个:
UIImageView *tempImgv = [[[UIImageView alloc]init] autorelease];
[tempImgv setImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/bg.png", [[NSBundle mainBundle]resourcePath]]]];
[tempImgv setFrame:your_desired_frame];
self.imageView = tempImgv;
[self.scrollWiew setContentSize: size_greater_than_scroll_frame];
[self.scrollWiew addSubview:self.imageView];
答案 2 :(得分:1)
首先检查您的滚动视图框架。仅当帧大小<1时才会启用滚动。 contentsize。因为如果所有内容都在框架中可见,那么为什么你需要滚动呢?
试试这个:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; //init with whatever frame you have scroll.scrollEnabled = YES // this is imp ... don't ignore this //whatever your code is self.scrollView.contentSize = image.size;// this is wrong //contentSize property to the size of the scrollable content. This specifies the size of the scrollable area. // do this self.scrollView.contentSize = imageView.frame.size;
答案 3 :(得分:0)
设置图像视图的用户交互启用属性,并将滚动视图设置为是。 请查看此代码,
- (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};
imageView.userInteractionEnabled = YES;
scrollView.userInteractionEnabled = YES;
[self.scrollView addSubview:self.imageView];
self.scrollView.contentSize = image.size;
[self centerScrollViewContents];
}
如果您需要将图片设置为滚动视图的背景,请使用:self.scrollView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"]]];
答案 4 :(得分:0)
在上面的代码中,您提供了
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;
您设置的scrollView的内容大小等于图像的大小,即上面的粗体代码。请尝试使用以下代码。
CGSize sizeOfScroll = image.size;
sizeOfScroll.width *= 1.5f;
sizeOfScroll.height *= 1.5f;
self.scrollView.contentSize = sizeOfScroll;
答案 5 :(得分:0)
滚动图片
scroll.userinteractionEnable = YES;
等等,
self.scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"image.png"]]];
答案 6 :(得分:0)
在上面的代码中,您需要设置最小和最大缩放比例。
self.scrollView.minimumZoomScale=0.5; self.scrollView.maximumZoomScale=6.0;
答案 7 :(得分:0)
您是否在其他地方设置了scrollview委托?
像这样:self.scrollView.delegate = self;