我有一个自定义类的问题UIScrollView
,我用它来保存UIImageView
它的内部。
我有另一个带有Main UIScrollView的 ViewController ,我添加了一些CustomScrollViewClass
类的对象。我正在尝试在此自定义中启用缩放,UIScrollvView
但没有成功!!!
一切似乎都很好,但我无法放大UIImageView
我的对象内部CustomScrollViewClass
!我已经尝试了很多东西,但现在没有任何效果。
//CustomScrollViewClass .h file
#import <UIKit/UIKit.h>
@interface CustomScrollViewClass : UIScrollView <UIScrollViewDelegate>
{
BOOL iPad;
BOOL iPhone5;
NSString *imageName;
UIImageView *imageView;
}
@property (nonatomic,strong) NSString *imageName;
@property (nonatomic,strong) UIImageView *imageView;
- (id)initWithImageAndFrame:(CGRect)frame imageName:(NSString*)image;
@end
这是.m文件
//CustomScrollViewClass .m file
#import "CustomScrollViewClass.h"
@implementation CustomScrollViewClass
@synthesize imageName,imageView;
- (id)initWithImageAndFrame:(CGRect)frame imageName:(NSString*)image
{
self = [super initWithFrame:frame];
if (self) {
CGRect imagef;
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
if (screenSize.height > 480.0f) {
iPhone5 = YES;
imagef = CGRectMake(0, 0, 320, 388);
} else {
iPhone5 = NO;
imagef = CGRectMake(0, 0, 320, 420);
}
iPad = NO;
} else {
iPad = YES;
iPhone5 = NO;
imagef = CGRectMake(0, 0, 768, 824);
}
self.contentSize = imagef.size;
self.maximumZoomScale = 2.0;
self.minimumZoomScale = 1.0;
self.zoomScale = 1.0;
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = YES;
imageName = image;
UIImageView *imageAux = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
imageAux.frame = imagef;
imageAux.contentMode = UIViewContentModeScaleAspectFit;
imageView = imageAux;
[self addSubview:imageView];
NSLog(@"%@",imageName);
}
return self;
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
NSLog(@"%@",@"viewForZoomingInScrollView");
return self.imageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
NSLog(@"%@",@"scrollViewDidZoom");
}
@end
如您所见,它应该与缩放一起使用,但事实并非如此!图像正常显示。我用来添加自定义视图的代码部分是这个(在这个例子中,我在Main UIScrollView添加了 5 个自定义视图):
//Method in MainView that I add objects from CustomScrollViewClass
-(void)PrepareScrollView
{
CGSize scrollviewc;
//Add Main ScrollView
UIScrollView *scroll = [[UIScrollView alloc] init];
if (iPad) {
scrollviewf = CGRectMake(0, 200, 768, 824);
scrollviewc = CGSizeMake((768 * 5), 824);
}
else {
if (iPhone5){
scrollviewf = CGRectMake(0, 180, 320, 388);
scrollviewc = CGSizeMake((320 * 5), 388);
} else {
scrollviewf = CGRectMake(0, 60, 320, 420);
scrollviewc = CGSizeMake((320 * 5), 420);
}
}
scroll.frame = scrollviewf;
scroll.contentSize = scrollviewc;
scroll.pagingEnabled = YES;
for (int i = 0; i < 5; i++) {
CGRect imagef;
if (iPad) {
imagef = CGRectMake((i * 768), 0, 768, 824);
} else {
if (iPhone5){
imagef = CGRectMake((i * 320), 0, 320, 388);
} else {
imagef = CGRectMake((i * 320), 0, 320, 420);
}
}
//Add Custom ScrollView with UIImageView
CustomScrollViewClass *AUX = [[ImageView alloc] initWithImageAndFrame:imagef imageName:[NSString stringWithFormat:@"%i%@",i+1,@"_pt.jpg"]];
[scroll addSubview:AUX];
}
self.scrollView = scroll;
[self.view addSubview:self.scrollView];
}