2016-10-20 13 views
0

UIViewController, Custum UIView 및 GameController가 있습니다. CustumUiView는 UIViewController 위에 겹쳐서 UIView에서 클릭을 추적하기 위해 뷰를 분리해야합니다. 클릭 수 등록하는 Custum UIView "인터페이스보기"에서 사용자 지정 단추가 있지만 GameController 또는 UIViewController (둘 다 시도한 수신기) 수신기가 클릭을 등록하지 않습니다.신속한 사용자 정의 버튼이 작동하지 않습니다.

코드는 다음과 같습니다.

인터페이스보기

class InterfaceView: UIView { 

var resetButton: UIButton! 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 

    resetButton = UIButton(frame: CGRect(x: 0, y: 0, width: 35, height: 35)) 
    resetButton.setImage(UIImage(named: "reset"), for: UIControlState()) 
    addSubview(resetButton) 
} 

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { 
    // let touches through and only catch the ones on buttons 
    let hitView = super.hitTest(point, with: event) 

    if hitView is UIButton { 
     print("Captured Hit") // Stack Overflow Note: This is working. 
     return hitView 
    } 

    return nil 
} 
} 

게임 컨트롤러 :

class GameController 
{ 
    var interface: InterfaceView! 
    { 
     didSet 
     { 
      interface.resetButton.addTarget(self, action: #selector (GameController.reset), for: .touchUpInside) 
     } 

    @objc func reset(sender: UIButton!) 
    { 
     print("button pressed") 
    } 
} 

의 UIViewController :

class GameViewController: UIViewController 
{ 

fileprivate let controller: GameController 
@IBOutlet weak var GameView: UIView! 

init(_ coder: NSCoder? = nil) 
{ 
    // Logic Goes Here 
    controller = GameController() 

    if let coder = coder 
    { 
     super.init(coder: coder)! 
    } else 
    { 
     super.init(nibName: nil, bundle: nil) 
    } 
} 

required convenience init(coder: NSCoder) 
{ 
    self.init(coder) 
} 
} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) 
{ 
    if let touch = touches.first as UITouch? 
    { 
     let point = touch.preciseLocation(in: GameView) 
     if (touch.view == GameView) 
     { 
      print("Do stuff") 
     } 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) 
{ 
    if let touch = touches.first 
    { 
     let point = touch.preciseLocation(in: GameView) 
     if (touch.view == GameView) 
     { 
      print("Do other stuff") 
     } 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) 
{ 
    print("Reset Stuff") 
} 

답변

0

를 해결 B y를 GameViewController에서 사용하고, didSet을 사용하여 버튼 누름의 대상과 선택기를 설정 한 다음 GameViewController의 메서드를 사용하여 GameController 내의 메서드를 호출합니다.

@IBOutlet weak var interface: InterfaceView! 
{ 
    didSet 
    { 
     interface.resetButton.addTarget(self, action: #selector (GameViewController.reset), for: .touchUpInside) 
    } 
}