【转】IOS的处理touch事件处理(依照手指的移动移动一个圆,开发环境用的ios7,storyboard)-- 不错
原文网址:http://blog.csdn.net/baidu_nod/article/details/32934565
先看下页面的效果图:
首先定义这个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-day17-01(UIView的拖拽(跟随手指移动))
原文网址:http://www.ithao123.cn/content-7926067.html
源码下载地址:http://download.csdn.net/detail/liu537192/8544289
【转】IOS的处理touch事件处理(依照手指的移动移动一个圆,开发环境用的ios7,storyboard)-- 不错的更多相关文章
- IOS的处理touch事件处理(按照手指的移动移动一个圆,开发环境用的ios7,storyboard)
先看下页面的效果图: 首先定义这个ball它有两个属性和两个方法: @property(nonatomic) CGPoint location; @property(nonatomic) CGFloa ...
- ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境(转)
原文地址:http://blog.csdn.net/shangyuan21/article/details/18153605 我们都知道开发iPhone等ios平台的移动应用时需要使用Mac本,但是M ...
- ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境
我们都知道开发iPhone等ios平台的移动应用时需要使用Mac本,但是Mac本都比较昂贵,所以我们可以采用Windows7上利用VMWare安装Mac操作系统的方法来模拟ios开发环境,达到降低成本 ...
- Cocoa Touch事件处理流程--响应者链
Cocoa Touch事件处理流程--响应者链 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/9264335 转载请注明 ...
- Android touch事件处理流程
前面我们看了key事件的处理流程,相信大家对此已经有了新的认识,这篇文章我打算带领大家来看看稍微复杂些的touch 事件的处理流程.说它复杂是因为key事件本身就key down,up,long pr ...
- Android的Touch事件处理机制
Android的Touch事件处理机制比较复杂,特别是在考虑了多点触摸以及事件拦截之后. Android的Touch事件处理分3个层面:Activity层,ViewGroup层,View层. 首先说一 ...
- 移动端的touch事件处理
简要的探讨一下移动端 touch 事件处理几个坑,以及相应的简单处理方法. click 穿透 假设有个弹出层,上面有个关闭的按钮支持 touchend 触发后关闭,若正好下方有个元素支持 click ...
- 自定义View系列教程06--详解View的Touch事件处理
深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...
- iOS开发教程之:iPhone开发环境搭建
安装条件: 硬件:一台拥有支持虚拟技术的64位双核处理器和2GB以上内存的PC. 注意:运行MAC OS,需要电脑支持虚拟技术(VT),安装时,需要将VT启动,在BIOS中开启. 软件: Window ...
随机推荐
- C# 将list<>泛型集合 转化为 DataTable
使用案例:将页面easy ui 中datagrid表格中的数据,存成json字符串, 通过ajax和ashx传入C#将string类型的json字符串解析成list<>泛型集合, 由于业务 ...
- 奖学金评分系统(系统分析与设计版与Delphi实现代码)
一.系统规划 1.1 项目背景介绍 在奖学金评比过程中,学生综合测评是学校普遍采用的评比手段.对学生实施综合素质测评的目的在于正确评价学生的综合素质,为评奖学金提供依据,实现学生教育管理工作的标准化. ...
- 找不到 com.google.zxing.ResultMetadataType 异常解决
在 https://github.com/zxing/zxing 下载二维码扫描 将 android 导入,code打成jar包运行时 报 06-14 23:43:08.690: E/AndroidR ...
- MySQL 5.1.63 单机配置多实例(简单配置)
需求: 在一台服务器上通过源码编译安装一个版本为5.1.63版本MySQL数据库: 方案:将所有配置文件与数据等均存放在/home/zhaoshuangshuang下.在同一个MySQL中运行两个实例 ...
- 商品库存SKU
一种解决方案(性能垃圾,基本实现功能)商品表 属性集表 属性表 属性值表 SKU表 SKU选项表 属性集和属性之间的中间表表关系商品表 *--------------1 属性集表属性集表 *- ...
- Xcode6中autolayout和sizeclass的使用
一.关于自动布局(Autolayout) 在Xcode中,自动布局看似是一个很复杂的系统,在真正使用它之前,我也是这么认为的,不过事实并非如此. 我们知道,一款iOS应用,其主要UI组件是由一个个相对 ...
- query specified join fetching, but the owner of the fetched association was not present in the select list
报标题的错误,是因为在 select count的时候,不需要fetch 所以在取完count以后,再把fetch加进去,变成left join fetch /** * 请把jhql以o为返回对象, ...
- Spring MVC控制层的返回类型--String类型与Bean类型
SpringMVC控制层的返回类型形式多样,现拿其中的两种--String类型与Bean类型作以说明. 一.测试项目的结构 说明:(jsp的名字没起好) 控制层:UserController.java ...
- jQuery列表拖动排列-jquery list dragsort插件参数和使用方法
在编写网页的时候,有时可能需要对ul的li进行排序,今天就给大家推荐使用jquery插件jquery list dragsort实现列表拖动排序效果. 效果如图: jquery list dragso ...
- ural 1233
可以推出规律 每一个数第一次出现的位置 和 n*10后出现的位置 要特殊考虑 是10的倍数的情况(10,100,1000, .......) 它的位置是不会改变的 #include<cstd ...