zoukankan      html  css  js  c++  java
  • 新浪微博客户端(52)-长按或滑动显示表情

    DJEmotionPageView.m

    /*
             * 添加长按监听事件
             * 类似于android里面setOnLongClickListener
             */
            
            [self addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongClick:)]];
    // 当触发长按事件时调用此方法
    - (void)onLongClick:(UILongPressGestureRecognizer *)recognizer {
    
        /*
         * 获取长按事件状态,类似于android里面的 MotionEvent
         * android 里面的MotionEvent 同样有以下几种状态:
         * MotionEvent.ACTION_DOWN
         * MotionEvent.ACTION_MOVE
         * MotionEvent.ACTION_UP
         */
        switch (recognizer.state) {
            case UIGestureRecognizerStateBegan: // 事件开始
            case UIGestureRecognizerStateChanged:{
                /*
                 * 获取触摸点的坐标,android里面对应的方法为
                 * motionEvent.getX(), motionEvent.getY()
                 */
                CGPoint touchPoint = [recognizer locationInView:recognizer.view];
                
                // 判断当前触摸点的坐标是否在某个表情按钮上,如果在则显示popView
                 DJEmotionButton *btn = [self emotionButtonWithTouchPoint:touchPoint];
                if (btn) [self.popView showFromEmotionBtn:btn]; // 显示popView
            }
                break;
            case UIGestureRecognizerStateCancelled:
            case UIGestureRecognizerStateEnded:{ // 事件结束
                // 当事件结束时,判断当前触摸点的坐标是否在表情按钮上,如果在,则将对应表情按钮的内容输入到textVeiw
                [self.popView removeFromSuperview];
                CGPoint touchPoint = [recognizer locationInView:recognizer.view];
                DJEmotionButton *btn = [self emotionButtonWithTouchPoint:touchPoint];
                if (btn) [self sendBtnClickNotification:btn]; // 发送按钮点击通知
                
            }
                break;
            default:
                break;
        }
    // 发送点击广播(和android类似,区别在于android的广播是只要有上下文对象context,就可以发送)
    // iOS中的通知发送和接收都是通过NSNotificationCenter完成
    - (void)sendBtnClickNotification:(DJEmotionButton *)btn {
    
        NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
        userInfo[DJEmotionDidSelctedEmotionKey] = btn.emotion;
        
        [[NSNotificationCenter defaultCenter] postNotificationName:DJEmotionDidSelectedNotification object:nil userInfo:userInfo];
    
    }
    
    /**
     * 点击表情监听方法
     * param btn 点击的表情按钮
     */
    - (void)emotionBtnClick:(DJEmotionButton *)btn {
        
        [self.popView showFromEmotionBtn:btn];
        [self sendBtnClickNotification:btn];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.popView removeFromSuperview];
        });
    
    }

    DJEmotionPopView.h

    #import <UIKit/UIKit.h>
    
    @class DJEmotion,DJEmotionButton;
    @interface DJEmotionPopView : UIView
    
    + (instancetype)popView;
    - (void)showFromEmotionBtn:(DJEmotionButton *)emotinBtn;
    
    
    @property (nonatomic,strong) DJEmotion *emotion;
    
    
    @end

    DJEmotionPopView.m

    // 在emotionBtn的位置显示popView
    - (void)showFromEmotionBtn:(DJEmotionButton *)emotinBtn {
    
        // 获取当前应用程序最顶层的窗口
        UIWindow *lastWindow = [[UIApplication sharedApplication].windows lastObject];
        
        // 转换btn当前坐标系
        CGRect newFrame = [emotinBtn convertRect:emotinBtn.bounds toView:nil];
        
        self.centerX = CGRectGetMidX(newFrame);
        self.y = CGRectGetMaxY(newFrame) - self.height;
        
        // 将当前点击按钮的表情模型传递给popview
        self.emotion = emotinBtn.emotion;
        
        [lastWindow addSubview:self];
    
    }

    最终效果:

      

  • 相关阅读:
    Linxu 挂载光盘和硬盘
    Linux firewall
    指纹获取 Fingerprint2
    Vue 封装的组件生命周期钩子
    vue富文本编辑,编辑自动预览,单个图片上传不能预览的问题解决:
    vue 集成百度富文本编辑器
    axios 的二次封装
    element 列表中已选的标记
    element 表单的input循环生成,并可单个input失去焦点单个验证并保存; (多个表单实例)
    axios 二进制流导出
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/6131895.html
Copyright © 2011-2022 走看看