美文网首页
iOS 仿微信发送语音消息按钮 - 手势按钮(一)

iOS 仿微信发送语音消息按钮 - 手势按钮(一)

作者: 我就叫Tom怎么了 | 来源:发表于2017-07-19 15:52 被阅读0次

    最近在做基于XMPP的IM,开发到发送语音消息的功能.在某度上搜了很久也没有找到适合的方法.索性自己琢磨了一个,提供给大家参考.(其中找到的很多文章都是一个复制另一个的,很烦!没格式没头没尾的.给不了人任何思路的一堆文字)

    发送语音功能模块主要有三个模块

    Part 1 :手势按钮

    仿照经常使用的微信"发送消息"按钮.当时第一想法就是想到- (BOOL)containsPoint:(CGPoint)p;方法.

    直接上代码

    .h

    #import <UIKit/UIKit.h>
    
    @protocol DPChatToolBarAudioDelegate <NSObject>
    
    /*
     * 录音完成
     *
     * @param audioData amr文件data
     * @prram body      附带信息,比如录音时长等信息
     */
    - (void)DPAudioRecordingFinishWithData:(NSData *)audioData withBodyString:(NSString *)body;
    
    @optional
    
    /*
     * 开始录音
     *
     * @param isRecording 是否开始
     *
     */
    - (void)DPAudioStartRecording:(BOOL)isRecording;
    
    /*
     * 录音失败
     */
    - (void)DPAudioRecordingFail:(NSString *)reason;
    
    /*
     * 音频值测量
     *
     * @param power 音频值
     */
    - (void)DPAudioSpeakPower:(float)power;
    
    @end
    
    @interface ChatToolBarAudioButton : UIButton
    
    @property (nonatomic, assign) id <DPChatToolBarAudioDelegate> delegate;
    
    @end
    

    .m

    #import "ChatToolBarAudioButton.h"
    
    @interface ChatToolBarAudioButton () <DPAudioRecorderDelegate>
    {
        BOOL isShouldSendAudioMessage;         //用户是否取消发送消息
        NSUInteger __block audioTimeLength; //录音时长
    }
    
    @end
    
    @implementation ChatToolBarAudioButton
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {
            
            self.layer.cornerRadius = 4;
            self.clipsToBounds = YES;
            [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
            [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self setTitle:@"按住 说话" forState:UIControlStateNormal];
            [self setTitle:@"松开 结束" forState:UIControlStateHighlighted];
            [self setBackgroundImage:[UIImage imageNamed:@"chatBar_recordBg"] forState:UIControlStateNormal];
            [self setBackgroundImage:[UIImage imageNamed:@"chatBar_recordSelectedBg"] forState:UIControlStateHighlighted];
            
            //增加长按手势
            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
            longPress.minimumPressDuration = 0;
            [self addGestureRecognizer:longPress];
        }
        return self;
    }
    
    - (void)longPress:(UILongPressGestureRecognizer *)gestureRecognizer
    {
        CGPoint point = [gestureRecognizer locationInView:self];
        
        if(gestureRecognizer.state == UIGestureRecognizerStateBegan) {
            [self setTitle:@"松开 结束" forState:UIControlStateNormal];
            [self setBackgroundImage:[UIImage imageNamed:@"chatBar_recordSelectedBg"] forState:UIControlStateNormal];
            [self audioStart];
        } else if(gestureRecognizer.state == UIGestureRecognizerStateEnded) {
            [self setTitle:@"按住 说话" forState:UIControlStateNormal];
            [self setBackgroundImage:[UIImage imageNamed:@"chatBar_recordBg"] forState:UIControlStateNormal];
            [self audioStop];
        } else if(gestureRecognizer.state == UIGestureRecognizerStateChanged) {
            if ([self.layer containsPoint:point]) {
                [self setTitle:@"松开 结束" forState:UIControlStateNormal];
                isCancelSendAudioMessage = NO;
            } else {
                [self setTitle:@"松开 取消" forState:UIControlStateNormal];
                isCancelSendAudioMessage = YES;
            }
        } else if (gestureRecognizer.state == UIGestureRecognizerStateFailed) {
            NSLog(@"失败");
        } else if (gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
            NSLog(@"取消");
        }
    }
    
    - (void)audioStart
    {
        //开始录音
        [[DPAudioRecorder sharedInstance] startRecording];
    }
    
    //结束录音
    - (void)audioStop
    {
        [[DPAudioRecorder sharedInstance] stopRecording];
    }
    
    //录音失败
    - (void)audioFailed
    {
        //do something
    }
    @end
    

    效果图:

    发送语音按钮.gif

    Demo 地址 :https://github.com/XL-Andrew/ChatToolBarAudioButton

    相关文章

      网友评论

          本文标题:iOS 仿微信发送语音消息按钮 - 手势按钮(一)

          本文链接:https://www.haomeiwen.com/subject/njwtkxtx.html