使用在另一个块中声明的UIImageView变量

时间:2012-11-27 00:15:48

标签: iphone objective-c

我正在尝试回忆一下我在另一个区块(void)viewDidLoad(void)moveObject加载的图片。但是,因为我正在处理两个不同的块,我显然无法回想起这个图像。有没有什么方法可以重新安排代码才能使其正常工作?

- (void)viewDidLoad {
    CGRect myImageRect = CGRectMake(200.0f, 350.0f, 52.0f, 100.0f);
    UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
    [myImage setImage:[UIImage imageNamed:@"flake.png"]];
    myImage.opaque = YES;
    [self.view addSubview:myImage];

    moveObjectTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(moveObject) userInfo:nil repeats:YES];
}

-(void) moveObject { 
    myImage.center = CGPointMake(myImage.center.x, myImage.center.y +1);
}

1 个答案:

答案 0 :(得分:0)

为myImage或属性创建一个实例变量,如果你也喜欢getter和setter。

这里只是一个实例变量:

的.m

@implementation MyClass {

    UIImageView *_myImage;

}

- (void)viewDidLoad
{
    CGRect myImageRect = CGRectMake(200.0f, 350.0f, 52.0f, 100.0f);
    UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
    [myImage setImage:[UIImage imageNamed:@"flake.png"]];
    myImage.opaque = YES;
    [self.view addSubview:myImage];
    _myImage = myImage;
}

-(void) moveObject { 
    _myImage.center = CGPointMake(_myImage.center.x, _myImage.center.y +1);