【问题标题】:replace a UIview animation by a timer animation用计时器动画替换 UIview 动画
【发布时间】:2011-09-24 06:02:41
【问题描述】:

这是我的代码:

- (CGPoint)randomPointSquare {
CGRect frame = [[self view] frame];

//CGFloat rand_x = ((float)arc4random() / (float)UINT_MAX) * (float)_window.frame.size.width;

NSInteger side = arc4random() / (UINT_MAX/4);
CGFloat offset = 0;

switch(side) {
    case 0: /* top */
        offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.width;
        return CGPointMake(offset, -10);

    case 1: /* bottom */
        offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.width;
        return CGPointMake(offset, frame.size.height-150);

    case 2: /* left */
        offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.height;
        return CGPointMake(-10, offset);

    default:
    case 3: /* right */
        offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.height;
        return CGPointMake(frame.size.width+170, offset);
}
}



-(void)createNewImageView {
UIImage * image = [UIImage imageNamed:@"abouffer_03.png"];
imageView = [[UIImageView alloc] initWithImage:image];


// Add it at a random point
[imageView setCenter:[self randomPointSquare]];
[[self view] addSubview:imageView];

// Animate it into place
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:8.0f];
[imageView setCenter:CGPointMake(240, 160)];
[UIView commitAnimations];
 [imageView release];
}

所以你可以看到有一个 UIView 动画,但现在我想用一个定时器来改变和替换它。但我不知道该怎么做。好吧,如果你知道怎么做,别忘了我希望 imageView 在随机位置创建,然后移动到屏幕中心。谢谢

【问题讨论】:

    标签: iphone xcode animation timer uiimageview


    【解决方案1】:

    与其为你编写所有代码,不如给你一些建议,帮助你改变它(我认为这是最好的学习方式)。

    首先,当您在对randomPointSqaure 的调用中创建位置时,将创建的点存储为实例变量。

    theLocation = [self randomPointSquare];
    

    然后将imageView的位置设置为this

    [imageView setCenter:theLocation];
    

    计算对象与您希望的点(240、160)之间的距离,然后除以您希望它移动的时间量。例如,如果您的位置是 (0, 160),那么它将移动 240 像素。如果您希望超过 10 秒,则为每秒 24 像素。将其保存在实例变量中作为“delta”

    现在创建一个调用方法来更新位置的计时器

    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updatePosition) userInfo:nil repeats:YES];
    

    然后创建updatePosition方法

    - (void)updatePosition {
        ...
        // Calculate the new position
        // delta is divided by 100 as the timer runs every 0.01 second
        // and delta was calculated previously as being distance per second
        theLocation = theLocation + (delta / 100);
        ...
        [imageView setCenter:theLocation];
    
        // Check if the location is at the "collision point" and do whatever if it is
        if (theLocation.x == 240 && theLocation.y == 160) {
           // We've hit the "collision point"
        }
    }
    

    这应该会给您一些关于如何使用计时器而不是动画的想法。这就是游戏执行相同操作的方式 - 您只需以小步骤更新对象的位置,然后每次检查碰撞等。

    【讨论】:

    • 我不太明白这部分内容:“计算对象与您希望的点之间的距离 (240, 160) 并将其除以您希望它移动的时间量over" 因为我不知道图像位置是在哪里创建的,因为它是一个随机位置。你能开发你的代码吗?
    • 您已经获得了创建它的随机位置 - 请参阅theLocation = [self randomPointSquare]; 要计算距 (240,160) 的距离,您可以简单地将 x 位置和 y 位置相减。因此,如果 theLocation.x 为 120,theLocation.y 为 100,则沿 x 的距离为 120,沿 y 的距离为 60。
    • 是的,我明白这一点,但我需要知道如何计算距离和坐标......如果你给我一个示例代码,我会更容易理解:/
    • 正如我在之前的评论中所说的 - 只需将 x 位置和 y 位置相减即可。您要开始的位置是 CGPoint,而您要前往的位置是 CGPoint。因此,只需从另一个的 x 和 y 中减去一个的 x。这将为您提供每个距离。然后只需将这些距离除以您希望它移动的时间(以秒为单位)。
    【解决方案2】:

    我不知道您是想在几秒钟后显示随机设置的图像,还是希望将动画从随机位置移动到中心位置。不管怎样,我把这两个选项都放在这里了

    1)

    首先,一旦你在下面创建了图像视图调用函数:

    -(void)randomlySetImage{
        int x = arc4random() % (int)self.view.frame.size.width;
        int y = arc4random() % (int)self.view.frame.size.height;
    
       imageView.center = CGPointMake(x,y);
    
       [NSTimer scheduledTimerWithTimeInterval:1.0f
                                        target:self 
                                      selector:@selector(randomlySetImage) 
                                      userInfo:nil 
                                       repeats:YES];
    }
    

    2)

    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:2.0f] forKey:kCATransactionAnimationDuration];
    
    CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGMutablePathRef positionPath = CGPathCreateMutable();
    CGPathMoveToPoint(positionPath, NULL, randomX ,  randomY );
    CGPathAddLineToPoint(positionPath, NULL, self.view.center.x , self.view.center.y);
    positionAnimation.path = positionPath;
    
    [[imageView layer] addAnimation:positionAnimation forKey:@"PositionAnimation"];
    
    [CATransaction commit];
    

    希望对你有帮助

    【讨论】:

    • randomX 和 randomY 是从哪里来的,有方法吗?
    • 可以找到randomX和randomY int randomX = arc4random() % (int)self.view.frame.size.width; int randomY = arc4random() % (int)self.view.frame.size.height;
    • 是的,但是我已经做了一个随机位置的方法,我希望这些随机位置不是你的,是否可以将我的与你的相关联
    • 我认为,您想使用您的方法移动带有计时器的图像视图,但是如果您设置起点和终点并将此任务委托给 CAAnimation 类,这将变成静态的,它将处理动画。
    • 我使用您的代码,但 xcode 给出错误:“CATransaction undeclared”“CAKeyframeAnimation undeclared”和“kCATransactionAnimationDuration undeclared”。我不知道如何声明它们
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多