我该如何使用这个动画代码?

时间:2021-01-13 20:05:26

I'm a newbie and I need some help.

我是新手,我需要一些帮助。

I want to display a popup image over a given UIView, but I would like it to behave like the UIAlertView or like the Facebook Connect for iPhone modal popup window, in that it has a bouncy, rubbber-band-like animation to it.

我想在给定的UIView上显示一个弹出图像,但我希望它的行为类似于UIAlertView或类似于Facebook Connect for iPhone模式弹出窗口,因为它有一个弹性的,类似rubbber-band的动画。

I found some code on the net from someone who was trying to do something similar. He/she put this together, but there was no demo or instructions.

我在网上发现了一些尝试做类似事情的人的代码。他/她把它放在一起,但没有演示或说明。

Being that I am so new, I don't have any idea as to how to incorporate this into my code.

由于我是如此新颖,我不知道如何将其融入我的代码中。

This is the routine where I need the bouncy image to appear:

这是我需要出现弹性图像的例程:

- (void) showProductDetail
{
. . .
    ////////////////////////////////////////////////////////////////////////
    // THIS IS A STRAIGHT SCALE ANIMATION RIGHT NOW. I WANT TO REPLACE THIS 
    // WITH A BOUNCY RUBBER-BAND ANIMATION
        _productDetail.transform = CGAffineTransformMakeScale(0.1,0.1);
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        _productDetail.transform = CGAffineTransformMakeScale(1,1);
        [UIView commitAnimations];      
    }
. . .
}

This is the code I found:

这是我找到的代码:

float pulsesteps[3] = { 0.2, 1/15., 1/7.5 };
- (void) pulse {
    self.transform = CGAffineTransformMakeScale(0.6, 0.6);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:pulsesteps[0]];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(pulseGrowAnimationDidStop:finished:context:)];
    self.transform = CGAffineTransformMakeScale(1.1, 1.1);
    [UIView commitAnimations];
}

- (void)pulseGrowAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {   
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:pulsesteps[1]];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(pulseShrinkAnimationDidStop:finished:context:)];
    self.transform = CGAffineTransformMakeScale(0.9, 0.9);
    [UIView commitAnimations];
}

- (void)pulseShrinkAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:pulsesteps[2]];
    self.transform = CGAffineTransformIdentity;
    [UIView commitAnimations];
}

Thanks in advance for any help that you can give me.

提前感谢您提供给我的任何帮助。

1 个解决方案

#1


2  

It's pretty simple. In your showProductDetail method, you start an animation block, then set the _productDetail.transform property, and then commit the block. This is what makes the animation happen.

这很简单。在showProductDetail方法中,启动动画块,然后设置_productDetail.transform属性,然后提交块。这就是动画发生的原因。

The code that you found is designed to do a chain of such animations, but the property being modified is on self instead of on _productDetail. If _productDetail is an instance of a class that you created yourself, you can put the animation code inside that class.

您找到的代码旨在执行一系列此类动画,但正在修改的属性是在self而不是_productDetail上。如果_productDetail是您自己创建的类的实例,则可以将动画代码放在该类中。

Otherwise, just move the code inside the pulse method to the showProductDetail method, and put the two other methods below that method. Replace self.transformwith _productDetail.transformin all three methods.

否则,只需将pulse方法中的代码移动到showProductDetail方法,并将另外两个方法放在该方法下面。将self.transform替换为_productDetail.transformin这三种方法。

#1


2  

It's pretty simple. In your showProductDetail method, you start an animation block, then set the _productDetail.transform property, and then commit the block. This is what makes the animation happen.

这很简单。在showProductDetail方法中,启动动画块,然后设置_productDetail.transform属性,然后提交块。这就是动画发生的原因。

The code that you found is designed to do a chain of such animations, but the property being modified is on self instead of on _productDetail. If _productDetail is an instance of a class that you created yourself, you can put the animation code inside that class.

您找到的代码旨在执行一系列此类动画,但正在修改的属性是在self而不是_productDetail上。如果_productDetail是您自己创建的类的实例,则可以将动画代码放在该类中。

Otherwise, just move the code inside the pulse method to the showProductDetail method, and put the two other methods below that method. Replace self.transformwith _productDetail.transformin all three methods.

否则,只需将pulse方法中的代码移动到showProductDetail方法,并将另外两个方法放在该方法下面。将self.transform替换为_productDetail.transformin这三种方法。