Iphone SDK:移动并旋转以触摸

| 我有一条鱼的图像-如果用户触摸屏幕,我希望鱼“触摸”到触摸点并移动到那里。如果触摸已经移动,我希望鱼不断跟随触摸。 我怎样才能做到这一点?     
已邀请:
将是这样的:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *tap = [touches anyObject];
    CGPoint pointToMove = [tap locationInView:self.view];

    CGFloat xdiff = pointToMove.x-myFish.center.x;
    CGFloat ydiff = pointToMove.y-myFish.center.y;

    if (!CGRectContainsPoint(myFish.frame, pointToMove)) {
        CGFloat angle = 0;
        if (xdiff) {
            if (xdiff>0) {
                angle = atanf(ydiff/xdiff);
            } else {
                angle = M_PI + atanf(ydiff/xdiff);
            }
        }
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3f];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];        
        myFish.transform = CGAffineTransformMakeRotation(angle);
        [UIView commitAnimations];
    } 

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];    
    myFish.center = pointToMove;
    [UIView commitAnimations];
}
并且您可能也想实现这些:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
编辑:更新了代码,现在旋转是在单独的动画中完成的,因此鱼的旋转速度比他游泳的速度还快。     

要回复问题请先登录注册