毫无意义的 Sigabart 错误

Sigabart Error that makes no sense

我的代码是:

   import UIKit

class BreakOutViewController: UIViewController, UICollisionBehaviorDelegate
{
    var blockArray: [Block] = []
    var allViewsArray: [UIView] = []
    var screenWidth = (Float)(UIScreen.mainScreen().bounds.width)
    var screenHight = (Float)(UIScreen.mainScreen().bounds.height)
    var blockCount = 0
    var dynamicAnimator = UIDynamicAnimator()
    var ballArray : [UIView] = []
    var collisionBehavior = UICollisionBehavior()

//    var paddle = Block(frame: CGRect(x: 140, y: 645, width: 150, height: 10))
//    var ball = Block(frame: CGRect(x: 197, y: 614, width: 30, height: 30))
    var paddle = UIView()
    var ball = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()

       dynamicAnimator = UIDynamicAnimator(referenceView: view)

        createBlocks()
        createPaddle()
        createBall()
        giveDynamicProperties()
     }

    func createBlocks()
    {
        var xPosition = 10
        var yPosition = 20
        let blockWidth = (Int(screenWidth - 55 ) / 5)
        let blockHeight = 20

        for rows in 1...10
        {
            for columns in 1...7
            {
                var drand1 = CGFloat(drand48())
                var drand2 = CGFloat(drand48())
                var drand3 = CGFloat(drand48())
                var block = Block(frame: CGRect(x: xPosition, y: yPosition, width: blockWidth, height: blockHeight))
                view.backgroundColor = UIColor.cyanColor()
                block.backgroundColor = UIColor(red: drand1, green: drand2, blue: drand3, alpha: 1)
                view.addSubview(block)
                allViewsArray.append(block)
                blockArray.append(block)
                ++blockCount
                xPosition += (blockWidth + 10)
            }


            xPosition = 10
            yPosition += (blockHeight + 10)
        }
    }


    func createPaddle()
    {
        var paddle = Block(frame: CGRect(x: 140, y: 645, width: 150, height: 10))
        var drand11 = CGFloat(drand48())
        var drand12 = CGFloat(drand48())
        var drand13 = CGFloat(drand48())
        paddle.backgroundColor = UIColor(red: drand11, green: drand12, blue: drand13, alpha: 1)
        view.addSubview(paddle)
        allViewsArray.append(paddle)
    }


    func createBall()
    {
        var ball = Block(frame: CGRect(x: 197, y: 614, width: 30, height: 30))
        ball.layer.cornerRadius = 15
        var drand21 = CGFloat(drand48())
        var drand22 = CGFloat(drand48())
        var drand23 = CGFloat(drand48())
        ball.backgroundColor = UIColor(red: drand21, green: drand22, blue: drand23, alpha: 1)
        view.addSubview(ball)
        allViewsArray.append(ball)
           }        

    func giveDynamicProperties()
    {

        let dynamicItemBehavoir = UIDynamicItemBehavior(items: blockArray)
        dynamicItemBehavoir.elasticity = 1.0
        dynamicItemBehavoir.density = 100000
        dynamicItemBehavoir.friction = 111111110
        dynamicItemBehavoir.resistance = 1111110
        dynamicItemBehavoir.allowsRotation = false
        dynamicAnimator.addBehavior(dynamicItemBehavoir)

        let dynamicItemBehavoir2 = UIDynamicItemBehavior(items: [paddle] )
        dynamicItemBehavoir2.elasticity = 1.0
        dynamicItemBehavoir2.density = 100000
        dynamicItemBehavoir2.friction = 111111110
        dynamicItemBehavoir2.resistance = 1111110
        dynamicItemBehavoir2.charge = 3.0
        dynamicItemBehavoir2.allowsRotation = false
        dynamicAnimator.addBehavior(dynamicItemBehavoir2)

        let dynamicItemBehavoir3 = UIDynamicItemBehavior(items: [ball] )
        dynamicItemBehavoir3.elasticity = 1.0
        dynamicItemBehavoir3.resistance = 0
        dynamicItemBehavoir3.allowsRotation = false
        dynamicAnimator.addBehavior(dynamicItemBehavoir3)


        let pushBehavior = UIPushBehavior(items: [ball], mode: .Instantaneous)
        pushBehavior.magnitude = 5
        pushBehavior.pushDirection = CGVectorMake(-1.0 , 0.9 )
        dynamicAnimator.addBehavior(pushBehavior)

        collisionBehavior.translatesReferenceBoundsIntoBoundary = true
        collisionBehavior = UICollisionBehavior(items: allViewsArray)
          collisionBehavior.collisionDelegate = self
//        collisionBehavior.addBoundaryWithIdentifier("verticalMin", fromPoint: CGPointMake(0, 0), toPoint: CGPointMake(0, CGRectGetHeight(view.frame)))
//        collisionBehavior.addBoundaryWithIdentifier("verticalMax", fromPoint: CGPointMake(CGRectGetMaxX(view.frame), 0), toPoint: CGPointMake(CGRectGetMaxX(view.frame), CGRectGetHeight(view.frame)))
//        collisionBehavior.addBoundaryWithIdentifier("horizontalMin", fromPoint: CGPointMake(0, CGRectGetMaxY(view.frame)), toPoint: CGPointMake(CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame)))
//        collisionBehavior.addBoundaryWithIdentifier("horizontalMax", fromPoint: CGPointMake(0, 0), toPoint: CGPointMake(CGRectGetMaxX(view.frame), 0))
        collisionBehavior.collisionMode = UICollisionBehaviorMode.Everything

        dynamicAnimator.addBehavior(collisionBehavior)       
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    @IBAction func pan(sender: UIPanGestureRecognizer)
    {            
        paddle.center = CGPointMake(sender.locationInView(view).x, paddle.center.y)
        dynamicAnimator.updateItemUsingCurrentState(paddle)

        //remove all beahavoris for dynamic animator
        //remove from subview
    }

    func collisionBehavior(behavior: UICollisionBehavior, beganContactForItem item1: UIDynamicItem, withItem item2: UIDynamicItem)
    {

        for block in blockArray
        {
            if item1.isEqual(Block) && item2.isEqual(ball)
            {
                //                 block dissappear
                block.removeFromSuperview()
                dynamicAnimator.updateItemUsingCurrentState(blockArray.removeAtIndex(0))
                //     collisionBehavior.removeItem(block)
                --blockCount
                print(blockCount)
            }                
            dynamicAnimator.updateItemUsingCurrentState(block)
        }
    }    
}

在输出中,我得到:

2016-04-12 10:25:48.321 Dynamic Animator[8732:289665] *** Assertion failure in -[UIDynamicAnimator _registerBodyForItem:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UIDynamicAnimator.m:872
2016-04-12 10:25:48.343 Dynamic Animator[8732:289665] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid size {0, 0} for item <UIView: 0x7fdab1d34010; frame = (0 0; 0 0); layer = <CALayer: 0x7fdab1d34180>> in Dynamics'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000108f77e65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010aedfdeb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000108f77cca +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x00000001095e84de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
    4   UIKit                               0x000000010a3ffaca -[UIDynamicAnimator _registerBodyForItem:] + 784
    5   UIKit                               0x0000000109c968c0 -[UIDynamicItemBehavior _reevaluate:] + 306
    6   UIKit                               0x0000000109c969b1 -[UIDynamicItemBehavior _associate] + 66
    7   UIKit                               0x000000010a3fc700 -[UIDynamicAnimator _registerBehavior:] + 306
    8   UIKit                               0x000000010a3fc4bc -[UIDynamicAnimator addBehavior:] + 118
    9   Dynamic Animator                    0x00000001089e5ea3 _TFC16Dynamic_Animator22BreakOutViewController21giveDynamicPropertiesfS0_FT_T_ + 675
    10  Dynamic Animator                    0x00000001089e4aca _TFC16Dynamic_Animator22BreakOutViewController11viewDidLoadfS0_FT_T_ + 378
    11  Dynamic Animator                    0x00000001089e4b42 _TToFC16Dynamic_Animator22BreakOutViewController11viewDidLoadfS0_FT_T_ + 34
    12  UIKit                               0x0000000109b52f98 -[UIViewController loadViewIfRequired] + 1198
    13  UIKit                               0x0000000109b532e7 -[UIViewController view] + 27
    14  UIKit                               0x0000000109bb679c -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 502
    15  UIKit                               0x0000000109bb5710 -[UITabBarController transitionFromViewController:toViewController:] + 59
    16  UIKit                               0x0000000109bb1522 -[UITabBarController _setSelectedViewController:] + 377
    17  UIKit                               0x0000000109bb1398 -[UITabBarController setSelectedViewController:] + 234
    18  UIKit                               0x0000000109bb5581 -[UITabBarController _tabBarItemClicked:] + 481
    19  UIKit                               0x00000001099be194 -[UIApplication sendAction:to:from:forEvent:] + 92
    20  UIKit                               0x0000000109d8f6c4 -[UITabBar _sendAction:withEvent:] + 498
    21  UIKit                               0x00000001099be194 -[UIApplication sendAction:to:from:forEvent:] + 92
    22  UIKit                               0x0000000109b2d6fc -[UIControl sendAction:to:forEvent:] + 67
    23  UIKit                               0x0000000109b2d9c8 -[UIControl _sendActionsForEvents:withEvent:] + 311
    24  UIKit                               0x0000000109d9479c -[UITabBar(Static) _buttonUp:] + 103
    25  UIKit                               0x00000001099be194 -[UIApplication sendAction:to:from:forEvent:] + 92
    26  UIKit                               0x0000000109b2d6fc -[UIControl sendAction:to:forEvent:] + 67
    27  UIKit                               0x0000000109b2d9c8 -[UIControl _sendActionsForEvents:withEvent:] + 311
    28  UIKit                               0x0000000109b2caf8 -[UIControl touchesEnded:withEvent:] + 601
    29  UIKit                               0x0000000109a2d49b -[UIWindow _sendTouchesForEvent:] + 835
    30  UIKit                               0x0000000109a2e1d0 -[UIWindow sendEvent:] + 865
    31  UIKit                               0x00000001099dcb66 -[UIApplication sendEvent:] + 263
    32  UIKit                               0x00000001099b6d97 _UIApplicationHandleEventQueue + 6844
    33  CoreFoundation                      0x0000000108ea3a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    34  CoreFoundation                      0x0000000108e9995c __CFRunLoopDoSources0 + 556
    35  CoreFoundation                      0x0000000108e98e13 __CFRunLoopRun + 867
    36  CoreFoundation                      0x0000000108e98828 CFRunLoopRunSpecific + 488
    37  GraphicsServices                    0x000000010e501ad2 GSEventRunModal + 161
    38  UIKit                               0x00000001099bc610 UIApplicationMain + 171
    39  Dynamic Animator                    0x00000001089ecefd main + 109
    40  libdyld.dylib                       0x000000010b9f192d start + 1
    41  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

无论我尝试什么,我都会得到这个。问题在哪里?我收到 sigabart 错误,我已经尝试了所有方法来修复它。

有道理。

错误消息中最重要的部分是

Terminating app due to uncaught exception 'NSInternalInconsistencyException',

reason: 'Invalid size {0, 0} for item < UIView: 0x7fdab1d34010; frame = (0 0; 0 0)

您必须使用 UIView

的指定初始化器
init(frame frame: CGRect)

提供大小而不是默认初始化程序(括号)

我认为您在这里遗漏的是,当您在 createBall() 函数中执行 var ball = Block(frame: CGRect(x: 197, y: 614, width: 30, height: 30)) 时,您正在创建一个与您的 ball 属性.

然后,当您将行为添加到 ball 属性 时,它指的是您使用 UIView() 创建的 属性 并具有默认框架CGRect.zero 你得到了例外。

在您的 createBall() 函数中,您应该在没有 var 的情况下执行 ball = Block(frame: CGRect(x: 197, y: 614, width: 30, height: 30)),以便它分配给您现有的 属性 而不是创建新视图。你的桨也一样。