UIimage不会继续按下,我做错了什么?

时间:2014-02-07 15:11:24

标签: ios iphone objective-c ipad

我正在尝试移动UIImage,第一次按下按钮创建图像,第二次按下移动它。

只需按下按钮即可存在图像。

在模拟器中,它会创建按钮并将其放置,第二次单击时不会执行任何操作。

这是我的代码

- (IBAction) btn:(id)sender {

    UIImageView *myImage = [[UIImageView alloc] init];

    myImage.image = [UIImage imageNamed:@"keyframe"];

    if (startUp == 1){

        //Create Image and add to view
        myImage.frame = CGRectMake(200, 300, 10, 10);
        myImage.image = [UIImage imageNamed:@"keyframe"];
        [self.view addSubview:myImage];

        //Set startUp to 0 and output rect value
        startUp = 0;
        NSLog(@"currentFrame %@", NSStringFromCGRect(myImage.frame));

    }else if (startUp == 0){

        //Change position, size and log to debug
        myImage.frame = CGRectMake(500,100 ,20, 20);
        NSLog(@"newFrame %@", NSStringFromCGRect(myImage.frame));
    }
}

如何以编程方式移动以编程方式添加的UIimage? 我尝试更改中心值,但这也不起作用。

1 个答案:

答案 0 :(得分:0)

尝试类似这样的测试和工作样本:

#import "ViewController.h"

@interface ViewController () {
    BOOL startUp;

    UIImageView *myImage;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    startUp = YES;
}

- (IBAction)doWork:(id)sender {
    if (startUp) {
        UIImage *img = [UIImage imageNamed: @"keyframe"];
        myImage = [[UIImageView alloc] initWithImage: img];

        [myImage sizeToFit];

        [myImage setCenter: CGPointMake(200, 300)];

        [self.view addSubview: myImage];

        startUp = NO;
    } else {
        [myImage setCenter: CGPointMake(400, 500)];
    }
}

@end