我有一个NSObject,它实例化了一个添加了UITapGestureRecognizer的视图。出了点问题,因为水龙头有时只能起作用......
#import "Monument.h"
@implementation Monument
-(id)initWithFrame:(CGRect)frame itiName:(NSString *)itiName itiPay:(BOOL)itiPay available:(BOOL)available imageName:(NSString *)imageName viewController:(UIViewController *)controller
{
self = [super init];
if (self)
{
self.view = [[UIView alloc] initWithFrame:frame];
self.view.userInteractionEnabled = YES;
self.itiName = itiName;
self.itiPay = itiPay;
self.itiImage = imageName;
self.controller = controller;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:itiName])
{
self.buy = NO;
}
else
{
self.buy = YES;
}
self.x = frame.origin.x;
self.y = frame.origin.y;
self.available = available;
}
return self;
}
-(UIView *)drawView
{
UIImage *background = [UIImage imageNamed:self.itiImage];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:background];
backgroundView.frame = self.view.frame;
backgroundView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:backgroundView];
// Inzializzo la Label del titolo
UILabel *titolo = [[UILabel alloc] initWithFrame:CGRectMake(self.x+8.0f, self.y+2.0f, 180.0f, 20.0f)];
titolo.text = self.itiName;
titolo.textColor = [UIColor whiteColor];
[self.view addSubview:titolo];
UIImage *imageFreeOrPayOrBought;
if (! self.buy)
{
if (self.itiPay)
{
imageFreeOrPayOrBought = [UIImage imageNamed:@"pay-ipad"];
}
else
{
imageFreeOrPayOrBought = [UIImage imageNamed:@"free-ipad"];
}
}
else
{
// già comprato
imageFreeOrPayOrBought = [UIImage imageNamed:@"free-ipad"];
}
UIImageView *payOrFree = [[UIImageView alloc] initWithImage:imageFreeOrPayOrBought];
CGRect frameImage = CGRectMake(self.x, self.y+117.0f, 214.5f, 20.8f);
payOrFree.frame = frameImage;
payOrFree.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:payOrFree];
// Controllo se disponibile o meno
if (! self.available)
{
self.view.layer.opacity = .3f;
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
[self.view addGestureRecognizer:tap];
return self.view;
}
-(void)tapRecognized: (UITapGestureRecognizer *)tap
{
if (! self.available)
{
// [self openUnavailableService];
NSLog(@"Service unavailable");
}
else
{
if (self.buy)
{
//[self openMonumentHomePage];
NSLog(@"Service bought");
}
else
{
if (self.itiPay)
{
//[self openPurchaseInApp];
NSLog(@"Service to buy");
}
else
{
//[self downloadDataForFree];
NSLog(@"Service to free download");
}
}
}
}
这就是我使用类
的方式self.arrayOfMonuments = [[NSMutableArray alloc] init];
self.scroll.contentSize = CGSizeMake(568.0f, 640.0f);
Monument *foroRomano = [[Monument alloc] initWithFrame:CGRectMake(20.0f, 0.0f, 217.1f, 117.0f) itiName:@"FORO ROMANO" itiPay:NO available:YES imageName:@"fori-romano-ipad" viewController:self];
[self.arrayOfMonuments addObject:foroRomano];
UIView *foroRomanoView = [foroRomano drawView];
[self.scroll addSubview:foroRomanoView];
Monument *colosseo = [[Monument alloc] initWithFrame:CGRectMake(150.0f, 0.0f, 217.1f, 117.0f) itiName:@"COLOSSEO" itiPay:NO available:YES imageName:@"colosseo-ipad" viewController:self];
[self.arrayOfMonuments addObject:colosseo];
[self.scroll addSubview:[colosseo drawView]];
Monument *appiaAntica = [[Monument alloc] initWithFrame:CGRectMake(20.0f, 80.0f, 217.1f, 117.0f) itiName:@"APPIA ANTICA" itiPay:YES available:NO imageName:@"appia-antica-ipad" viewController:self];
[self.arrayOfMonuments addObject:appiaAntica];
UIView *appiaAnticaView = [appiaAntica drawView];
[self.scroll addSubview:appiaAnticaView];
Monument *esquilino = [[Monument alloc] initWithFrame:CGRectMake(150.0f, 80.0f, 217.1f, 117.0f) itiName:@"ESQUILINO" itiPay:YES available:NO imageName:@"esquilino-ipad" viewController:self];
[self.arrayOfMonuments addObject:esquilino];
[self.scroll addSubview:[esquilino drawView]];
Monument *palatino = [[Monument alloc] initWithFrame:CGRectMake(20.0f, 160.0f, 217.1f, 117.0f) itiName:@"PALATINO" itiPay:YES available:NO imageName:@"palatino-ipad" viewController:self];
[self.arrayOfMonuments addObject:palatino];
[self.scroll addSubview:[palatino drawView]];
Monument *laterano = [[Monument alloc] initWithFrame:CGRectMake(150.0f, 160.0f, 217.1f, 117.0f) itiName:@"LATERANO" itiPay:YES available:NO imageName:@"laterano-ipad" viewController:self];
[self.arrayOfMonuments addObject:laterano];
[self.scroll addSubview:[laterano drawView]];
Monument *foriImperiali = [[Monument alloc] initWithFrame:CGRectMake(20.0f, 240.0f, 217.1f, 117.0f) itiName:@"FORI IMPERIALI" itiPay:YES available:NO imageName:@"fori-imperiali-ipad" viewController:self];
[self.arrayOfMonuments addObject:foriImperiali];
[self.scroll addSubview:[foriImperiali drawView]];
怎么会发生?非常感谢
编辑:我也尝试用UIButton替换UITapGesture,但问题仍然存在
UIButton *monumentBtn = [[UIButton alloc] initWithFrame:self.view.frame];
monumentBtn.backgroundColor = [UIColor clearColor];
[self.view addSubview:monumentBtn];
[monumentBtn addTarget:self action:@selector(tapRecognized:) forControlEvents:UIControlEventTouchUpInside];
编辑:我注意到我传递给init方法的x / y坐标是错误的,因为第一帧的高度是117.0f,位于此下的视图的原点是80.0f。这很奇怪,这就是Tap并不总是有效的原因。我该如何解决这个问题?
重新编辑:我用屏幕映射解决了......遗憾的是我没有找到其他解决方案。