0

我希望有人能帮忙。我有一个相当标准的 UIView 动画,其中唯一真正的动画是设置在循环上的 CGAffineTransformMakeScale。我想要两个按钮,一个是增加比例的速率(或更准确地说是减少动画持续时间),一个是降低比例的速率(或更准确地说是增加动画持续时间)。

这甚至可能吗?如果这很明显,我深表歉意,但我是新手,正在寻求建议——即使是定向阅读。让我知道我是否应该提供任何进一步的信息来提供帮助。

提前谢谢了!

4

2 回答 2

1

我不知道修改进行中动画的方法。

这是我认为可以满足您要求的代码。它使用增加/减少按钮来更改 ivar animationDuration,并手动将动画循环为两半(前半部分和后半部分)。每次新动画(正向或反向)开始时,它都会获取该animationDuration时间点的值,因此对于短动画,它看起来会立即发生变化。但是,它不适用于长时间的动画,因为它实际上只会在动画的最大/最小点改变动画速度。

如果您想更频繁地更新,您可以将动画分解得更小 - 例如,将其分成 4 个而不是 2 个(1 -> 1.5、1.5 -> 2.0、2.0 -> 1.5、1.5 -> 1.0)或者更多,如果你需要它。

头文件(MyViewController.h):

// Define some constants to be edited to suit our needs

#define kAnimationDurationMin 0.1
#define kAnimationDurationDefault 0.4
#define kAnimationDurationMax 2.0
#define kAnimationDurationStep 0.05

@interface MyViewController : UIViewController {
    // The variable to store the target animation duration in
    double animationDuration;
    // Whether the next animation should be a reverse animation or not
    BOOL reverse;
    // The view to be animated, this should be connected in Interface Builder
    IBOutlet UIView *ball;
}

//The method to actually do the animation
-(void)doAnimation;

// These 2 methods should be connected to the 'touch up inside' event of the relevant buttons in Interface Builder
- (IBAction)incButtonPressed:(id)sender;
- (IBAction)decButtonPressed:(id)sender;
@end

实现(MyViewController.m):

#import "MyViewController.h"

@implementation MyViewController
-(void)viewDidLoad {
    // If animation duration has not yet been set (it will be zero) then set it to the default.
    if (animationDuration < kAnimationDurationMin) animationDuration = kAnimationDurationDefault;   
    // Start the animation
    [self doAnimation];
}

// This method does the animation
-(void)doAnimation {
    [UIView beginAnimations:@"ball" context:NULL];
    [UIView setAnimationDuration: animationDuration];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    // Do not repeat
    [UIView setAnimationRepeatCount: 0];
    // Not autoreversing allows us to trigger the animationDuration change twice as frequently.
    [UIView setAnimationRepeatAutoreverses:NO];
    // When the animation is complete, start it again by calling [self doAnimation];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(doAnimation)];
    if (reverse) {
        // Reset to default
        ball.transform = CGAffineTransformMakeScale(1,1);
    } else {
        // Target of forward animation
        ball.transform = CGAffineTransformMakeScale(2,2);       
    }
    // Toggle reverse
    reverse = !reverse;
    [UIView commitAnimations];
}

- (IBAction)incButtonPressed:(id)sender {
    animationDuration += kAnimationDurationStep;
    if (animationDuration > kAnimationDurationMax) animationDuration = kAnimationDurationMax;
}

- (IBAction)decButtonPressed:(id)sender {
    animationDuration -= kAnimationDurationStep;
    if (animationDuration < kAnimationDurationMin) animationDuration = kAnimationDurationMin;
}
@end
于 2011-03-25T08:30:10.993 回答
0

你想要 UIView 类的 setAnimationDuration :

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
view.transform = CGAffineTransformMakeScale(0.5, 0.5);
[UIView commitAnimations];

以下是相关文档:

https://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW62

于 2011-03-24T20:51:52.780 回答