zoukankan      html  css  js  c++  java
  • IOS的处理touch事件处理(按照手指的移动移动一个圆,开发环境用的ios7,storyboard)

    先看下页面的效果图:


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

  • 相关阅读:
    说说那些低级错误
    vue双向数据绑定
    妙用$.extend
    写一个限制执行次数的函数
    强类型语言不好的地方,比如这样:
    form表单里的坑
    Java Day 09
    Java Day 08
    Java Day 07
    Java Day 06
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5112924.html
Copyright © 2011-2022 走看看