我正在做这个项目,我试图让一个 4-4 的红色方块在屏幕上弹跳,但是每次我运行这段代码时,它都会给出一个线程错误,上面写着 Thread 1:signal SIGABRT 我对 Objective-c 来说是相当新的可以有人帮我做我想做的事吗?这是我的代码
#import <UIKit/UIKit.h>
@class home;
@interface ViewController : UIViewController{
ViewController* home;
CGFloat rhx;
CGFloat rhy;
CGFloat red_direction_x;
CGFloat red_direction_y;
}
@property CGFloat rhx;
@property CGFloat rhy;
@property CGFloat red_direction_x;
@property CGFloat red_direction_y;
@property ViewController* home;
@end
#import "ViewController.h"
@implementation ViewController
@synthesize rhx,rhy, red_direction_x, red_direction_y, home;
-(void)event:(NSTimer*)timername{
UIView* redhead = [[UIView alloc]initWithFrame:CGRectMake(rhx, rhy, 4, 4)];
redhead.backgroundColor = [UIColor redColor];
[self.view addSubview:redhead];
if (rhx >= self.view.bounds.size.width) {
red_direction_x = -4;
} else if (rhx <= 0) {
red_direction_x = 4;
}
if (rhy <= 0) {
red_direction_y = 4;
} else if (rhy >= self.view.bounds.size.height) {
red_direction_y = -4;
}
rhx += red_direction_x;
rhy += red_direction_y;
}
- (void)viewDidLoad {
self.home = [[ViewController alloc] init];
red_direction_x = 4;
red_direction_y = -4;
rhx = 4;
rhy = 4;
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(event)
userInfo:nil
repeats:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end