如何使用按钮修改 xcode 中 UIView 的动画持续时间

发布于 2024-10-26 03:35:38 字数 241 浏览 4 评论 0原文

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

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

非常感谢!

I'm hoping someone can help. I have a fairly standard UIView animation where the only real animation is a CGAffineTransformMakeScale that is set on a loop. I want to have two buttons, one that increases the rate (or more accurately reduces the animation duration) of scale and one that decreases the rate (or more accurately increases the animation duration) of the scale.

Is this even possible? I apologise if this is obvious but I am a novice and looking for advice - even if it's directed reading. Let me know if I should supply any further info to help.

Many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦旅人picnic 2024-11-02 03:35:38

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

我认为这里的代码可以满足您的要求。它使用增加/减少按钮来更改 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

I don't know of a way to modify an in-progress animations.

Here's code that I think does what you want. It uses the increase/decrease buttons to change an ivar animationDuration, and manually loops the animation as two halves (a forward half and a reverse half). Each time the new animation (forwards or reverse) starts, it gets the value for animationDuration at that point in time, so for short animations it will appear to change pretty much instantly. It would not work well for long duration animations however, as it actually only changes the animation speed at the max/min points of the animation.

You could break the animation up even smaller if you wanted to update more frequently - e.g. split it into 4 instead of 2 (1 -> 1.5, 1.5 -> 2.0, 2.0 -> 1.5, 1.5 -> 1.0) or even more if you need it.

Header (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

Implementation (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
耀眼的星火 2024-11-02 03:35:38

您想要 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/文档/uid/TP40006816-CH3-SW62

You want setAnimationDuration of the UIView class:

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

Here's the relevant documentation:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文