我正在使用平移手势,在它移动之后,我试图让它以触摸发生的角度移动。我想在平底锅发生时获得触摸点,然后当用户抬起手指时。之后计算点的角度并使用它来移动。我不确定我在这里做错了什么。
-(void) dragging:(UIPanGestureRecognizer *)p{
UIView *v = p.view;
CGPoint velocity;
if(p.state == UIGestureRecognizerStateBegan){
self.origC = v.center;
self.pointIn = [p locationInView:[UIApplication sharedApplication].keyWindow];
//NSLog(@"%f",self.pointIn.x);
//pointInWindow = CGPointMake(pointIn.x, pointIn.y);
}
//NSLog(@"%f,%f orig %f, %f",pointInWindow.x, pointInWindow.y,self.origC.x,self.origC.y);
//velocity = [p velocityInView:v.superview];
//NSLog(@"x=%f",velocity.x);
//NSLog(@"y=%f",velocity.y);
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationCurveEaseInOut animations:^{
CGPoint delta = [p translationInView:v.superview];
CGPoint c = self.origC;
c.x += delta.x;
c.y += delta.y;
v.center = c;
}completion:nil];
if(p.state == UIGestureRecognizerStateEnded){
CGPoint lastPoint = [p locationInView:[UIApplication sharedApplication].keyWindow];
NSLog(@"orig %f , last %f",self.pointIn.x,lastPoint.x);
self.lateC = v.center;
CGFloat deltaX = self.pointIn.x - lastPoint.x;
CGFloat deltaY = self.pointIn.y - lastPoint.y;
double angle = atan(deltaX/deltaY) * 180/M_PI;
if(angle < 0)
angle += 360;
//NSLog(@"%f",angle);
self.addy = angle;
self.addx = angle;
}
}
这是在我的ViewController中:
-(void) animateBalls:(NSTimer*) time{
for(Ball *balls in self.view.subviews){
[UIView beginAnimations:nil context:nil];
//[UIView setAnimationRepeatAutoreverses:NO];
CGFloat x;
CGFloat y;
for(Ball *subBalls in self.view.subviews){
if(balls != subBalls)
if((CGRectIntersectsRect([balls frame], [subBalls frame]))){
[subBalls changeDirectionX];
[subBalls changeDirectionY];
}
}
CGPoint point = balls.center;
if(point.x + 25 > self.view.bounds.size.width)
[balls changeDirectionX];
if(point.x - 25 < 0)
[balls changeDirectionX];
if(point.y + 25 > self.view.bounds.size.height)
[balls changeDirectionY];
if(point.y - 25 < 0)
[balls changeDirectionY];
if(balls.directionX == 1){
x = point.x + (2 * cos(balls.addx));
}
else{
x = point.x - (2 * cos(balls.addx));
//balls.addx;
}
if(balls.directionY == 1){
y = point.y + (2 * sin(balls.addx));
}
else
y = point.y - (2 * sin(balls.addx));
//balls.addy;
//CGFloat x = (CGFloat) random()/(CGFloat) RAND_MAX * self.view.bounds.size.width;
//CGFloat y = (CGFloat) random()/(CGFloat) RAND_MAX * self.view.bounds.size.height;
CGPoint squarePostion = CGPointMake(x, y);
balls.center = squarePostion;
[UIView commitAnimations];
//[self.view setNeedsDisplay];
}
}