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

时间:2023-03-09 14:45:24
【转】IOS的处理touch事件处理(依照手指的移动移动一个圆,开发环境用的ios7,storyboard)-- 不错

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

先看下页面的效果图:

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

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