【问题标题】:Override touchesBegan覆盖 touchesBegan
【发布时间】:2015-05-27 23:36:19
【问题描述】:

我目前正在使用 swift 开发 iOS 应用程序。我使用 override 来编写我自己的 tocuhesBegan 和 tochesEnded 函数。现在当我使用 self.button 时。

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    super.touchesBegan(touches, withEvent: event)
    for touch in touches {
        var tap = touch as? UITouch
        var touchPoint: CGPoint = tap!.locationInView(self)
        self.touchDown(touchPoint)
    }
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    super.touchesEnded(touches, withEvent: event)
    self.releaseTouch()
}

override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
    super.touchesCancelled(touches, withEvent: event)
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    super.touchesMoved(touches, withEvent: event)
}

func touchDown(point: CGPoint){
    if rippleEffect == true {
        touchView.frame = touchViewInitFrame
        self.addSubview(touchView)
        var touchViewFrameXPosition: CGFloat = point.x - (frame.size.width) / 2
        println("X position = \(touchViewFrameXPosition)")
        var touchViewFrameYPosition: CGFloat = -(self.frame.size.width - self.frame.size.height) / 2
        touchViewFrame = CGRectMake(touchViewFrameXPosition, touchViewFrameYPosition, frame.size.width, frame.size.width)
        UIView.animateWithDuration(0.25, delay: 0.0, options: .CurveEaseInOut, animations: {
            self.touchView.frame = self.touchViewFrame
            }, completion: nil)
    }

}

func releaseTouch(){
    if rippleEffect == true {
        UIView.animateWithDuration(0.0, delay: 0.0, options: .CurveEaseInOut, animations: {
            self.touchView.removeFromSuperview()
            }, completion: nil)
    }
}

它没有转到我在选择器中指定的功能。是否有其他人遇到此问题或有人知道发生了什么?

这是我遇到问题时使用的代码。它是 UIButton 的子类。

【问题讨论】:

  • 你的问题不清楚。覆盖 touchesBegan 与按钮的操作方法有什么关系?您是否在自定义按钮类中开始覆盖触摸?您应该显示调用 addTarget:action: 的代码,以及选择器的实现。

标签: ios xcode swift uibutton


【解决方案1】:

如果你重写任何一个 touches 方法,你应该重写所有四个 并且调用 super。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    //your code
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    //your code
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesMoved:touches withEvent:event];
    //your code
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesCancelled:touches withEvent:event];
    //your code
}

【讨论】:

  • 谢谢戴夫,但我试过了,但没有帮助。不过,我感谢您的尝试。
【解决方案2】:

您的视图和按钮可能有两个不同的句柄,以证明尝试触摸屏幕中的任何其他位置并且您的触摸将被检测到,但是在按下按钮时触摸是句柄并且永远不会传递给其他处理程序.您必须在执行您需要做的事情之前/之后选择,拦截所有消息并处理是否应该传递原始句柄,或者实现按钮处理程序并使其将消息传递到链上:

From the apple documentation:

覆盖命中测试可确保超级视图接收所有触摸 因为,通过将自己设置为命中测试视图,超级视图 拦截并接收通常传递给 首先是子视图。如果一个superview没有覆盖hitTest:withEvent:, 触摸事件与它们首先出现的子视图相关联 发生并且永远不会发送到superview。

【讨论】:

  • 我将如何实施这两个选项?
猜你喜欢
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2016-01-28
  • 1970-01-01
  • 2011-06-11
  • 2012-01-28
  • 2012-02-16
  • 1970-01-01
相关资源
最近更新 更多