iPhone SDK:移动和旋转触摸

发布于 2024-11-01 02:45:52 字数 83 浏览 1 评论 0原文

我有一张鱼的图像 - 如果用户触摸屏幕,我希望鱼“看”触摸点并移动到那里。如果触摸已移动,我希望鱼不断跟随触摸。

我怎样才能做到这一点?

I have an image of a fish - If the user touches the screen I want the fish to "look" at the touched point and move there. If the touch has been moved i want the fish constantly following the touch.

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没结果 2024-11-08 02:45:52

会是这样的:

- (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

编辑:更新了代码,现在旋转是在单独的动画中完成的,因此鱼旋转的速度比游动的速度快。

would be something like this:

- (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];
}

and you probably want to implement these too:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

EDIT: updated code, now the rotation is done in a separate animation, so that the fish rotates faster than he swims.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文