先看下页面的效果图:

首先定义这个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下载

IOS的处理touch事件处理(按照手指的移动移动一个圆,开发环境用的ios7,storyboard)的更多相关文章

  1. 【转】IOS的处理touch事件处理(依照手指的移动移动一个圆,开发环境用的ios7,storyboard)-- 不错

    原文网址:http://blog.csdn.net/baidu_nod/article/details/32934565 先看下页面的效果图: 首先定义这个ball它有两个属性和两个方法: @prop ...

  2. ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境(转)

    原文地址:http://blog.csdn.net/shangyuan21/article/details/18153605 我们都知道开发iPhone等ios平台的移动应用时需要使用Mac本,但是M ...

  3. ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境

    我们都知道开发iPhone等ios平台的移动应用时需要使用Mac本,但是Mac本都比较昂贵,所以我们可以采用Windows7上利用VMWare安装Mac操作系统的方法来模拟ios开发环境,达到降低成本 ...

  4. Cocoa Touch事件处理流程--响应者链

    Cocoa Touch事件处理流程--响应者链 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/9264335 转载请注明 ...

  5. Android touch事件处理流程

    前面我们看了key事件的处理流程,相信大家对此已经有了新的认识,这篇文章我打算带领大家来看看稍微复杂些的touch 事件的处理流程.说它复杂是因为key事件本身就key down,up,long pr ...

  6. Android的Touch事件处理机制

    Android的Touch事件处理机制比较复杂,特别是在考虑了多点触摸以及事件拦截之后. Android的Touch事件处理分3个层面:Activity层,ViewGroup层,View层. 首先说一 ...

  7. 移动端的touch事件处理

    简要的探讨一下移动端 touch 事件处理几个坑,以及相应的简单处理方法. click 穿透 假设有个弹出层,上面有个关闭的按钮支持 touchend 触发后关闭,若正好下方有个元素支持 click ...

  8. 自定义View系列教程06--详解View的Touch事件处理

    深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...

  9. iOS开发教程之:iPhone开发环境搭建

    安装条件: 硬件:一台拥有支持虚拟技术的64位双核处理器和2GB以上内存的PC. 注意:运行MAC OS,需要电脑支持虚拟技术(VT),安装时,需要将VT启动,在BIOS中开启. 软件: Window ...

随机推荐

  1. Proving Equivalences(加多少边使其强联通)

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. BI系统的应用组织思路与数据分析模式

    BI商业智能软件一般都会提供若干数据整合.数据查询.分析与评价.数据可视化及数据分享的手段,但是在BI项目的构建与实施过程中,如果不按照一定的应用组织思路.数据分析模式及分析流程使用这些工具或手段,呈 ...

  3. LeetCode 二叉树的最小深度

    计算二叉树的最小深度.最小深度定义为从root到叶子节点的最小路径. public class Solution { public int run(TreeNode root) { if(root = ...

  4. Vmware中Ubuntu挂ISO到虚拟光驱

    Ubuntu 下挂ISO到虚拟光驱的方法 https://i.cnblogs.com/EditPosts.aspx?opt=1 如果要ubuntu找到rpm软件包,需要把iso挂载到/media/cd ...

  5. c语言: 文件io, 拷贝文件(二进制)

    #include <stdio.h> #include <stdlib.h> #define TRAN_SZIE 1024 int copy_bin(char* from, c ...

  6. CoffeeScript 入门笔记

    写在前面: 被英文版指南坑了...闹了很久才明白.coffee怎么用.安装前需要有稳定版 Node.js, 和 npm (Node Package Manager). 借助 npm 可以安装 Coff ...

  7. PHP学习笔记9-生成图片

    用PHP代码在网页上生成图片 <?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/6/29 * Time: 2 ...

  8. Linux系统目录(转载)

    /         (这就是著名的根)├── bin         (你在终端运行的大多数程序,比如cp.mv...)├── boot         (内核放在这里,这个目录也经常被作为某个独立分 ...

  9. 第三章 视图和URL配置

    在Mysite文件夹中,创建一个views.py的空文件,输入: from django.http import HttpResponse def hello(request): return Htt ...

  10. ASP.NET JQuery 随笔-搜索框默认提示

    一.文本框中创建默认文本提示 通常用户在搜索内容时,在文本框输入内容前,文本框都会给出默认提示,提示用户输入正确的内容进行搜索. 当文本框获得焦点,如果文本框内容跟提示内容一样,提示内容会自然消失. ...