Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone xcode:将UIImageView子类与NSTimer一起使用_Iphone_Xcode - Fatal编程技术网

Iphone xcode:将UIImageView子类与NSTimer一起使用

Iphone xcode:将UIImageView子类与NSTimer一起使用,iphone,xcode,Iphone,Xcode,我正在创建一个程序,在这个程序中,一只鸟的图像不断地从屏幕顶部落下(就像“下雨”的鸟)。为了让每只鸟都有一个NSTimer,我创建了一个UIImageView子类(称为“BirdUIImageView”)。然而,我不确定如何正确地实现代码——把什么放在哪里,等等 以下是我在ViewController.m中的代码: #import "ViewController.h" #import "BirdUIImageView.h" @interface ViewController () @end

我正在创建一个程序,在这个程序中,一只鸟的图像不断地从屏幕顶部落下(就像“下雨”的鸟)。为了让每只鸟都有一个NSTimer,我创建了一个UIImageView子类(称为“BirdUIImageView”)。然而,我不确定如何正确地实现代码——把什么放在哪里,等等

以下是我在ViewController.m中的代码:

#import "ViewController.h"

#import "BirdUIImageView.h"

@interface ViewController ()

@end

@implementation ViewController {

    BirdUIImageView *_myImage;

}

- (void)viewDidLoad
{


    //IMAGE CREATOR TIMER
    createImagesTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(createImages) userInfo:nil repeats:YES];

}


  //CREATES AN IMAGE
-(void) createImages {  
    srand(time(NULL));
    int random_x_coordinate = rand() % 286;
    CGRect myImageRect = CGRectMake(random_x_coordinate, 0.0f, 40.0f, 40.0f);
    BirdUIImageView *myImage = [[BirdUIImageView alloc] initWithFrame:myImageRect];
    [myImage setImage:[UIImage imageNamed:@"flake.png"]];
    myImage.opaque = YES;
    [self.view addSubview:myImage];
    _myImage = myImage;

}
这是我在BirdUIImageView.m中的代码。我完全不知道该在这个文件中做什么,但我尝试了:

#import "BirdUIImageView.h"



@implementation BirdUIImageView  


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)viewDidLoad
{
    //FALLING BIRDS TIMER
    moveObjectTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(moveObject) userInfo:nil repeats:YES];
}

//FALLING BIRDS MOVER
-(void) moveObject {

    _myImage.center = CGPointMake(_myImage.center.x, _myImage.center.y +1);

}

首先,从
BirdUIImageView
类中删除
viewDidLoad
moveObject
方法,然后在
ViewController.m
类上尝试下面的代码。您可以随意使用计时器设置,以达到所需的效果:

ViewController.m上

- (void)viewDidLoad {
    [super viewDidLoad];
    createImagesTimer = [NSTimer scheduledTimerWithTimeInterval:2.5
                                                         target:self
                                                       selector:@selector(createImages)
                                                       userInfo:nil
                                                        repeats:YES];
}


  //CREATES AN IMAGE
-(void) createImages {
    srand(time(NULL));
    int random_x_coordinate = rand() % 286;
    CGRect myImageRect = CGRectMake(random_x_coordinate, 0.0f, 40.0f, 40.0f);
    BirdUIImageView *myImage = [[BirdUIImageView alloc] initWithFrame:myImageRect];
    [myImage setImage:[UIImage imageNamed:@"flake.png"]];
    myImage.opaque = YES;
    [self.view addSubview:myImage];
    _myImage = myImage;

    [self move];
}


-(void)move {

    //FALLING BIRDS TIMER
    moveObjectTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(moveObject) userInfo:nil repeats:YES];


}


//FALLING BIRDS MOVER
-(void) moveObject {

    _myImage.center = CGPointMake(_myImage.center.x, _myImage.center.y +1);

}

有很多标准方法可以很好地设置视图位置的动画,不要使用NSTimer。如果我使用这些动画方法,我能在以后的某个时候回忆起每个落鸟图像的位置吗?(这最终将是一个游戏,我必须“抓住”篮子图像中的对象,因此我需要回忆位置)。我的印象是,我必须把他们一个位置一个位置地移动,才能让他们工作啊,非常感谢你的帮助!终于开始工作了,没问题。我真的很高兴能帮上忙:-)