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下载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值