【问题标题】:How can I use this animation code?如何使用此动画代码?
【发布时间】:2010-04-04 07:37:36
【问题描述】:

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

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

我在网上找到了一些试图做类似事情的人的代码。他/她把这个放在一起,但没有演示或说明。

由于我是新手,我不知道如何将其合并到我的代码中。

这是我需要显示有弹性图像的例程:

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

这是我找到的代码:

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

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

【问题讨论】:

  • 如果 Felixyz 的回答解决了您的问题,您应该接受它。

标签: iphone objective-c cocoa-touch iphone-sdk-3.0


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    相关资源
    最近更新 更多