开发者

increase and decrease an imageView's width in one second

开发者 https://www.devze.com 2023-04-13 06:37 出处:网络
S开发者_开发技巧o here is my question: I have an image \'image\' and I would like to increase is width of two and decrease it of two in one second with an animation. and I wanna do this every second

S开发者_开发技巧o here is my question: I have an image 'image' and I would like to increase is width of two and decrease it of two in one second with an animation. and I wanna do this every second . How can I do this please ? sorry for my english I'm french :/


Hi you can use CoreAnimation With CALayer of UIImageView just copy past this code.

CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulseAnimation.duration = .5;
pulseAnimation.toValue = [NSNumber numberWithFloat:1.1];
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pulseAnimation.autoreverses = YES;
pulseAnimation.repeatCount = FLT_MAX;

UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon.png"] ];
[imgView setFrame:CGRectMake(0, 0, 50, 50)];
[self.view addSubview:imgView];
CALayer *layer = imgView.layer;
[layer addAnimation:pulseAnimation forKey:nil];


UIImage * imgOne;

imgOne.transform = CGAffineTransformMakeScale(2,1);

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
imgOne.transform = CGAffineTransformIdentity;
[UIView commitAnimations];


Well start by creating two floats that have the value of the original view of the image, lets say float1 and float2.

image.imageView.bounds.size.width = float1;
image.imageView.bounds.size.height = float2;

Then, create an integer called x, and inside your ccTime function increment x by one every time(this will cause x to be called 60 times a second) so now you can set up statements like...

if(x > 60){
float1 = float1 + (how much bigger you want the image to be)
float2 = float2 + (how much bigger you want the image to be)
}

If you would have provided some example code, I would have been able to answer your question better :/

And if your not using cocos2d, then you will have to use NSTimer instead of ccTime.


  1. Put it in an animation block
  2. Use a selector

    - (void)startSizeChange {
        [UIView beginAnimations:@"changeSize" context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        [UIView setAnimationDidStopSelector:@selector(endSizeChange)];
        [UIView setAnimationDuration:0.5];
        imageview.frame.size.height = 100;
        [UIView commitAnimations]; 
     }
    
    - (void)endSizeChange {
    
        [UIView beginAnimations:@"changeSize" context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        [UIView setAnimationDidStopSelector:@selector(startSizeChange)];
        [UIView setAnimationDuration:0.5];
        imageview.frame.size.height = 50;
        [UIView commitAnimations]; 
     }
    


Try this:

- (void)startSizeChange {

    [UIView beginAnimations:@"changeSize" context:nil];
    [UIView setAnimationCurve:
    [UIView setAnimationDuration:0.3];
    YourImageView.frame = CGRect(YourImageView x + 10 , YourImageView y + 10, YourImageView weight - 10, YourImageView height - 10);
    [UIView commitAnimations]; 

    [self endSizeChange];
 }

- (void)endSizeChange {

    [UIView beginAnimations:@"changeSize" context:nil];
    [UIView setAnimationCurve:
    [UIView setAnimationDuration:0.3];
    YourImageView.frame = CGRect(YourImageView x - 10 , YourImageView y - 10, YourImageView weight + 10, YourImageView height + 10);
    [UIView commitAnimations]; 
 }
0

精彩评论

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

关注公众号