IOS的处理touch事件处理(依照手指的移动移动一个圆,开发环境用的ios7,storyboard)
来源:互联网 发布:c语言字符串转化为数字 编辑:程序博客网 时间:2024/05/21 08:37
先看下页面的效果图:
首先定义这个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下载
0 0
- IOS的处理touch事件处理(依照手指的移动移动一个圆,开发环境用的ios7,storyboard)
- 对于移动端浏览器touch事件的研究总结(4)判断手指滑动方向
- Android官方开发文档Training系列课程中文版:手势处理之记录手指移动的轨迹
- 移动端的touch事件
- 浅谈移动端之touch事件--手指的上滑、下滑、左滑和右滑
- 移动端的touch事件(二)
- 一个可以跟随手指移动的PopupWindow
- Android移动开发-手势事件处理MotionEvent的实现
- js中的移动设备的事件处理
- 角色移动的处理
- Android的Touch事件处理
- 详解移动端的Touch事件
- Android 跟随手指移动的圆
- 跟随手指移动的小球
- 移动开发,背景图片的处理问题
- html5 移动端的手指触屏事件
- 自定义view的进度条与手指移动事件
- [Phonegap+Sencha Touch] 移动开发21 Sencha touch tapHold事件 触发时间太长的解决办法
- ojX
- ZOJ1100 状压DP +深搜
- 用Scheme来写二分查找
- 课程设计之职工工资的管理系统
- Global.asax.cs中的方法的含义
- IOS的处理touch事件处理(依照手指的移动移动一个圆,开发环境用的ios7,storyboard)
- Linux(CentOS6.4)Solr4.8.1中文分词配置(IK分词)
- Python数字
- [NSFileManager defaultManager] 与[[NSFileManager alloc] init]的区别分析
- Android关于AndroidManifest.xml详细分析
- 输入两个整数n 和m,从数列1,2,3.......n 中随意取几个数, 使其和等于m ,要求将其中所有的可能组合列出来
- 我所经历的企业中IT部门在企业内部的地位
- firefox嵌入vlc插件
- HDU2136 Largest prime factor 筛选法求素数位置