Ios 缩放在自定义UIScrollView中不起作用
我有一个自定义类Ios 缩放在自定义UIScrollView中不起作用,ios,iphone,objective-c,uiscrollview,zooming,Ios,Iphone,Objective C,Uiscrollview,Zooming,我有一个自定义类UIScrollView的问题,我用它来保存UIImageView 我有另一个带有主UIScrollView的ViewController,我添加了CustomScrollViewClass类的一些对象。我正在尝试启用此自定义UIScrollvView中的缩放功能,但没有成功 一切似乎都正常,但我无法从CustomScrollViewClass缩放对象内部的UIImageView!我试过很多东西,但到现在都没用 //CustomScrollViewClass .h file #
UIScrollView
的问题,我用它来保存UIImageView
我有另一个带有主UIScrollView的ViewController,我添加了CustomScrollViewClass
类的一些对象。我正在尝试启用此自定义UIScrollvView
中的缩放功能,但没有成功
一切似乎都正常,但我无法从CustomScrollViewClass
缩放对象内部的UIImageView
!我试过很多东西,但到现在都没用
//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
正如您所看到的,它应该可以与缩放一起工作,但它不能!图像显示不正常。我用于添加自定义视图的代码部分如下(在本例中,我在主UIScrollView中添加了5个自定义视图):
//在MainView中我从CustomScrollViewClass添加对象的方法
-(无效)准备滚动视图
{
CGC;
//添加主滚动视图
UIScrollView*滚动=[[UIScrollView alloc]init];
if(iPad){
scrollviewf=CGRectMake(020076824);
scrollviewc=CGSizeMake((768*5),824);
}
否则{
if(iPhone5){
scrollviewf=CGRectMake(0、180、320、388);
scrollviewc=CGSizeMake((320*5),388);
}否则{
scrollviewf=CGRectMake(0,60,320,420);
scrollviewc=CGSizeMake((320*5),420);
}
}
scroll.frame=scrollviewf;
scroll.contentSize=scrollviewc;
scroll.paginEnabled=是;
对于(int i=0;i<5;i++){
CGRect图像;
if(iPad){
imagef=CGRectMake((i*768),076824);
}否则{
if(iPhone5){
imagef=CGRectMake((i*320),0320388);
}否则{
imagef=CGRectMake((i*320),0,320,420);
}
}
//使用UIImageView添加自定义滚动视图
CustomScrollViewClass*AUX=[[ImageView alloc]initWithImageAndFrame:imagef imageName:[NSString stringWithFormat:@“%i%@”、i+1、@“\u pt.jpg”];
[滚动添加子视图:辅助];
}
self.scrollView=滚动;
[self.view addSubview:self.scrollView];
}
正如我在您的代码中所看到的ImageView
本身是UIScrollView
的子类,因此您再次将其添加到新的滚动视图中,只需在[self.view addSubview:imageViewAux]
中添加ImageView*imageViewAux
对象即可Good LuckI已经将我的自定义类重命名为CustomScrollViewClass
,因此更好地理解它。但是是的,CustomScrollViewClass是UIScrollView的一个子类,我正在使用[scroll addSubview:AUX]在主UIScrollView中添加它的对象。因此在此之后,我的UIScrollView对象名为scrollView
接收scroll
,然后我使用[self.view addSubview:self.scrollView]将其添加到主视图中;这样做一切都很正常,请在我的CustomScrollViewClass
对象中执行缩放,这些对象是UIScrollView
的子类,我刚才说的只是从代码中删除滚动
,然后直接将辅助
添加到self.view
,在您的情况下,它无法将触摸传递到底部滚动视图,这就是原因。只需删除该滚动
代码并添加[self.view addSubview:AUX]
,但删除我的滚动
这是我的主要滚动视图,我将没有滚动功能!
//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
//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];
}