replace a UIview animation by a timer animation

淺唱寂寞╮ 提交于 2020-02-08 10:19:32

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/6505834/replace-a-uiview-animation-by-a-timer-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!