我有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