我想在屏幕上四处移动多个图像(在本例中是篮球)。
如何创建多个具有相同名称的球?我有一个已经在移动的球。因此,所有其他球都应使用具有相同属性名称的相同计算进行颠簸和类似操作。
有人能帮我吗?我很了解Java,但对Obj C却不太了解。
编辑:
ball.center = CGPointMake(ball.center.x + ballmovement.x, ball.center.y + ball movement.y);
if (ball.center.x > self.view.frame.size.width || ball.center.x < 0) {
ballmovement.x = -1* ballmovement.x;
}
if (ball.center.y > self.view.frame.size.height || ball.center.y < 0) {
ballmovement.y = -1* ballmovement.y;
这是我的简单计算。这段代码在我的循环方法中。此循环方法由我的viewDidLoad方法中的以下代码调用:
[NSTimer scheduledTimerWithTimeInterval:0.050 target:self selector:@selector(loop) userInfo:nil repeats:YES];
在MainView .h文件中,我写了
BasketballView *ball;
通过以下代码在我的按钮操作方法中创建新的Balls:
for (int x = 0; x < numberOfBalls; x++)
{
ball = [[BasketballView alloc]initWithFrame:CGRectMake(arc4random()%100,arc4random()%100, 40, 40) andCustomProperty:@"my string" andAnotherProperty:100];
[self.view addSubview:ball];
}
当我运行我的应用程序时,只有一个球在移动!您是否需要更多代码?感谢您的帮助
编辑2:
每个球现在都在移动,但是它们没有正确地移动..这就是我所做的。我创建了一个NSMuteableArray。我在我的Button动作方法中添加了以下代码:
[array addObject:ball];
之后,我在循环方法中添加了一些代码。这是我现在拥有的新循环代码:
for (int i=0; i<array.count;i++){
ball = [array objectAtIndex:i];
ball.center = CGPointMake(ball.center.x + ballmovement.x, ball.center.y + ballmovement.y);
}
if (ball.center.x > self.view.frame.size.width || ball.center.x < 0) {
ballmovement.x = -1* ballmovement.x;
}
if (ball.center.y > self.view.frame.size.height || ball.center.y < 0) {
ballmovement.y = -1* ballmovement.y;
现在,当一个球击中寄宿生时,每个球都在改变方向。因此所有其他球都像这个球一样运动。他们无视寄宿生。只有当这一个球击中了寄宿生时,他们才会改变方向。
如果我将用于球运动的代码放在for循环内,则每次其中一个球击中寄宿生时,球都会改变方向。他们像疯了似的移动着,因为每0.5秒有一个球撞到其中一个寄宿生
我认为您正在寻找的是一种添加来自同一类的对象的方法。为此,我建议您覆盖一个UIImageView
类。如果这样做,则可以向球添加自定义属性。请参见下面的示例:
在头文件中,您将具有以下内容:
#import <UIKit/UIKit.h>
@interface BasketBallView : UIImageView
@property (nonatomic, strong) NSString *customProperty;
@property (assign) NSInteger anotherProperty;
//custom init method if you want
- (id) initWithFrame:(CGRect)frame andCustomProperty:(NSString *)CustomProperty andAnotherProperty:(NSInteger)AnotherProperty;
-(void)startMovement;
@end
实现将是这样的:
#import "BasketBallView.h"
@implementation BasketBallView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setImage:[UIImage imageNamed:@"your_bb_image.png"]];
}
return self;
}
-(id) initWithFrame:(CGRect)frame andCustomProperty:(NSString *)CustomProperty andAnotherProperty:(NSInteger)AnotherProperty
{
self = [self initWithFrame:frame];
if (self) {
self.customProperty = CustomProperty;
self.anotherProperty = AnotherProperty;
}
return self;
}
-(void)startMovement{
[NSTimer scheduledTimerWithTimeInterval:0.050 target:self selector:@selector(loop) userInfo:nil repeats:YES];
}
-(void)moveBall{
self.center = CGPointMake(self.center.x + ballmovement.x, self.center.y + ballmovement.y);
if (self.center.x > self.superview.frame.size.width || self.center.x < 0) {
ballmovement.x = -1* ballmovement.x;
}
if (self.center.y > self.superview.frame.size.height || self.center.y < 0) {
ballmovement.y = -1* ballmovement.y;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
若要从视图控制器中使用和访问对象,请按照以下步骤操作:
BasketBallView *bb = [[BasketBallView alloc]initWithFrame:CGRectMake(0, 0, 100, 100) andCustomProperty:@"my string" andAnotherProperty:100];
[self.view addSubview:bb];
当然,您可以将以上代码行放入循环中,并将其多次添加到视图中。
编辑:
如果要BasketBallView
在接口生成器的视图中添加,则需要initWithCoder
在实现文件中自定义方法。在这种方法中,您将类似地设置图像等。
我建议您不要BasketBallView
通过Interface Builder添加。由于您需要随机数量的球,因此需要从代码中添加它们,并使用我上面概述的方法。您可以这样添加它们:
for(int i = 0; i < yourRandomNumber; i++){
BasketBallView *bb = [[BasketBallView alloc]initWithFrame:CGRectMake(0, 0, 100, 100) andCustomProperty:@"my string" andAnotherProperty:100];
[self.view addSubview:bb];
}
您不能将视图的副本添加到另一个视图,换句话说,您不能在IB中创建球,然后在代码中对其进行复制并将其添加到视图中。
我添加到类中的属性只是一些自定义示例,您可以根据需要将其添加到类中。
当然,您仍然可以使用center
属性,因为BasketBallView
是的子类UIImageView
,因此继承了所有UIImageView
属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句