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

IOS的处理touch事件处理(按照手指的移动移动一个圆,开发环境用的ios7,storyboard)...

先看下页面的效果图:


首先定义这个ball它有两个属性和两个方法:

@property(nonatomic) CGPoint location;

@property(nonatomic) CGFloat length;

-(CGPoint) getCenterPoint;

-(BOOL) isInTheBall:(CGPoint) point;


方法体是:

//找出ball的中心点
-(CGPoint) getCenterPoint {
    
    return CGPointMake((self.location.x+self.length/2), self.location.y+self.length/2);
};


//看点point是不是在ball的范围内
-(BOOL) isInTheBall:(CGPoint) point{
    CGPoint center = self.getCenterPoint;
    float t = (point.x - center.x) * (point.x - center.x);
    float y = (point.y - center.y) * (point.y - center.y);
    
    float k = sqrtf(t+y);
    if (k < self.length/2) {
        return YES;
    }else {
        return NO;
    }
};

定义BallView继承UIView

@property(nonatomic) Ball* ball;
@property(nonatomic) BOOL isTouch;  //表示手指在ball的范围内移动
@property(nonatomic) CGPoint prePoint;  //手指在进入move事件之前的那个点
- (id)initWithBall:(CGRect)frame aBall:(Ball*) ball; //初始化方法

初始化函数为:

- (id)initWithBall:(CGRect)frame aBall:(Ball*) ball
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.ball = ball;
    }
    return self;
}

-(void)awakeFromNib{
    self.backgroundColor = nil;
    self.opaque = NO;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [super drawRect:rect];
    
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    [[UIColor whiteColor] set];
    
    //rect是整个view
    CGContextFillRect(contextRef, rect);
    
    [[UIColor redColor] set];
    
    //CGContextAddEllipseInRect不会填充圆圈的内部
   // CGContextAddEllipseInRect(contextRef, CGRectMake(200.0f, 200.0f, 50.0f, 50.0f));
    CGContextFillEllipseInRect(contextRef, CGRectMake(self.ball.location.x,self.ball.location.y,self.ball.length,self.ball.length));
    
    CGContextStrokePath(contextRef);
}

我们在viewController里初始化仅仅要:

-(void) loadView{
    [super loadView];

    Ball* ball = [[Ball alloc] init];
    ball.location = CGPointMake(200.0f, 100.0f);
    ball.length = 80.0f;
    BallView* view = [[BallView alloc] initWithBall:[UIScreen mainScreen].bounds aBall:ball];
    [self.view addSubview:view];
    
    
}

然后在以下在BallView中进行事件处理

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesBegan");
    //以下两句知道手指在屏幕上的点的信息
    UITouch* touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    if ([self.ball isInTheBall:point]) {
        self.isTouch = YES;
        self.prePoint = point;
    }else{
        self.isTouch = NO;
    }
    NSLog(@"x=%f,y=%f",point.x,point.y);
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
     NSLog(@"touchesMoved");
    if (self.isTouch) {
        
        CGRect preRect = CGRectMake(self.ball.location.x, self.ball.location.y, self.ball.length, self.ball.length);
        //先用之前的location绘制一遍
        [self setNeedsDisplayInRect:preRect];
        
        UITouch* touch = [touches anyObject];
        CGPoint point = [touch locationInView:self];
        
        //cx和cy是手指的偏移量。用他们能够计算出新的location
        float cx = point.x - self.prePoint.x;
        float cy = point.y - self.prePoint.y;
        
        self.ball.location = CGPointMake(self.ball.location.x + cx, self.ball.location.y+cy);
        CGRect newRect = CGRectMake(self.ball.location.x, self.ball.location.y, self.ball.length, self.ball.length);
        //用新的location绘制一遍
        [self setNeedsDisplayInRect:newRect];
        self.prePoint = point;
    }
}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesEnded");
    self.isTouch = NO;
}


代码能够在http://download.csdn.net/detail/baidu_nod/7533317下载

相关文章:

  • DataTable转实体类
  • [Java][Android][Process] ProcessBuilder与Runtime差别
  • NET Core微服务之路:自己动手实现Rpc服务框架,基于DotEasy.Rpc服务框架的介绍和集成...
  • 服务器虚拟化的十大谎言
  • cacti找不到网卡的解决方法
  • Java并发3:线程
  • 技术部门怎么年终考核最合理?
  • python机器学习实战(二)
  • 生成验证码
  • 【Spring Boot】19.集成消息
  • Visio2010建立ER图并直接导出为SQL语句
  • tomcat架构分析和源码解读
  • 2015博客升级记(五):CentOS 7.1编译安装PHP7
  • Git的一些常用操作
  • 正确配置jstl的maven依赖,jar包冲突的问题终于解决啦
  • [NodeJS] 关于Buffer
  • 【162天】黑马程序员27天视频学习笔记【Day02-上】
  • 【407天】跃迁之路——程序员高效学习方法论探索系列(实验阶段164-2018.03.19)...
  • Android 控件背景颜色处理
  • Docker 笔记(1):介绍、镜像、容器及其基本操作
  • Fundebug计费标准解释:事件数是如何定义的?
  • Java 23种设计模式 之单例模式 7种实现方式
  • Next.js之基础概念(二)
  • php的插入排序,通过双层for循环
  • Spark VS Hadoop:两大大数据分析系统深度解读
  • Storybook 5.0正式发布:有史以来变化最大的版本\n
  • vagrant 添加本地 box 安装 laravel homestead
  • vue从创建到完整的饿了么(18)购物车详细信息的展示与删除
  • 关于字符编码你应该知道的事情
  • 后端_MYSQL
  • 浅谈JavaScript的面向对象和它的封装、继承、多态
  • 腾讯大梁:DevOps最后一棒,有效构建海量运营的持续反馈能力
  • 项目实战-Api的解决方案
  • 一个SAP顾问在美国的这些年
  • 深度学习之轻量级神经网络在TWS蓝牙音频处理器上的部署
  • Android开发者必备:推荐一款助力开发的开源APP
  • k8s使用glusterfs实现动态持久化存储
  • 数据可视化之下发图实践
  • ​Linux·i2c驱动架构​
  • ​linux启动进程的方式
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • (zhuan) 一些RL的文献(及笔记)
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (附源码)springboot工单管理系统 毕业设计 964158
  • (附源码)计算机毕业设计SSM疫情居家隔离服务系统
  • (九)信息融合方式简介
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (三)终结任务
  • (四)JPA - JQPL 实现增删改查
  • (算法)求1到1亿间的质数或素数
  • (五)网络优化与超参数选择--九五小庞
  • (新)网络工程师考点串讲与真题详解
  • (转)scrum常见工具列表
  • (转)重识new