iphone - xcode:将 UIImageView 子类与 NSTimers 一起使用

标签 iphone xcode

我正在创建一个程序,其中一只鸟的图像不断从屏幕顶部落下(就像“下雨”的鸟一样)。为了让每只鸟都有一个 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);

}

最佳答案

首先,删除 viewDidLoadmoveObject来自您BirdUIImageView的方法类,然后在您的 ViewController.m 上尝试下面的代码类(class)。您可以调整计时器设置,以获得您想要的效果:

关于 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);

}

关于iphone - xcode:将 UIImageView 子类与 NSTimers 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13589392/

相关文章:

Swift:在进度 View 完成时更改标签文本 1.0

iphone - 我的 UIViewController 的中心属性是 "lying"

ios - 是否可以在 dismissmodelviewcontroller 时返回一个值

iphone - 使用 CALayer 创建 .PNG 的笔触/轮廓?

iphone - 自定义 UITabBarController View Controller 和 View 的问题

html - 如何从 xcode 项目外部查看 HTML

android - 使用应用程序的相关数据创建新闻源

ios - 无法将 NSString 传递给 NSURL URLWithString

ios - 在 XCode 中导出文件中的所有警告

ios - Xcode iOS-带计时器的本地通知