iPhone代码 - CGPoint分配问题

dan*_*_au 2 iphone objective-c

我试图实现这个移动UIImageView对象的代码,
我在createPosition方法中得到了对象(jumpBall1 ...)的编译器警告:UIImageView may not respont to -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)];
  ...
}
Run Code Online (Sandbox Code Playgroud)

mik*_*csh 5

您的代码试图在已声明为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)];     
...     
}     
Run Code Online (Sandbox Code Playgroud)