xcode:将UIImageView子类与NSTimers一起使用 [英] xcode: Using a UIImageView subclass with NSTimers

查看:37
本文介绍了xcode:将UIImageView子类与NSTimers一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个程序,其中鸟类的图像连续从屏幕顶部掉落(就像雨中的"鸟类一样).为了让每只鸟都有一个NSTimer,我制作了一个UIImageView子类(称为"BirdUIImageView").但是,我不确定如何正确地执行代码-在哪里放置东西,等等.

I'm creating a program where an image of a bird continuously falls from the top of the screen (like "raining" birds). In order to have an NSTimer for each individual bird, I made a UIImageView subclass (called "BirdUIImageView"). However, I'm not sure how to implement the code correctly--where to put what, etc.

这是ViewController.m中的代码:

Here is the code I have in 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中拥有的代码.我对此文件的处理完全不知所措,但是我尝试了:

And here is the code I have in BirdUIImageView.m. I'm totally at a loss as to what to do in this file, but I made an attempt:

#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 类上尝试以下代码.您可以使用定时器设置来发挥所需的效果:

First, remove the viewDidLoad and moveObject methods from your BirdUIImageView class, and then try this code below on your ViewController.m class. You can play around with the timers settings, to have your desired effect:

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

}

这篇关于xcode:将UIImageView子类与NSTimers一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆