添付のコードに 2 つの問題があります。
まず、シェイプのアニメーション期間を 1 秒に設定して、シェイプが常に動いているようにすると、touchesBegan は起動しません。調べてみると設定されていないC4View.m
ようですUIViewAnimationOptionAllowUserInteraction
。にアクセスしてUIView
このオプションを設定する方法はありますか? 書き直さないと難しそうですC4View
。
第二に、形状がヒットするたびに、少し収縮させたい. 私が抱えている問題は、最初に叩いた後、動かなくなることです。掘り下げたC4Shape
後、新しいフレームで再構築された後、形状が失われているようです. 以前のコンソール情報は次のとおりです。
[C4Log] <C4Shape: 0x7b63820; baseClass = UIControl;
frame = (263 691; 200 200);
animations = { position=<CABasicAnimation: 0x1082c490>;
animateFillColor=<CABasicAnimation: 0x8a62c00>;
animateLineDashPhase=<CABasicAnimation: 0x8a64920>;
animateStrokeColor=<CABasicAnimation: 0x8a64ef0>;
animateStrokeEnd=<CABasicAnimation: 0x8a65190>;
animateStrokeStart=<CABasicAnimation: 0x8a65430>; };
layer = <C4ShapeLayer: 0x7b63d00>>
以降:
[C4Log] <C4Shape: 0x1082d3a0; baseClass = UIControl;
frame = (204 17; 180 180);
layer = <C4ShapeLayer: 0x1082d4e0>>
最初に形状を nil に設定するか、再定義後にアニメーションをやり直しても、問題は解決しないようです。
コード
//
// C4WorkSpace.m
// touchgame
//
// Created by Adam Tindale on 2013-09-28.
//
#import "C4WorkSpace.h"
@implementation C4WorkSpace
{
C4Shape * shape;
C4Timer * timer;
C4Label * text;
C4Font * font;
int score;
}
-(void)setup
{
score = 0;
shape = [C4Shape ellipse:CGRectMake(self.canvas.center.x, self.canvas.center.y, 200, 200)];
font = [C4Font fontWithName:@"Chalkduster" size:40];
text = [C4Label labelWithText:@"Score : 0" font:font];
[shape setAnimationDuration:0.05];
timer = [C4Timer automaticTimerWithInterval:1.0 target:self method:@"runaway" repeats:YES];
[self listenFor:@"touchesBegan" fromObject:shape andRunMethod:@"imhit"];
[self.canvas addSubview:text];
[self.canvas addSubview:shape];
}
-(void) runaway
{
[shape setCenter:CGPointMake([C4Math randomIntBetweenA:0 andB:self.canvas.width], [C4Math randomIntBetweenA:0 andB:self.canvas.height])];
C4Log(@"%@",shape);
}
-(void) imhit
{
[text setText:[NSString stringWithFormat:@"Score: %d",++score]];
[text sizeToFit];
CGRect r = shape.frame;
r.size = CGSizeMake(shape.size.width * 0.9, shape.size.height * 0.9);
shape = [C4Shape ellipse:r];
}
@end