【问题标题】:Collision of 2 imageView ios2 imageView ios的碰撞
【发布时间】:2014-09-25 11:55:13
【问题描述】:

我有 2 张图像,我需要正确地碰撞它们。我尝试了许多解决方案,但无法正确碰撞它们。

这是图片

图片1

图片2

我尝试了 CGRectIntersectsRect(),但它无法正常工作,因为由于框架问题,图像在实际相遇之前就发生了碰撞。请帮帮我。

我也尝试使用 Sprite Kit,但对我有用。

编辑 1:

试过这段代码,但它不工作或很慢:

- (void) checkIntersectionOfRect1 :(CGRect) frame1 Rect2 : (CGRect) frame2 {

    CGAffineTransform cg1 = CGAffineTransformIdentity;
    CGMutablePathRef *cgPath1 = CGPathCreateWithRect(frame1,&cg1);

    CGAffineTransform cg2 = CGAffineTransformIdentity;
    CGMutablePathRef *cgPath2 = CGPathCreateWithRect(frame2,&cg1);

    ANPathBitmap * bm1 = [[ANPathBitmap alloc] initWithPath:cgPath1];
    ANPathBitmap * bm2 = [[ANPathBitmap alloc] initWithPath:cgPath2];

    [bm1 generateBitmap];
    [bm2 generateBitmap];

    ANPathIntersection * intersection = [[ANPathIntersection alloc] initWithPathBitmap:bm1                                                                        anotherPath:bm2];

    if ([intersection pathLinesIntersect:&CGPointZero]) {
        NSLog(@"intersect");
        [self endGame];
    }
    else {
        NSLog(@"No intersection");
    }
}

请人帮忙。

【问题讨论】:

  • 感谢您解释得很好,请查看下面的答案!

标签: ios xcode ios7 sprite-kit cgrectintersectsrect


【解决方案1】:

这是两个图像碰撞的例子,crow1 和 crow2 是两个 uiimage,它们会相互碰撞。

class GameViewController: UIViewController {

    var dynamicAnimator : UIDynamicAnimator!
    var collisionBehavior : UICollisionBehavior!
    var gravityBeehavior : UIGravityBehavior!

    @IBOutlet weak var aeroplane: UIImageView!
    @IBOutlet weak var crow1: UIImageView!
    @IBOutlet weak var crow2: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()




        dynamicAnimator = UIDynamicAnimator(referenceView: self.view)

        gravityBeehavior = UIGravityBehavior(items: [crow2])
        dynamicAnimator.addBehavior(gravityBeehavior)

        collisionBehavior = UICollisionBehavior(items: [crow1, crow2])
        collisionBehavior.translatesReferenceBoundsIntoBoundary = true
        dynamicAnimator.addBehavior(collisionBehavior)
}

【讨论】:

    【解决方案2】:

    嗯,我不确定,但是。我检查了您的图像,并将它们下载到我的 Mac 上。 它们是透明的,因此在您的实际透明图像之前的任何空间都看不到,并且如果碰到将触发碰撞的图像。

    Step 1:
    

    可以肯定的是,将图像视图的背景颜色设置为像蓝色这样的醒目颜色,然后查看问题发生在哪里。然后你需要使用预览并切掉一些空间,直到它精确到边缘点。

    Step 2 :
    

    或者将图像视图的视图更改为重绘。这将使您的图像适合您的图像视图。你的代码对我来说看起来不错。

    Step 3 :
    

    或者您可以使用 NSTimer 并将时间设置为 0.0001,然后进行 if 语句。

    //Try my methods, I'm pretty sure it  is not to do with your code but I am not a professional and maybe it is. You never know. 
    

    你会知道,你的代码很复杂,因为你可以用更简单的方法来完成它。如果您有任何疑问,请询问!谢谢!

    【讨论】:

      猜你喜欢
      • 2014-09-15
      • 2018-08-23
      • 1970-01-01
      • 2016-10-26
      • 2015-10-11
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多