由计时器动画更换一个UIView动画 [英] replace a UIview animation by a timer animation

查看:123
本文介绍了由计时器动画更换一个UIView动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好这里是我的code:

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

因此​​,大家可以看到有一个的的UIView 的动画,但现在我想改变,并通过计时器更换。但我不知道该怎么做。好吧,如果你知道怎么做了,别忘了,我想了的的ImageView 的在随机位置创建,然后将其移动到屏幕的中心。谢谢

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

推荐答案

而不是编写所有code你,我给你一些指针,可以帮助你改变它(最好的学习方式,我想)。

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).

首先,当你创建在调用的位置 randomPointSqaure ,存储为一个实例变量创建点。

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

theLocation = [self randomPointSquare];

然后设置ImageView的位置是这样的。

Then set the location of the imageView to be this

[imageView setCenter:theLocation];

计算对象有多远离你希望它是(240,160)和除以你想让它搬过来的时间点。例如,如果您所在的位置为(0,160),那么它将被行驶240像素。如果你希望它在10秒内,这将是每秒24像素。在一个实例变量保存此为三角洲

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

然后创建 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"
    }
}

这样应该可以给你如何你可以用一个定时器,而不是动画做的一些想法。这是一个游戏将如何执行同样的事情 - 你只需更新对象的位置在小的小步骤,然后再检查各碰撞时间等

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.

这篇关于由计时器动画更换一个UIView动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆