-1

我有个问题。我有这个代码缩放不起作用。

- (void)viewDidLoad {

UIImage *image = [UIImage imageNamed:@"zoomimage.jpeg"];

CGRect yFrame = CGRectMake(0.0, 0.0, 320, 432);

imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
[imageView setImage:image];

scrollView = [[UIScrollView alloc] initWithFrame:yFrame];
[scrollView setScrollEnabled:YES];
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setContentSize:CGSizeMake(image.size.width, image.size.height)];
scrollView.minimumZoomScale = 0.4;
scrollView.maximumZoomScale = 4.0;
[scrollView setZoomScale:scrollView.minimumZoomScale];

[self.view addSubview:scrollView];
[self.view bringSubviewToFront:scrollView];

[scrollView addSubview:imageView];

}

4

2 回答 2

5

首先,将滚动视图的委托设置为您自己。

scrollView.delegate = self;

现在,在您的类中添加此委托方法UIScrollView以启用图像缩放。

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
  return imageView;
}
于 2012-07-23T11:30:33.400 回答
1

尝试这个:

.h 控制器:

@interface MyViewController : UIViewController{

IBOutlet UIScrollView *scrollView; 
UIImageView *imageView;

}

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *imageView;

.m 控制器:

@synthesize scrollView, imageView;


-(UIView *)viewForZoomingInScrollView:(UIScrollVi­ew *)scrollView{ 

return imageView;

}

- (void)viewDidLoad
{

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YourImageName.png"]]; 
self.imageView = tempImageView; 
[tempImageView release]; 
scrollView.maximumZoomScale = 3.0; 
scrollView.minimumZoomScale = 0.6; 
scrollView.clipsToBounds = YES; 
scrollView.delegate = self; 
[scrollView addSubview:imageView];
}

这段代码对我有用:)

于 2013-11-10T17:26:31.350 回答