- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想让多个图像(在本例中为篮球)在屏幕上移动。
如何创建多个具有相同名称的球?我有一个已经在四处移动的球。所以所有其他球应该使用相同的计算和相同的属性名称来进行碰撞和类似的事情。
有人可以帮助我吗?我很了解 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;
新的球是通过我的按钮操作方法中的这段代码创建的:
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。我在按钮操作方法中添加了这段代码:
[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
。类(class)。如果这样做,您可以为球添加自定义属性。请参见下面的示例:
在您的头文件中,您将拥有以下内容:
#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
然后要从您的 View Controller 使用和访问您的对象,请按以下方式进行:
BasketBallView *bb = [[BasketBallView alloc]initWithFrame:CGRectMake(0, 0, 100, 100) andCustomProperty:@"my string" andAnotherProperty:100];
[self.view addSubview:bb];
当然,您可以将上面的代码行放入循环中,然后多次将其添加到 View 中。
编辑:
如果你想添加BasketBallView
到 Interface Builder 的 View ,您需要自定义 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];
}
您不能将 View 的副本添加到另一个 View ,换句话说,您不能在 IB 中创建球,然后在代码中复制它并将其添加到您的 View 。
我添加到类中的属性只是一些自定义的示例,您可以根据需要添加到类中。
您当然仍然可以使用 center
自BasketBallView
以来的属性(property)是 UIImageView
的子类,因此继承了所有 UIImageView
属性。
关于iphone - 如何添加具有相同属性的多个 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18402074/
这是代码片段。 请说出这种用小内存存储大数据的算法是什么。 public static void main(String[] args) { long longValue = 21474836
所以我使用 imap 从 gmail 和 outlook 接收电子邮件。 Gmail 像这样编码 =?UTF-8?B?UmU6IM69zq3OvyDOtc68zrHOuc67IG5ldyBlbWFpb
很久以前就学会了 C 代码;想用 Scheme 尝试一些新的和不同的东西。我正在尝试制作一个接受两个参数并返回两者中较大者的过程,例如 (define (larger x y) (if (> x
Azure 恢复服务保管库有两个备份配置选项 - LRS 与 GRS 这是一个有关 Azure 恢复服务保管库的问题。 当其驻留区域发生故障时,如何处理启用异地冗余的恢复服务保管库?如果未为恢复服务启
说,我有以下实体: @Entity public class A { @Id @GeneratedValue private Long id; @Embedded private
我有下一个问题。 我有下一个标准: criteria.add(Restrictions.in("entity.otherEntity", getOtherEntitiesList())); 如果我的
如果这是任何类型的重复,我会提前申请,但我找不到任何可以解决我的具体问题的内容。 这是我的程序: import java.util.Random; public class CarnivalGame{
我目前正在使用golang创建一个聚合管道,在其中使用“$ or”运算符查询文档。 结果是一堆需要分组的未分组文档,这样我就可以进入下一阶段,找到两个数据集之间的交集。 然后将其用于在单独的集合中进行
是否可以在正则表达式中创建 OR 条件。 我正在尝试查找包含此类模式的文件名列表的匹配项 第一个案例 xxxxx-hello.file 或者案例二 xxxx-hello-unasigned.file
该程序只是在用户输入行数时创建菱形的形状,因此它有 6 个 for 循环; 3 个循环创建第一个三角形,3 个循环创建另一个三角形,通过这 2 个三角形和 6 个循环,我们得到了一个菱形,这是整个程序
我有一个像这样的查询字符串 www.google.com?Department=Education & Finance&Department=Health 我有这些 li 标签,它们的查询字符串是这样
我有一个带有静态构造函数的类,我用它来读取 app.config 值。如何使用不同的配置值对类进行单元测试。我正在考虑在不同的应用程序域中运行每个测试,这样我就可以为每个测试执行静态构造函数 - 但我
我正在寻找一个可以容纳多个键的容器,如果我为其中一个键值输入保留值(例如 0),它会被视为“或”搜索。 map, int > myContainer; myContainer.insert(make_
我正在为 Web 应用程序创建数据库,并正在寻找一些建议来对可能具有多种类型的单个实体进行建模,每种类型具有不同的属性。 作为示例,假设我想为“数据源”对象创建一个关系模型。所有数据源都会有一些共享属
(1) =>CREATE TABLE T1(id BIGSERIAL PRIMARY KEY, name TEXT); CREATE TABLE (2) =>INSERT INTO T1 (name)
我不确定在使用别名时如何解决不明确的列引用。 假设有两个表,a 和 b,它们都有一个 name 列。如果我加入这两个表并为结果添加别名,我不知道如何为这两个表引用 name 列。我已经尝试了一些变体,
我的查询是: select * from table where id IN (1,5,4,3,2) 我想要的与这个顺序完全相同,不是从1...5,而是从1,5,4,3,2。我怎样才能做到这一点? 最
我正在使用 C# 代码执行动态生成的 MySQL 查询。抛出异常: CREATE TABLE dump ("@employee_OID" VARCHAR(50)); "{"You have an er
我有日期 2016-03-30T23:59:59.000000+0000。我可以知道它的格式是什么吗?因为如果我使用 yyyy-MM-dd'T'HH:mm:ss.SSS,它会抛出异常 最佳答案 Sim
我有一个示例模式,它的 SQL Fiddle 如下: http://sqlfiddle.com/#!2/6816b/2 这个 fiddle 只是根据 where 子句中的条件查询示例数据库,如下所示:
我是一名优秀的程序员,十分优秀!