私はいくつかの簡単なアニメーションを試しており、基本的な弾むボールのチュートリアルに従っています。
画面全体ではなく、独自の座標セットを使用してボールが跳ねる場所を制限するにはどうすればよいですか? チュートリアルから使用しているコードは、画面全体からランダムに座標を選択します。その座標を設定して、画面中央の小さな正方形でのみバウンスし、外に出ないようにしたいと思いますそれらの境界の。
.m
int ballx, bally;
///////
ballx = arc4random() % 320;
bally = arc4random() % 480;
//////////
-(void)movetheball {
[UIView beginAnimations: @"MovingTheBallAround" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 1.0];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
myball.frame = CGRectMake(ballx, bally,myball.frame.size.width,myball.frame.size.height);
[UIView commitAnimations];
}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(SEL)aSelector {
if (finished) {
// set new coordinate for ballx and bally
ballx = arc4random() % 320;
bally = arc4random() % 480;
[self movetheball];
}
私はSOを見てきましたが、以下を除いて似たようなものは見つかりませんでした:
ballx.center = CGPointMake(320/2, [self randNumBetween:-50:-100]);
私はあまり成功せずに適応しようとしました。私はプログラミングの経験が豊富ではないので、そのコードでベースから外れているかどうかはわかりません