【问题标题】:"Drag 'n' Drop" game animation“拖放”游戏动画
【发布时间】:2012-03-24 11:48:14
【问题描述】:

我是 Objective-C 的新手,我希望创建一个相当简单的 Jigsaw。我目前只是在研究制作一个 4 块拼图作为学习该方法的一种方式。

目前,我在屏幕周围放置了 4 个片段,当它们拖动到屏幕上的特定位置时,会捕捉到该位置。

我的问题是尝试为触摸设置动画。我希望触摸的图像扩大一点,但语法有问题。我查看了 Apple 提供的 MoveMe 示例,但在这种情况下它对我没有帮助。下面是代码。我很感激这方面的任何帮助。谢谢。

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    //location of current touch
    CGPoint location = [touch locationInView:self.view];
    if ([touch view] == image1) {
        image1.center = location;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        image1.transform = CGAffineTransformMakeScale(1.5, 1.5);  
        [UIView commitAnimations];    
    } else if ([touch view] == image2) {
        image2.center = location;
        animateFirstTouch:image2;
    } else if ([touch view] == image3) {
        image3.center = location;
        image3.transform = CGAffineTransformMakeScale(1.5, 1.5);
    } else if ([touch view] == image4) {
        image4.center = location;
        image4.transform = CGAffineTransformMakeScale(1.5, 1.5);
    }

}

-(void) animateFirstTouch:(UIView *)image {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    self.view.transform = CGAffineTransformMakeScale(1.5, 1.5);  
    [UIView commitAnimations];  
}

image1 的代码有效,图像放大超过 0.5 秒。

image2 的代码抛出错误:表达式结果未用于 animateFirstTouch:image2。

image3 和 image4 的代码放大了没有动画的图像。

【问题讨论】:

    标签: objective-c ios animation drag-and-drop


    【解决方案1】:

    请参阅此链接中 Kuldeep 的回答。我认为这对你有用:

    iPhone/iOS: How to implement Drag and Drop for subviews of a Scrollview?

    也可以参考 iPhone 在此链接中的回答:

    iPhone App: implementation of Drag and drop images in UIView

    -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        //location of current touch
        CGPoint location = [touch locationInView:self.view];
        if ([touch view] == image1) {
           [self animateFirstTouch:image1 withLocation:location];
        } else if ([touch view] == image2) {
           [self animateFirstTouch:image2 withLocation:location];
        } else if ([touch view] == image3) {
           [self animateFirstTouch:image3 withLocation:location];
        } else if ([touch view] == image4) {
            [self animateFirstTouch:image4 withLocation:location];
        }
    
    }
    
    -(void) animateFirstTouch:(UIImageView *)image withLocation:(CGPoint)location {
            image.center = location;
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.5];
            image.transform = CGAffineTransformMakeScale(1.5, 1.5);  
            [UIView commitAnimations];   
    }
    

    希望对你有所帮助。

    如果您需要更多帮助,请与我联系。

    【讨论】:

    • 嗨。谢谢,但这似乎与我真正想要的方法完全不同。我正在尝试学习动画和缩放。据我所知,我已经设置了动画功能,我只是无法将图像传递给该功能。如果您能对此提供帮助,那就太好了。
    • @garethdn:请检查我已编辑我的答案的代码并稍微修改了您的代码并将其添加到我的答案中。请使用该代码。如果您需要有关此方面的帮助,请与我联系。
    • 感谢帕思。有没有办法将这个动画限制在它自己的方法中,就像我上面写的 animateFirstTouch 函数一样。一遍又一遍地重复相同的代码似乎很浪费。或者更好的是,有没有办法说:如果被触摸的对象是图像,则将其动画放大 0.5 秒以上?
    • @garethdn 我已经编辑了答案并添加了修改后的代码。还要检查animateFirstTouch: withLocation: 方法,我还修改了touchesBegan 下的代码。请检查我的答案。我已经编辑了答案。请使用我在答案中发布的代码。如果您需要更多帮助,请与我联系。谢谢
    • 感谢我们一直以来的帮助 Parth。我在上面的代码中遇到了一些错误。对于 [self animateFirstTouch:imageXXX withLocation:location];我收到错误“不兼容的指针类型将“UIImageView *_weak”发送到“UIImage *”类型的参数。方法中还有另一个错误:在“UIImage *”类型的对象上找不到“属性“中心”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    • 2023-01-27
    相关资源
    最近更新 更多