iPhone代码-CGPoint分配问题

标签 iphone objective-c

我尝试实现移动 UIImageView 对象的代码,
我在 createPosition 方法中收到有关对象 (jumpBall1...) 的编译器警告:UIImageView 可能无法响应 -setupperLimit、-setlowerLimit、-setspeed

代码:

@interface JumpBallClass : UIViewController
{
    CGPoint center;
    CGPoint speed;

    CGPoint lowerLimit;
    CGPoint upperLimit;

    IBOutlet UIImageView *jumpBall1;
    IBOutlet UIImageView *jumpBall2;
}

@property (assign) CGPoint lowerLimit;
@property (assign) CGPoint upperLimit;
@property (assign) CGPoint speed;
@property(nonatomic,retain) IBOutlet UIImageView *jumpBall1;
@property(nonatomic,retain) IBOutlet UIImageView *jumpBall2;


- (void)update;

@end

@implementation JumpBallClass
- (void)update
{
    center.x += speed.x;
    center.y += speed.y;

    if (center.x > upperLimit.x || center.x < lowerLimit.x)
    { speed.x = -speed.x; }
    if (center.y > upperLimit.y || center.y < lowerLimit.y)
    { speed.y = -speed.y; }
}
@end

- (void) jumpOnTimer {          

 NSArray * balls = [NSArray arrayWithObjects:jumpBall1, jumpBall2, nil];

 [balls makeObjectsPerformSelector:@selector(update)];
}

- (void) createPosition { 
  [jumpBall1 setUpperLimit:CGPointMake(60, 211)];
  [jumpBall1 setLowerLimit:CGPointMake(0, 82)];
  [jumpBall1 setspeed:CGPointMake(2.0,7.0)];
  ...
}

最佳答案

您的代码正在尝试调用您已声明为 UIImageView 的成员变量 JumpBall1 上的方法 setUpperLimit: setLowerLimit: 和 setspeed: 。然而,这些不是 UIImageView 的方法 - 它们是您在自己的自定义 JumpBallClass 中声明的 @properties 的 setter 方法。但是,您尚未在实现文件中指定这些属性的综合(使用 @synthesize 关键字)。

也许您想要做的是使 JumpBallClass 成为 UIImageView 的子类,并实例化它的两个实例,一个实例对应于您想要控制的每个 JumpBall。

@interface JumpBallClass : UIImageView
{ 
CGPoint center; 
CGPoint speed; 

CGPoint lowerLimit; 
CGPoint upperLimit; 

IBOutlet UIImageView *jumpBallView; 
} 

@property (assign) CGPoint lowerLimit; 
@property (assign) CGPoint upperLimit; 
@property (assign) CGPoint speed; 
@property(nonatomic,retain) UIImageView *jumpBallView; 

- (void)update; 

@end 

@implementation JumpBallClass
@synthesize lowerLimit, upperLimit, speed, jumpBallView;

- (void)update 
{ 
    center.x += speed.x; 
    center.y += speed.y; 

    if (center.x > upperLimit.x || center.x < lowerLimit.x) 
    { speed.x = -speed.x; } 
    if (center.y > upperLimit.y || center.y < lowerLimit.y) 
    { speed.y = -speed.y; } 
} 
@end 

//assuming jumpBall1&2 have beeen declared like so:
//JumpBallClass *jumpBall1=[[JumpBallClass init] alloc];
//JumpBallClass *jumpBall2=[[JumpBallClass init] alloc];

- (void) jumpOnTimer {               

NSArray * balls = [NSArray arrayWithObjects:jumpBall1, jumpBall2, nil];     

[balls makeObjectsPerformSelector:@selector(update)];     
}     

- (void) createPosition {      
[jumpBall1 setUpperLimit:CGPointMake(60, 211)];     
[jumpBall1 setLowerLimit:CGPointMake(0, 82)];     
[jumpBall1 setspeed:CGPointMake(2.0,7.0)];     
...     
}     

关于iPhone代码-CGPoint分配问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1999184/

相关文章:

iphone - 使用 SudzC 传递参数

ios - NSDateFormatter 日期从字符串 : returns nil on specific device

ios - 如何防止 Xcode/Interface Builder 自动向下移动 View ?

objective-c - OpenGL 绘图模型顺序不正确

objective-c - 没有互联网时位置管理器崩溃

iphone - 自定义日期选择器 : How to change width & height of datepicker in iphone?

iphone - 从 subview 导航 Controller 访问顶部导航 Controller

c++ - objective-c 为什么 .mm 文件被认为是 int* 类型?

ios - 当我有多个 iOS 部分时从 UITableView 中删除行(值在 Dynamic NSMutableDictionary 中)

ios - indexPath.row 在 tableView 自定义单元格 iOS 中没有给出正确的值