カスタム ビューで作成した円の動きをアニメーション化する方法を学習しようとしています。ただし、startMovingCircle のループ中は円は表示されず、ループが終了したときにのみ表示されます。ビューを再描画するたびにsetNeedsDisplayを呼び出すと、動く円のアニメーションが表示されるはずだと思いました。誰かが私が間違っていることを教えてもらえますか?
また、この方法でアニメーションをプログラムするのが正しいのか、それとも CoreAnimation を使用する必要があるのか、その利点は何ですか? CoreAnimation は、CoreGraphics でプログラムされた円、楕円、四角形などの形状をアニメーション化できますか?
MyDrawingView.m:
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
xCoordinate = 100.0;
yCoordinate = 100.0;
speed = 1.0;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
NSLog(@"Drawing");
CGContextRef myContext = UIGraphicsGetCurrentContext();
CGContextBeginPath(myContext);
CGContextAddArc(myContext, xCoordinate, yCoordinate, 20.0, 0.0, 2 * M_PI, 0);
CGContextClosePath(myContext);
CGContextSetLineWidth(myContext, 1);
CGContextSetStrokeColorWithColor(myContext, [UIColor whiteColor].CGColor);
CGContextStrokePath(myContext);
}
-(void)moveCircle{
xCoordinate += speed;
yCoordinate += speed;
NSLog(@"Moving circle. X: %f, Y: %f", xCoordinate, yCoordinate);
[self setNeedsDisplay];
}
MyDrawingViewController.m:
-(void)startMovingCircle{
for(int i = 0; i < 1000; i++){
[myView moveCircle];
}
}