Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIScrollView触控开始_Iphone_Uiscrollview - Fatal编程技术网

Iphone UIScrollView触控开始

Iphone UIScrollView触控开始,iphone,uiscrollview,Iphone,Uiscrollview,所以我只想在用户触摸UIScrollView时播放声音。 UIScrollViewDelegate具有ScrollViewWillBeginDraging:方法,但它仅在touchMoved时被调用。我想让它在触摸屏上被调用 尝试触摸开始:withEvent:但它没有收到任何触摸。 有人有线索吗?我认为您必须将UIScrollView子类化才能做到这一点 ToucheSBegind:withEvent:仅发送到UIView的子类。您可能正在控制器中实现touchsbegind:withEvent

所以我只想在用户触摸UIScrollView时播放声音。 UIScrollViewDelegate具有ScrollViewWillBeginDraging:方法,但它仅在touchMoved时被调用。我想让它在触摸屏上被调用

尝试触摸开始:withEvent:但它没有收到任何触摸。
有人有线索吗?

我认为您必须将UIScrollView子类化才能做到这一点

ToucheSBegind:withEvent:仅发送到UIView的子类。您可能正在控制器中实现touchsbegind:withEvent:触摸,对吗?如果是这样的话,从那里就不行了

或者,如果您将UIView的子类(您编写的)放在UIScrollView中,您还可以从那里捕获TouchesBegind事件(但仅当用户触摸该特定子视图时)。
默认情况下,UIScrollView会将触摸传递到其子视图(请参见UIScrollView中的ToucheShouldBegin:withEvent:inContentView)。

您应该在UIScrollView上,而不是在UIScrollViewDelegate上添加ToucheSBegind:withEvent:。
UIScrollView是UIResponder的一个子类,它具有触摸事件。

您也可以在控制器中响应这些事件。为此,您必须(在控制器中)定义此功能:

然后,在“视图显示”中,您需要调用“becomeFirstResponder”:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];

关于这个问题的最新更新(包括ZoomScrollView源代码的链接+一些关于UIScrollView内部结构的优秀解释)。另外,请查看苹果更新的示例。

改用轻触手势识别器:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)];
[recognizer setNumberOfTapsRequired:1];
[recognizer setNumberOfTouchesRequired:1];
[scrollView addGestureRecognizer:recognizer];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)];
[recognizer setNumberOfTapsRequired:1];
[recognizer setNumberOfTouchesRequired:1];
[scrollView addGestureRecognizer:recognizer];  

请改用轻触手势识别器:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)];
[recognizer setNumberOfTapsRequired:1];
[recognizer setNumberOfTouchesRequired:1];
[scrollView addGestureRecognizer:recognizer];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)];
[recognizer setNumberOfTapsRequired:1];
[recognizer setNumberOfTouchesRequired:1];
[scrollView addGestureRecognizer:recognizer];  

创建
UIScrollView
的子类并实现所有

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

// If not dragging, send event to next responder
  if (!self.dragging){ 
    [self.nextResponder touchesBegan: touches withEvent:event]; 
  }
  else{
    [super touchesEnded: touches withEvent: event];
  }
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

// If not dragging, send event to next responder
    if (!self.dragging){ 
     [self.nextResponder touchesBegan: touches withEvent:event]; 
   }
   else{
     [super touchesEnded: touches withEvent: event];
   }
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

  // If not dragging, send event to next responder
   if (!self.dragging){ 
     [self.nextResponder touchesBegan: touches withEvent:event]; 
   }
   else{
     [super touchesEnded: touches withEvent: event];
   }
}

Rajneesh071用swift 4回答 将脚本中scrollView的自定义类更改为
CustomScrollView

import Foundation

class CustomScrollView: UIScrollView {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if !isDragging {
            next?.touchesBegan(touches, with: event)
        } else {
        super.touchesBegan(touches, with: event)
        }
    }
}
<代码>导入基础 类CustomScrollView:UIScrollView{ 覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){ 如果!我是拉格{ 下一步?触摸开始(触摸,带有:事件) }否则{ super.touchesbeated(touches,with:event) } } }
您需要将scroll view子类化,然后实现touchesBegind方法。您还需要将delayTouchDown属性设置为false。

如果
UIScrollView
UIScrollViewDelegate
该怎么办?好的,这是可行的,但是我应该如何声明该方法“touch”要接收用户触摸的位置?@HernanArber例如:@Rajneesh071您在哪里处理布尔“拖动”?当子类化
UIScorllView
时,是否会自动处理它?@jeet.chanchawat Dude是UIScrollView的子类,拖动是UIScrollView的布尔属性