我的UIScrollView
自定义类存在问题,我用它来保存UIImageView
。
我有另一个带有主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
正如你所看到它应该适用于缩放,但事实并非如此!图像显得非常明显。我用来添加自定义视图的部分代码就是这一部分(在本例中我在主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];
}