如何使用按钮修改xcode中UIView的动画合成率

时间:2011-03-24 01:05:17

标签: xcode animation uiview duration

我希望有人可以提供帮助。我有一个相当标准的UIView动画,唯一真正的动画是在循环上设置的CGAffineTransformMakeScale。我希望有两个按钮,一个可以提高比例的速率(或者更准确地减少动画持续时间),另一个可以降低比例的速率(或更准确地增加动画持续时间)。

这甚至可能吗?我很抱歉,如果这是显而易见的,但我是一个新手,并寻求建议 - 即使它是直接阅读。如果我应该提供任何进一步的信息来帮助我。

非常感谢提前!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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