用计时器动画替换 UIview 动画

发布于 2024-11-17 15:27:38 字数 1526 浏览 1 评论 0原文

这是我的代码:

- (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,然后将其移动到屏幕中心。谢谢

well here is my code :

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

SO as you can see there is a UIView animation, but now I want to change and replace it by a Timer. But I don't know how to do it . Well, if you know how to do it, don't forget that I want that the imageView is created at a random position and then it moves to the center of the screen. thank you

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

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

发布评论

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

评论(2

笑忘罢 2024-11-24 15:27:38

我不会为您编写所有代码,而是会给您一些可能帮助您更改代码的指示(我认为这是最好的学习方式)。

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

theLocation = [self randomPointSquare];

然后将 imageView 的位置设置为此

[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"
    }
}

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

Rather than writing all the code for you, I'll give you some pointers that might help you change it (the best way of learning, I think).

First, when you create the position in the call to randomPointSqaure, store the point created as an instance variable.

theLocation = [self randomPointSquare];

Then set the location of the imageView to be this

[imageView setCenter:theLocation];

Calculate how far the object is from the point you want it to be (240, 160) and divide that by the amount of time you want it to move over. For example, if your location is (0, 160) then it will be travelling 240 pixels. If you want it over 10 seconds, it will be 24 pixels per second. Save this in an instance variable as the "delta"

Now create a timer that calls a method to update the position

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

And then create the updatePosition method

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

This should hopefully give you some ideas on how you can do it with a Timer instead of animations. This is how a game would perform the same thing - you just update the positions of the objects in small little steps and then check for collisions etc each time.

晨曦÷微暖 2024-11-24 15:27:38

我不知道你是否想要在几秒钟后显示随机设置的图像,或者你想要从随机位置移动到中心位置的平移动画。不管怎样,我已经把这两个选项都放在这里了

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

希望这有帮助

I do not know whether you want to display image randomly set after few seconds, or you want translation animation from moving random position to center position. Anyways, I have put both options over here

1)

First of all, once you create image view call function below:

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

Hope this helps

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