开发者

After applying while condition code starts acting differently

I'm developing a game in which I want my image to reduce in size gradually. I'm reducing the frame size gradually in my code when it works fine. [I've already used CGAffineTransform and it doesn't suit my requirement.]

-(void)function     
{
    ravanImage1.frame=CGRectMake(150,((ravanImage1.frame.origin.y)-5),q,z);

    if(ravanImage1.center.y>=300&&ravanImage1.center.y<=370)
    {
        q=60;
        z=60;
        ravanImage1.frame=CGRectMake(150,((ravanImage1.frame.origin.y)-5),q,z);
    }   

    if(ravanImage1.center.y>=230&&ravanImage1.center.y<=299)
    {
        q=40;
        z=40;
        ravanImage1.frame=CGRectMake(150,((ravanImage1.frame.origin.y)-5),q,z);
    }

    if(ravanImage1.center.y>=150&&ravanImage1.center.y<=229)
    {   
        q=20;
        z=20;
        ravanImage1.frame=CGRe开发者_运维问答ctMake(150,((ravanImage1.frame.origin.y)-5),q,z);
    }
}

But when I apply a while loop for the same code specifying wheather at what point to stop reducing the frame("while that point isn't reached"), it doesn't show the image frame reduction little by little as it shows it otherwise, but directly places the image at the end point with proper frame. I want it to get displyed the way it gets without the while loop i.e. reduction little by little. Yes, while debugging it steps through all the steps properly. Can anybody please help me?


As others have pointed out, manually adjusting the frame of your view will give you terrible performance. If you really don't want to use a standard UIView animation block for changing your view, you can specify bounds size values to animate through using a CAKeyframeAnimation applied to your view's layer:

CAKeyframeAnimation * customSizeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"bounds.size"];
NSArray *sizeValues = [NSArray arrayWithObjects:[NSValue valueWithCGSize:size1], [NSValue valueWithCGSize:size2], [NSValue valueWithCGSize:size3], nil];
[customSizeAnimation setValues:frameValues];
NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.5f], [NSNumber numberWithFloat:1.0f], nil]; 
[customSizeAnimation setKeyTimes:times];
customSizeAnimation.fillMode = kCAFillModeForwards;
customSizeAnimation.removedOnCompletion = NO;
[view.layer addAnimation: customSizeAnimation forKey:@"customSizeAnimation"];

This animation will start at size1, pass through size2 at the midway point in the animation, and end at size3. You can have an arbitrary number of key frames and times for your animation, so you should be able to achieve the effect you desire

EDIT (1/5/2010): Removed kCAAnimationPaced as a calculationMode, which would cause the key times to be ignored. Also, I forgot that frame was a derived property, so you need to animate something like the bounds size instead.


The reason it does that is because it executes the while loop very quickly. I think the best thing to do is put some sort of a delay timer after each step of the while loop, then you'll see each step and it won't just 'jump' to it's final state.


[self setTimer: [NSTimer scheduledTimerWithTimeInterval: 3.5
                                                                    target: self
                                                                  selector: @selector (function_name)
                                                                  userInfo: nil
                                                                   repeats: YES]];

try using this.


This' my move function in which I'm trying to change the size of my imageView. If you can point out any error, I'll be really grateful..

-(void)move {

UIImageView *imageViewForAnimation = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ravan.jpg"]];

imageViewForAnimation.alpha = 1.0f;
CGSize size1=CGSizeMake(60,60);
CGSize size2=CGSizeMake(40,40);
CGSize size3=CGSizeMake(20,20);
CAKeyframeAnimation *customFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"frame"];
NSArray *sizeValues = [NSArray arrayWithObjects:[NSValue valueWithCGSize:size1], [NSValue valueWithCGSize:size2], [NSValue valueWithCGSize:size3], nil];
[customFrameAnimation setValues:sizeValues];
customFrameAnimation.duration=10.0;
NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.5f], [NSNumber numberWithFloat:1.0f], nil]; 
[customFrameAnimation setKeyTimes:times];

customFrameAnimation.fillMode = kCAFillModeForwards;
customFrameAnimation.removedOnCompletion = NO;
[imageViewForAnimation.layer addAnimation:customFrameAnimation forKey:@"customFrameAnimation"]; [self.view addSubview:imageViewForAnimation]; }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜