【问题标题】:How to Make UIImageView Framesize Larger Following Collision Detection碰撞检测后如何使 UIImageView Framesize 更大
【发布时间】:2011-09-26 23:04:42
【问题描述】:

我有两个名为 image1 和 image2 的 UIImageView。我想要做的是:当 image1 和 image2 碰撞时, image1 应该变大(带有比例动画。)例如如果 image1 与 image2 碰撞十次,则 image1 应该变大 10 倍。

我尝试使用缩放动画和 CGRectIntersectsRect 进行碰撞,然后使用计时器进行碰撞,但图像只会变大一次,并且在所有后续碰撞后保持该大小。

这是我的代码:

-(void)collision {

    if(CGRectIntersectsRect(imageView.frame,centre.frame)){

        scale=scale+1;
        imageView.alpha=0.5;

        [imageView removeFromSuperview];

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0f];
        centre.transform = CGAffineTransformScale(centre.transform, scale, scale);
        [UIView commitAnimations];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(collision) userInfo:nil repeats:YES];
}

(TechZen 说——作者最初指出他的母语是法语,而且他的英语有点粗糙。)

【问题讨论】:

  • 我从上周开始尝试这样做,所以我认为这是一个问题
  • 我尝试使用缩放动画和 CGRectIntersectsRect 进行碰撞,然后使用计时器进行碰撞,但图像一次变大,然后保持相同的大小,我使用了很多东西来解决它但是没有结果,所以这就是我发布问题的原因
  • 你可以在你的问题中发布一些代码吗?
  • 我已经用我的代码编辑了我的帖子
  • @TechZen - 很抱歉你被耽搁了,但我真的很喜欢你的编辑。

标签: iphone xcode uiimageview collision-detection scale


【解决方案1】:

转换不是累积的,例如每个变换都对原始矩阵进行操作,而不是对最后一次变换返回的矩阵进行操作。这就是为什么你 image1 对象变换一次然后停留在那里的原因。这是从头开始一遍又一遍的完全相同的变换。

您需要对scale 变量进行累加或求和,而不是尝试对变换进行累加或求和。使 scale 变量成为持有该方法的控制器对象的属性,然后逐渐增加值以使 image1 更大。

类似:

-(void)collision {

    if(CGRectIntersectsRect(imageView.frame,centre.frame)){

        self.scale=self.scale+1; // scale is no longer a local value and will increment
        imageView.alpha=0.5;

        [imageView removeFromSuperview];

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0f];
        centre.transform = CGAffineTransformScale(centre.transform, scale, scale);
        [UIView commitAnimations];
    }
}

更新:

要转换局部变量,即在单个方法中定义的变量,请将定义移动到头文件 (.h),如下所示:

@implementation MyViewController:NSViewController{
//... various definitions
}
@property NSInteger *scale;

...然后在实现文件(.m)中添加合成指令:

@synthesize scale;

现在您可以使用self.scale 在类中的任何位置引用scale 属性,它将被保留。每次调用:

self.scale=self.scale + 1;

...该值将增加并保存,以便下一次碰撞可用。

每次调用collision 时,刻度值都会改变一,例如

centre.transform = CGAffineTransformScale(centre.transform, 1, 1);
...
centre.transform = CGAffineTransformScale(centre.transform, 2, 2);
....
centre.transform = CGAffineTransformScale(centre.transform, 3, 3);
//... and so on

...CGAffineTransformScale 将增加将 imageview 转换为大尺寸。

【讨论】:

  • 我不太明白“让刻度变量成为持有方法的控制器对象的属性”你能解释一下吗?
  • 有个大问题:当center与imageView发生碰撞时,imageView在一秒钟内以指数方式快速增长并消失
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多