首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >覆盖touchesBegan

覆盖touchesBegan
EN

Stack Overflow用户
提问于 2015-05-27 23:36:19
回答 2查看 2.6K关注 0票数 1

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

代码语言:javascript
复制
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的一个子类。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-27 23:42:58

如果您重写了任何一个触摸方法,那么您应该覆盖所有四个方法并调用超级方法。

代码语言:javascript
复制
-(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
}
票数 3
EN

Stack Overflow用户

发布于 2015-05-28 00:19:23

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

从苹果的文档来看:

覆盖命中测试确保superview接收所有的触摸,因为通过将自己设置为命中测试视图,superview拦截并接收通常首先传递给子视图的触摸。如果superview不覆盖hitTest:withEvent:,则触摸事件与它们第一次发生的子视图相关联,并且从未发送到superview。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30494888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档