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