如何在调整大小时移动UIImageView?

时间:2013-03-08 06:23:57

标签: iphone objective-c cocoa-touch uikit core-graphics

我试图让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];
}

0 个答案:

没有答案