[英]xcode: Using a UIImageView subclass with NSTimers
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").
为了让每只鸟都有一个 NSTimer,我创建了一个 UIImageView 子类(称为“BirdUIImageView”)。 However, I'm not sure how to implement the code correctly--where to put what, etc.
但是,我不确定如何正确实现代码——在哪里放什么等等。
Here is the code I have in ViewController.m:这是我在 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;
}
And here is the code I have in BirdUIImageView.m.这是我在 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);
}
First, remove the viewDidLoad
and moveObject
methods from your BirdUIImageView
class, and then try this code below on your ViewController.m
class.首先,从您的
BirdUIImageView
类中删除viewDidLoad
和moveObject
方法,然后在您的ViewController.m
类上尝试以下代码。 You can play around with the timers settings, to have your desired effect:您可以使用计时器设置,以获得您想要的效果:
On 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.