我试图像这样创建自己的ScrollableImageView:
@interface ZoomableImageScrollView : UIScrollView <UIScrollViewDelegate>
@property (strong, nonatomic) UIImageView *imageView;
- (void)setImage:(UIImage *)image;
@end
@implementation ZoomableImageScrollView
- (void)awakeFromNib
{
self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
// Finally
[self addSubview:self.imageView];
self.delegate = self;
}
- (void)setImage:(UIImage *)image
{
self.imageView.image = image;
self.imageView.frame = (CGRect){.origin = CGPointZero, .size = image.size};
self.contentSize = image.size;
CGRect scrollViewFrame = self.frame;
CGFloat scaleWidth = scrollViewFrame.size.width / self.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height / self.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);
self.minimumZoomScale = minScale;
self.maximumZoomScale = 4;
self.zoomScale = 1;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
NSLog(@"start!!");
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
NSLog(@"here!!");
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
NSLog(@"DidZoom");
}
@end
然后,我在其他viewControllers中创建了这个scrollView,设置了image,尝试缩放,但它从未遇到过3个最后一个委托方法。有人可以解释为什么这不起作用吗?如果我在viewController中使用这些行,它实际上就是这样。这是否意味着UIView无法实现某些协议,只有viewController才能实现?
最后,利用这些东西的最佳做法是什么?就像,我想创建可重复使用的可滚动图像视图。