0

UIImageView大きくしながら上下に動かそうとしています。これらを個別に行うと、思ったように機能しますが、それらを組み合わせると、本当にグリッチに見え、私が望んでいたように見えます.

これが私のコードです。Xcode にコピーして、自分で何が起こるかを確認してください。インターフェイス ビルダーで取得したイメージを使用して を追加UIImageViewし、次のコードを viewController.h に追加します。

IBOutlet UIImageView *bg1;

int steps;

float a;

これをviewController.mに

#define kA 3

- (void)viewDidLoad {
    [super viewDidLoad];

    steps = 0;

    a = kA;

    [NSTimer scheduledTimerWithTimeInterval:.04 target:self selector:@selector(walk)
                                            userInfo:nil repeats:YES];
}

-(void)resetA { // This function makes it possible for the UIImageView to move up and down
    if (a > 0) a=-kA;
    else a=kA;

    steps++;
    NSLog(@"%i", steps);
}

-(void)walk { // This function makes the UIImageView bigger and move up and down
    if (a > 0) [bg1 setFrame:CGRectMake(0, 0, bg1.bounds.size.width+a,  
                                              bg1.bounds.size.height+a)];
    else [bg1 setFrame:CGRectMake(0, 0, bg1.bounds.size.width-a, 
                                        bg1.bounds.size.height-a)];

    [bg1 setCenter:CGPointMake(160, bg1.center.y+a)];
    a *= 0.9;

    if (a < 0.15 & a > 0 || a > -0.15 & a < 0) [self resetA];
}
4

0 に答える 0