按钮微动算法中的错误 [英] error in Button Jiggle algorithm

查看:57
本文介绍了按钮微动算法中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个按钮微动动画.按下按钮会摆动,但问题是动画不会停止并在 [self.layer removeAllAnimations] 中出现错误;下面是代码;

i have impletemented a button jiggle animation.where pressing a button wobbles ,but the problem is the animation does'nt stop and getting an error in [self.layer removeAllAnimations]; below is the code;

 -(IBAction)button1Clicked:(id)sender
 {
UIButton *no1 =sender;
output= [self answerCheck:no1.titleLabel.text];
self.label.text=output;
[self enableOptions:NO];
[self loadingView];
[self startJiggling:2];

 }
- (void)startJiggling:(NSInteger)count
{

    CGAffineTransform leftWobble = CGAffineTransformMakeRotation(degreesToRadians( kAnimationRotateDeg * (count%2 ? +1 : -1 ) ));
    CGAffineTransform rightWobble = CGAffineTransformMakeRotation(degreesToRadians( kAnimationRotateDeg * (count%2 ? -1 : +1 ) ));
    CGAffineTransform moveTransform = CGAffineTransformTranslate(rightWobble, -kAnimationTranslateX, -kAnimationTranslateY);
    CGAffineTransform conCatTransform = CGAffineTransformConcat(rightWobble, moveTransform);

    self.btnOption1.transform = leftWobble;  // starting point

    [UIView animateWithDuration:0.1
                          delay:(count * 0.08)
                        options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                     animations:^{ self.btnOption1.transform = conCatTransform; }
                     completion:nil];
 [self stopJiggling];
}

-(void)stopJiggling 
{
    [self.btnOption1.layer removeAllAnimations];
    self.btnOption1.transform = CGAffineTransformIdentity;   // Set it straight 
}

推荐答案

您正在 self.btnOption1 上设置动画,因此您需要将其从 self.btnOption1:

You're setting the animation on self.btnOption1, so you would need to remove it from self.btnOption1:

- (void)stopJiggling {
    [self.btnOption1.layer removeAllAnimations];
    self.btnOption1.transform = CGAffineTransformIdentity;
}

但事实上,如果你只是再次设置按钮的 transform 属性,在动画块之外,它会移除动画:

but in fact if you just set the transform property of the button again, outside of an animation block, it will remove the animation:

- (void)stopJiggling {
    self.btnOption1.transform = CGAffineTransformIdentity;
}

(这在我的测试项目中有效.)

(This worked in my test project.)

我注意到您延迟开始动画,并且您在调用 animateWithDuration:... 后立即调用 stopJiggling 立即代码>.我不知道你为什么要使用延迟或为什么要立即调用 stopJiggling.

I notice that you're starting the animation with a delay, and you're calling stopJiggling immediately after you call animateWithDuration:.... I don't know why you're using a delay or why you're calling stopJiggling immediately.

我创建了一个测试用例来匹配您的代码:

I created a test case to match your code:

@implementation ViewController {
    __unsafe_unretained IBOutlet UIButton *btnOption1;
}

- (IBAction)startJiggling {
    btnOption1.transform = CGAffineTransformMakeRotation(-.1);
    [UIView animateWithDuration:.1 delay:2 * 0.08 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{
        btnOption1.transform = CGAffineTransformMakeRotation(.1);
    } completion:nil];
    [self stopJiggling];
}

- (void)stopJiggling {
    [btnOption1.layer removeAllAnimations];
    btnOption1.transform = CGAffineTransformIdentity;
}

@end

我将我的 btnOption1 ivar 连接到一个按钮,并将该按钮连接到 startJiggling 方法.使用如图所示的代码,单击按钮什么也不做,因为动画在添加后立即被删除.如果我注释掉 removeAllAnimations 消息,单击该按钮会使按钮开始抖动并永远抖动.我在 iPhone 4.3 模拟器、iPhone 5.0 模拟器、iPhone 5.1 模拟器和运行 iOS 5.1 的 iPhone 4S 上进行了测试.

I hooked up my btnOption1 ivar to a button, and connected the button to the startJiggling method. With the code as shown, clicking the button does nothing, because the animation is removed immediately after it is added. If I comment out the removeAllAnimations message, clicking the button makes the button start jiggling and it jiggles forever. I tested on the iPhone 4.3 simulator, the iPhone 5.0 simulator, the iPhone 5.1 simulator, and my iPhone 4S running iOS 5.1.

所以,我无法重现您的问题.发送 removeAllAnimations 会删除我测试中的动画.

So, I could not reproduce your problem. Sending removeAllAnimations removes the animation in my test.

我怀疑您只是希望动画重复两次然后停止(因为您有一个名为 count 的参数并且您正在传递 2).如果这就是你想要做的,你可以这样做:

I suspect that you just want the animation to repeat twice and then stop (since you have an argument named count and you're passing 2). If that's what you want to do, you can do it like this:

- (IBAction)startJiggling {
    btnOption1.transform = CGAffineTransformMakeRotation(-.1);
    [UIView animateWithDuration:.1 delay:2 * 0.08 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{
        [UIView setAnimationRepeatCount:2];
        btnOption1.transform = CGAffineTransformMakeRotation(.1);
    } completion:^(BOOL completed){
        btnOption1.transform = CGAffineTransformIdentity;
    }];
}

您使用 +[UIView setAnimationRepeatCount:] 在动画块内设置重复计数,并在完成块中恢复按钮的变换.

You set the repeat count inside the animation block using +[UIView setAnimationRepeatCount:], and you restore the button's transform in the completion block.

这篇关于按钮微动算法中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆