addBoundary withIdentifier не работает в пользовательском UIView - PullRequest
0 голосов
/ 28 января 2020

Мое пользовательское столкновение не работает для BrickView. Как это исправить? Кирпичи падают на экране, а затем останавливаются в нижней части моего просмотра игры. Ниже 2 картинки. 1) Это стартовая позиция моих кирпичей 2) Это примерно через 5 секунд. Мои кирпичи падают на UIGravityBehavior и сталкиваются на UICollisionBehavior. Все мои предметы добавляются в UIDynamicAnimator и с UIDynamicItemCollisionBoundsType.rectangle все работает нормально.

    override var collisionBoundsType: UIDynamicItemCollisionBoundsType{
        return .path
    }

    override var collisionBoundingPath: UIBezierPath{
        let path = BrickView.getPathForBrickType(type: self.brick, and: self.bounds.size)
        path.apply(CGAffineTransform(translationX: -self.bounds.size.width*0.5, y: -self.bounds.size.height*0.5))
        return path
    }

    static func getPathForBrickType(type: BrickView.BricksType ,and size: CGSize) -> UIBezierPath{
        let safeConst = CGFloat(3)
        let path = UIBezierPath()
        path.move(to: CGPoint(x:safeConst,y:safeConst))
        let width = size.width
        let height = size.height
        switch type {
        case .I:
            path.addLine(to: CGPoint(x: width - safeConst,      y: safeConst))
            path.addLine(to: CGPoint(x: width - safeConst,      y: height - safeConst))
            path.addLine(to: CGPoint(x: safeConst,              y: height - safeConst))
            path.close()
            return path
        case .J:
            path.addLine(to: CGPoint(x: width - safeConst,      y: safeConst))
            path.addLine(to: CGPoint(x: width - safeConst,      y: height - safeConst))
            path.addLine(to: CGPoint(x: width*2/3 + safeConst,  y: height - safeConst))
            path.addLine(to: CGPoint(x: width*2/3 + safeConst,  y: height/2 - safeConst))
            path.addLine(to: CGPoint(x: safeConst,              y: height/2 - safeConst))
            path.close()
            return path
        case .L:
            path.addLine(to: CGPoint(x: width - safeConst,      y: safeConst))
            path.addLine(to: CGPoint(x: width - safeConst,      y: height/2 - safeConst))
            path.addLine(to: CGPoint(x: width/3 - safeConst,    y: height/2 - safeConst))
            path.addLine(to: CGPoint(x: width/3 - safeConst,    y: height - safeConst))
            path.addLine(to: CGPoint(x: safeConst,              y: height - safeConst))
            path.close()
            return path
        case .O:
            path.addLine(to: CGPoint(x: width - safeConst,      y: safeConst))
            path.addLine(to: CGPoint(x: width - safeConst,      y: height - safeConst))
            path.addLine(to: CGPoint(x: safeConst,              y: height - safeConst))
            path.close()
            return path
        case .S:
            path.move   (to: CGPoint(x: safeConst,              y: height/2 - safeConst))
            path.addLine(to: CGPoint(x: width/3 + safeConst,    y: height/2 - safeConst))
            path.addLine(to: CGPoint(x: width/3 + safeConst,    y: safeConst))
            path.addLine(to: CGPoint(x: width - safeConst,      y: safeConst))
            path.addLine(to: CGPoint(x: width - safeConst,      y: height/2 - safeConst))
            path.addLine(to: CGPoint(x: width*2/3 - safeConst,  y: height/2 - safeConst))
            path.addLine(to: CGPoint(x: width*2/3 - safeConst,  y: height - safeConst))
            path.addLine(to: CGPoint(x: safeConst,              y: height - safeConst))
            path.close()
            return path
        case .T:
            path.addLine(to: CGPoint(x: width - safeConst,      y: safeConst))
            path.addLine(to: CGPoint(x: width - safeConst,      y: height/2 - safeConst))
            path.addLine(to: CGPoint(x: width*2/3 - safeConst,  y: height/2 - safeConst))
            path.addLine(to: CGPoint(x: width*2/3 - safeConst,  y: height - safeConst))
            path.addLine(to: CGPoint(x: width*1/3 + safeConst,  y: height - safeConst))
            path.addLine(to: CGPoint(x: width*1/3 + safeConst,  y: height - safeConst))
            path.addLine(to: CGPoint(x: width*1/3 + safeConst,  y: height/2 - safeConst))
            path.addLine(to: CGPoint(x: safeConst,              y: height/2 - safeConst))
            path.close()
            return path
        case .Z:
            path.addLine(to: CGPoint(x: width*2/3 - safeConst,  y: safeConst))
            path.addLine(to: CGPoint(x: width*2/3 - safeConst,  y: height/2 + safeConst))
            path.addLine(to: CGPoint(x: width - safeConst,      y: height/2 + safeConst))
            path.addLine(to: CGPoint(x: width - safeConst,      y: height - safeConst))
            path.addLine(to: CGPoint(x: width/3 + safeConst,    y: height - safeConst))
            path.addLine(to: CGPoint(x: width/3 + safeConst,    y: height/2 - safeConst))
            path.addLine(to: CGPoint(x: safeConst,              y: height/2 - safeConst))
            path.close()
            return path
        }
    }

    enum BricksType : String{
        case I
        case J
        case L
        case O
        case S
        case T
        case Z
    }
}

начальная позиция для кирпичей: enter image description here

вот так они сталкиваются на мой взгляд: enter image description here

...