我有1年的Objective-C工作经验,是我做的第一个问题,因为我总是在这个网站上找到答案,但这种情况完全是疯了,没有发现任何类似的东西。UITapGesture只有在重新加载UIViewController后才能正常工作
我有一个自定义类(从UIView称为BallClass)。 UIView里面有一个UIImageView和一个UITapGesture来做一个动作。
因此,当我在UIViewController中创建对象时,按下对象时不起作用tapAction,但如果我切换到其他屏幕并返回它,tapGesture的工作就非常完美。
我尝试了很多选择,但我无法在第一次加载中工作。我使用ARC,Xcode(4.6.3)和OSX(10.8.4)和IB工作,但是这个对象是在没有IB的情况下创建的。
在此先感谢。
这里是类BallClass.m
- (void)tapGesture:(UIGestureRecognizer *)tapGesture {
NSLog(@"hola");
}
- (id)initBall:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
// Recibir las variables iniciales para el control del timer
ball = [[Ball alloc] init];
// Agregar la imagen de la pelota en el view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height)];
[imageView setCenter:CGPointMake(CGRectGetMidX([self bounds]), CGRectGetMidY([self bounds]))];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImage:[UIImage imageNamed:@"pelota.png"]];
[imageView setTag:100];
[imageView setNeedsDisplay];
// Add TAP
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
// Tap Properties
[tapGesture setDelaysTouchesBegan:YES];
[tapGesture setNumberOfTapsRequired:1];
[tapGesture setDelegate:(id)self];
[imageView addGestureRecognizer:tapGesture];
[self addSubview:imageView];
[self setNeedsDisplay];
// Propiedades del View
[self setUserInteractionEnabled:YES];
self.backgroundColor = [UIColor clearColor];
[self setTag:100];
// BALL BOUNCE
[NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
}
return self;
}
- (void)onTimer:(NSTimer *)timerLocal {
// Do something
}
... // more methods
这里是实例化:
ballView00 = [[BallView alloc] initBall:CGRectMake(10, 10, 40, 40)];
UIView的完美展现,和动画工作正常,但UITapGesture只是工作,早提及,重新加载ViewController后。
这里是图像Ball和UIView(@kBall)的链接。对不起,有关图像,但我不能负荷,直到我有10分:(
好主意@kball,让我检查,我稍后返回结果。 – Beto
不幸的是仍然是同样的问题。我重新配置我的ImageView,但不起作用。实际上,我更新了我的UIImageView配置代码,并添加了黑色背景的球和UIView的图像。 – Beto