首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >捕获拖出UIControl - UILongPressGestureRecognizer

捕获拖出UIControl - UILongPressGestureRecognizer
EN

Stack Overflow用户
提问于 2014-11-15 20:30:12
回答 2查看 168关注 0票数 0

我正在尝试实现异步语音记录功能。有一个按钮有一个UILongPressGestureRecognizer,它启动录音。现在,当他们松手的时候,录音就被保存并发送了。

代码语言:javascript
复制
#pragma mark - Actions
- (void) recordButtonPressed:(UILongPressGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        [self.voiceRecorderView.recordButton setImage:[UIImage imageNamed:kWSGreyDotXLarge] forState:UIControlStateNormal];
        [self startRecording];

    }
    if (gesture.state == UIGestureRecognizerStateEnded) {

        [self stopRecording];
        [self.voiceRecorderView.recordButton setImage:[UIImage imageNamed:kWSPinkDotXLarge] forState:UIControlStateNormal];
    }

}

这是可行的,但现在我需要复制现在的标准功能,允许用户拖动他们的手指从按钮,而它是按下取消录音。

如何添加gestureRecognizer以判断用户是否拖出控件之外但仍在按该控件?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-20 04:33:34

@Jaleel的回答给了我一个开始。以下是一个完整的工作版本:

代码语言:javascript
复制
- (void) recordButtonPressed:(UILongPressGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {

        [self.voiceRecorderView setCancelText:WSCancelLabelTextStart];

        if (!audioRecorder.recording)
        {
            [self startRecording];
        }
        cancelflag = NO;
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        CGPoint touchPoint = [gesture locationInView:self.voiceRecorderView];

        if (!CGRectContainsPoint(self.voiceRecorderView.recordButton.frame, touchPoint )) {
            cancelflag = YES;
            [self.voiceRecorderView.recordButton setImage:[UIImage imageNamed:kWSGreyDotXLarge] forState:UIControlStateNormal];
            [self.voiceRecorderView setCancelText:WSCancelLabelTextCancelling];
        }
        else {
            cancelflag = NO;
            [self.voiceRecorderView.recordButton setImage:[UIImage imageNamed:kWSPinkDotXLarge] forState:UIControlStateNormal];
            [self.voiceRecorderView setCancelText:WSCancelLabelTextStart];
        }
    }
    else if (gesture.state == UIGestureRecognizerStateEnded) {

        [self stopRecording];

        if(!cancelflag)
        {
            [self sendRecording:self.recordingURL];
        }
        else {
            [self.voiceRecorderView setCancelText:WSCancelLabelTextCancelled];
        }

        [self.voiceRecorderView resetView];
    }
}
票数 1
EN

Stack Overflow用户

发布于 2014-11-18 13:30:36

我希望它能对你有用..。

代码语言:javascript
复制
 BOOL cancelflag;
 UIButton *recordBtn;
 - (void)viewDidLoad
   {
   [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]    initWithTarget:self action:@selector(handleLongPress:)];
   [longPress setDelegate:self];
    [recordBtn addGestureRecognizer:longPress];
   }  


-(void)handleLongPress:(UILongPressGestureRecognizer *)longPressRecognizer
 {

  if(longPressRecognizer.state == UIGestureRecognizerStateBegan)
   {
    if (!stopBtnFlag)
    {
        if (!audioRecorder.recording)
        {
            [self performSelectorOnMainThread:@selector(setUpAudioRecorder) withObject:nil waitUntilDone:YES];
            [audioRecorder record];
            NSLog(@"Recording...");

        }
        stopBtnFlag = YES;
         cancelflag =YES;
    }
}
else if (longPressRecognizer.state == UIGestureRecognizerStateChanged)
{
    [audioRecorder stop];
    stopBtnFlag = NO;
    NSLog(@"moved");

 }
  else if (longPressRecognizer.state == UIGestureRecognizerStateEnded)
  {
    if(cancelflag)
    {
    [audioRecorder stop];
        [self playmusic];
    }

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

https://stackoverflow.com/questions/26950395

复制
相关文章

相似问题

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