我正在使用UIScrollview来缩放图像..我的UIScrollview大小是固定的,所以每当我对图像进行缩放然后它就会被剪切..我想这样做,因为我缩放图像UIScrollview也增加高度和宽度根据图像缩放.. 任何建议将不胜感激......
答案 0 :(得分:3)
这应该有效: - )
viewForZoomingInScrollView
将它放入.h文件中:
UIImageView *imageView;
@property (nonatomic, retain) UIImageView *imageView;
请记住合成并发布imageView
将它放在您想要滚动视图的位置:
UIImage *image = [UIImage imageNamed:@"myImage.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setMinimumZoomScale:0.5];
[scrollView setMaximumZoomScale:3.0];
[scrollView setContentSize:CGSizeMake(image.size.width, image.size.height)];
[scrollView setDelegate:self];
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];
[scrollView release];
添加UIScrollView委托方法
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
<强>更新强>
我在添加上述解决方案时误解了这个问题。这应该这样做: - )
kScrollViewAddSize
是一个常量,它定义了添加到UIScrollView大小的数量。
在你的.h文件中,你应该添加以下内容:
UIImageView *imageView;
UIScrollView *scrollView;
请记住合成并发布它们。
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"apple.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(kScrollViewAddSize / 2, kScrollViewAddSize / 2, image.size.width, image.size.height)];
int width = imageView.frame.size.width + kScrollViewAddSize;
int height = imageView.frame.size.height + kScrollViewAddSize;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - width) / 2, (self.view.frame.size.height - height) / 2, width, height)];
[scrollView setMinimumZoomScale:0.5];
[scrollView setMaximumZoomScale:3.0];
[scrollView setBackgroundColor:[UIColor redColor]];
[scrollView setContentSize:CGSizeMake(image.size.width, image.size.height)];
[scrollView setDelegate:self];
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];
}
- (void)scrollViewDidZoom:(UIScrollView *)_scrollView {
int width = imageView.frame.size.width + kScrollViewAddSize;
int height = imageView.frame.size.height + kScrollViewAddSize;
[scrollView setFrame:CGRectMake((self.view.frame.size.width - width) / 2, (self.view.frame.size.height - height) / 2, imageView.frame.size.width + kScrollViewAddSize, imageView.frame.size.height + kScrollViewAddSize)];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)_scrollView {
return imageView;
}
在viewDidLoad
中添加要创建UIScrollView的parth并添加两个委托方法。