私はObjective-Cで1年間働いており、このサイトで常に答えを見つけたので最初の質問ですが、この状況は完全に正気ではなく、同様のものは見つかりませんでした.
カスタム クラス (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 は完璧に表示され、アニメーションは正常に動作しますが、前述のように、ViewController を再読み込みした後、UITapGesture は動作します。
これは、Ball と UIView (@kBall) をイメージするためのリンクです。画像について申し訳ありませんが、10ポイントになるまでロードできません:(