当前位置: 首页 > news >正文

新浪微博客户端(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];

}

最终效果:

 

  

相关文章:

  • 基本概念学习(9005)---位段
  • 常用python机器学习库总结
  • 下拉框
  • AOP注解不起作用的debug结果
  • Unity应用架构设计(3)——构建View和ViewModel的生命周期
  • 动态链接库相关
  • java集合类
  • Autodesk Vault: 获取授权失败
  • 017,idea集成svn
  • 一个mysql的备份脚本(可以结合多实例进行调整)
  • Oracle Redo Log
  • 基于Maven引入Hadoop包报Missing artifact jdk.tools:jdk.tools:jar:1.6
  • List运用
  • EXP-00003: 未找到段xxx的存储定义
  • js-JavaScript高级程序设计学习笔记14
  • [nginx文档翻译系列] 控制nginx
  • android高仿小视频、应用锁、3种存储库、QQ小红点动画、仿支付宝图表等源码...
  • IDEA常用插件整理
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • JavaScript标准库系列——Math对象和Date对象(二)
  • magento 货币换算
  • magento2项目上线注意事项
  • Phpstorm怎样批量删除空行?
  • React-Native - 收藏集 - 掘金
  • Ruby 2.x 源代码分析:扩展 概述
  • vue2.0项目引入element-ui
  • vue中实现单选
  • yii2权限控制rbac之rule详细讲解
  • 编写符合Python风格的对象
  • 对话:中国为什么有前途/ 写给中国的经济学
  • 工程优化暨babel升级小记
  • 深入 Nginx 之配置篇
  • 实现简单的正则表达式引擎
  • 一个SAP顾问在美国的这些年
  • 与 ConTeXt MkIV 官方文档的接驳
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • 好程序员大数据教程Hadoop全分布安装(非HA)
  • 正则表达式-基础知识Review
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • #etcd#安装时出错
  • #WEB前端(HTML属性)
  • #我与Java虚拟机的故事#连载06:收获颇多的经典之作
  • (145)光线追踪距离场柔和阴影
  • (16)UiBot:智能化软件机器人(以头歌抓取课程数据为例)
  • (附源码)ssm教师工作量核算统计系统 毕业设计 162307
  • (免费领源码)Python#MySQL图书馆管理系统071718-计算机毕业设计项目选题推荐
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
  • (转)iOS字体
  • (转)机器学习的数学基础(1)--Dirichlet分布
  • (转载)CentOS查看系统信息|CentOS查看命令
  • **PyTorch月学习计划 - 第一周;第6-7天: 自动梯度(Autograd)**
  • .Net8 Blazor 尝鲜
  • .NET企业级应用架构设计系列之开场白
  • .NET设计模式(2):单件模式(Singleton Pattern)