我正在构建一个ViewController只是为了显示一个图像。我以编程方式将ImageView添加到滚动视图中。我想允许用户放大和缩小。这是我的代码
@interface ImageViewerViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIImageView *ImageView;
@property (weak, nonatomic) IBOutlet UIScrollView *Scroll;
@end
@implementation ImageViewerViewController
-(UIView*) viewForZoomingInScrollView{
return self.ImageView;
}
-(void) viewDidLoad{
self.Scroll.minimumZoomScale = 0.2;
self.Scroll.maximumZoomScale = 1.5;
self.Scroll.delegate = self;
NSLog(@"View did load");
if(self.imageName)
[self updateImage];
}
-(void)setImageName:(NSString *)imageName{
NSLog(@"set Image");
_imageName = imageName;
}
-(void)updateImage{
self.ImageView =[[UIImageView alloc]init];
self.ImageView.image = [UIImage imageNamed:self.imageName];
[self.ImageView sizeToFit];
self.Scroll.contentSize = self.imageName? self.ImageView.image.size: CGSizeZero;
[self.Scroll addSubview:self.ImageView];
}
@end
如您所见,我已将滚动的委托设置为self
,并添加了协议标题和所需的消息。
但缩放功能无效。
你能帮帮我吗? 我感谢你的时间和努力。此致
答案 0 :(得分:1)
这将在我创建它的演示时起作用。如有其他事情,请告诉我。
-(void)viewDidLoad
{
float minimumScale = [_floorPlanImageView frame].size.width /[_floorPlanScrollView frame].size.width;
_floorPlanScrollView.maximumZoomScale = 5; //Change as per you need
_floorPlanScrollView.minimumZoomScale = minimumScale; //Change as you need
_floorPlanScrollView.zoomScale = minimumScale;
_floorPlanScrollView.delegate =self;
_floorPlanScrollView.clipsToBounds = YES;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return _floorPlanImageView;
}