im 尝试创建一个从顶部下降图像的基本应用程序。但是,我似乎遇到了这个问题:
Array subscript is not integer
在我的 .m 中的这一行上:
image.center = CGPointMake[image.center.x+pos.x, image.center.y+pos.y];
这是我的完整 .m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(IBAction)Start {
timer = [NSTimer scheduledTimerWithTimeInterval:(0.10)
target: self
selector:@selector(onTimer)
userInfo: nil repeats: YES];
}
-(void)onTimer {
image.center = CGPointMake[image.center.x+pos.x, image.center.y+pos.y];
pos = CGPointMake(0.0, 0.17);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我的.h文件是:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
CGPoint pos;
IBOutlet UIImageView *image;
NSTimer *timer;
}
-(IBAction)Start;
@end
另外,您能告诉我如何使图像在应用程序中的随机位置出现和落下吗?
将[
替换为(
,将]
替换为)
在
CGPointMake[image.center.x+pos.x, image.center.y+pos.y];
因为
CGPointMake
是一个函数和函数调用CGPointMake(...)
它总是使用括号而不是括号。
CGPointMake 是一个 C 函数。
它接受 2 个 CGFloats 作为参数,并返回一个 CGPoint。
调用 C 函数的语法是
result = function(param, param);
函数名称后应跟一个左括号、0 个或多个参数的逗号分隔列表和一个右括号。
正如其他人已经指出的那样,你使用的是方括号而不是括号。