原文网址:http://blog.csdn.net/baidu_nod/article/details/32934565

先看下页面的效果图:

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

@property(nonatomic) CGPoint location;

@property(nonatomic) CGFloat length;

-(CGPoint) getCenterPoint;

-(BOOL) isInTheBall:(CGPoint) point;

方法体是:

  1. //找出ball的中心点
  2. -(CGPoint) getCenterPoint {
  3. return CGPointMake((self.location.x+self.length/2), self.location.y+self.length/2);
  4. };
  5. //看点point是不是在ball的范围内
  6. -(BOOL) isInTheBall:(CGPoint) point{
  7. CGPoint center = self.getCenterPoint;
  8. float t = (point.x - center.x) * (point.x - center.x);
  9. float y = (point.y - center.y) * (point.y - center.y);
  10. float k = sqrtf(t+y);
  11. if (k < self.length/2) {
  12. return YES;
  13. }else {
  14. return NO;
  15. }
  16. };

定义BallView继承UIView

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

初始化函数为:

  1. - (id)initWithBall:(CGRect)frame aBall:(Ball*) ball
  2. {
  3. self = [super initWithFrame:frame];
  4. if (self) {
  5. // Initialization code
  6. self.ball = ball;
  7. }
  8. return self;
  9. }
  10. -(void)awakeFromNib{
  11. self.backgroundColor = nil;
  12. self.opaque = NO;
  13. }
  14. // Only override drawRect: if you perform custom drawing.
  15. // An empty implementation adversely affects performance during animation.
  16. - (void)drawRect:(CGRect)rect
  17. {
  18. // Drawing code
  19. [super drawRect:rect];
  20. CGContextRef contextRef = UIGraphicsGetCurrentContext();
  21. [[UIColor whiteColor] set];
  22. //rect是整个view
  23. CGContextFillRect(contextRef, rect);
  24. [[UIColor redColor] set];
  25. //CGContextAddEllipseInRect不会填充圆圈的内部
  26. // CGContextAddEllipseInRect(contextRef, CGRectMake(200.0f, 200.0f, 50.0f, 50.0f));
  27. CGContextFillEllipseInRect(contextRef, CGRectMake(self.ball.location.x,self.ball.location.y,self.ball.length,self.ball.length));
  28. CGContextStrokePath(contextRef);
  29. }

我们在viewController里初始化只要:

  1. -(void) loadView{
  2. [super loadView];
  3. Ball* ball = [[Ball alloc] init];
  4. ball.location = CGPointMake(200.0f, 100.0f);
  5. ball.length = 80.0f;
  6. BallView* view = [[BallView alloc] initWithBall:[UIScreen mainScreen].bounds aBall:ball];
  7. [self.view addSubview:view];
  8. }

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

  1. -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  2. NSLog(@"touchesBegan");
  3. //下面两句知道手指在屏幕上的点的信息
  4. UITouch* touch = [touches anyObject];
  5. CGPoint point = [touch locationInView:self];
  6. if ([self.ball isInTheBall:point]) {
  7. self.isTouch = YES;
  8. self.prePoint = point;
  9. }else{
  10. self.isTouch = NO;
  11. }
  12. NSLog(@"x=%f,y=%f",point.x,point.y);
  13. }
  14. -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
  15. NSLog(@"touchesMoved");
  16. if (self.isTouch) {
  17. CGRect preRect = CGRectMake(self.ball.location.x, self.ball.location.y, self.ball.length, self.ball.length);
  18. //先用之前的location绘制一遍
  19. [self setNeedsDisplayInRect:preRect];
  20. UITouch* touch = [touches anyObject];
  21. CGPoint point = [touch locationInView:self];
  22. //cx和cy是手指的偏移量,用他们可以计算出新的location
  23. float cx = point.x - self.prePoint.x;
  24. float cy = point.y - self.prePoint.y;
  25. self.ball.location = CGPointMake(self.ball.location.x + cx, self.ball.location.y+cy);
  26. CGRect newRect = CGRectMake(self.ball.location.x, self.ball.location.y, self.ball.length, self.ball.length);
  27. //用新的location绘制一遍
  28. [self setNeedsDisplayInRect:newRect];
  29. self.prePoint = point;
  30. }
  31. }
  32. -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
  33. NSLog(@"touchesEnded");
  34. self.isTouch = NO;
  35. }

代码可以在http://download.csdn.net/detail/baidu_nod/7533317下载

ios-day17-01(UIView的拖拽(跟随手指移动))

原文网址:http://www.ithao123.cn/content-7926067.html

源码下载地址:http://download.csdn.net/detail/liu537192/8544289

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

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

    先看下页面的效果图: 首先定义这个ball它有两个属性和两个方法: @property(nonatomic) CGPoint location; @property(nonatomic) CGFloa ...

  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. jsp日期控件My97DatePicker的使用

    My97DatePicker是一款非常灵活好用的日期控件.使用非常简单. 1.下载My97DatePicker组件包 2.将My97DatePicker包放在项目WebContent目录下 3.在页面 ...

  2. 地形图比例尺、等高距和DEM分辨率关系

    地表面的形态是很复杂的,不同地貌类型的形态是由它的相对高度、地面坡度以及所处的地势所决定的,它们是影响等高距的主要因素。从等高距计算公式可以看出,当地图比例尺和图上等高线间的最小距离简称等高线间距确定 ...

  3. 如何查看windows操作系统当前使用的字符集

    如何查看windows操作系统当前使用的字符集 Chcp 显示活动控制台代码页数量,或更改该控制台的活动控制台代码页.如果在没有参数的情况下使用,则 chcp 显示活动控制台代码页的数量. 语法 ch ...

  4. easy ui datagrid 设置冻结列

    为了冻结列,您需要定义 frozenColumns 属性.frozenColumn 属性和 columns 属性一样. $('#tt').datagrid({ title:'Frozen Column ...

  5. 在树莓派上 搭建sqlite数据库

    最近找工作需要学习一些数据库方面的知识,所以就在实验室的树莓派上准备装个数据库试试,刚开始准备装一个mysql数据库,出现了很多问题,放弃了,后来查了一些资料原来还有很多可以用的小巧实用的数据库,sq ...

  6. java接口与继承

    class Grandparent { public Grandparent() { System.out.println("GrandParent Created."); } p ...

  7. HTTP幂等性

    http://www.cnblogs.com/weidagang2046/archive/2011/06/04/2063696.html 理解HTTP幂等性 基于HTTP协议的Web API是时下最为 ...

  8. Eclipse编辑jsp、js文件时,经常出现卡死现象解决汇总

    使用Eclipse编辑jsp.js文件时,经常出现卡死现象,在网上百度了N次,经过N次优化调整后,卡死现象逐步好转,具体那个方法起到作用,不太好讲.将所有用过的方法罗列如下: 1.取消验证 windo ...

  9. asp.net中@ Import 命令的使用

    @ Import  将命名空间显式导入到 ASP.NET 应用程序文件(如网页.用户控件.母版页或 Global.asax 文件)中,同时使导入的命名空间的所有类和接口可用于文件.导入的命名空间可以是 ...

  10. Window Event 2008

    https://support.microsoft.com/en-us/kb/947226