繁体   English   中英

iPhone UIView动画禁用UIButton子视图

[英]iPhone UIView Animation Disables UIButton Subview

所以我在按钮和动画方面遇到了问题。 基本上,我正在使用UIView动画为视图创建动画,同时还尝试监听视图内部按钮上的轻击。 该视图与按钮一样大,并且该视图实际上是UIImageView的子类,该图像在按钮下方。 该视图是放置在Interface Builder中的容器视图的子视图,其中启用了用户交互并启用了裁剪。 所有动画和按钮处理都在此UIImageView子类中完成,而startFloating消息是根据需要从单独的类发送的。

如果我不制作动画,则buttonTapped:消息将正确发送,但是在动画过程中不会发送。 我也尝试实现touchesEnded方法,并且会发生相同的行为。

UIImageView子类init(我将按钮填充为一种颜色,以便可以看到框架已正确设置,它确实如此):

- (id)initWithImage:(UIImage *)image {
    self = [super initWithImage:image];
    if (self != nil) {
        // ...stuffs

        UIButton *tapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        tapBtn.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        [tapBtn addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        tapBtn.backgroundColor = [UIColor cyanColor];
        [self addSubview:tapBtn];

        self.userInteractionEnabled = YES;
    }
    return self;
}

启动动画的动画方法(如果我不调用此按钮,则该按钮将正常工作):

- (void)startFloating {
    [UIView beginAnimations:@"floating" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:10.0f];
    self.frame = CGRectMake(self.frame.origin.x, -self.frame.size.height, self.frame.size.width, self.frame.size.height);
    [UIView commitAnimations];
}

因此,要明确:

  • 使用UIView动画可有效禁用该按钮。
  • 禁用动画会使按钮起作用。
  • 该按钮的大小和位置正确在屏幕上,并随视图一起正确移动。

这样可以解决问题:

[UIView animateWithDuration:20 delay: 0.0 options: UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
  ...

动画只是糖果。 动画落后于视图的实际移动。 动画开始时,该按钮已位于目标点。 您只看到视图/按钮移动的电影。

如果要在动画过程中单击按钮,则必须自己制作动画。

...遇到了同样的问题,因为我的代码每个块都做了一个大动画。 我制作了一个基于NSTimer的解决方案,就像上面建议的那样,并且奏效了……但是动作却很生涩(除非我在每个计时器事件触发器中插入了动画)。

因此,由于仍然需要动画,因此我找到了不需要计时器的解决方案。 它仅对短距离进行动画处理,因此按钮单击仍然准确,只有一个小错误,在我的情况下,这在UI中是非常不明显的,并且可以根据您的参数进行减少。

请注意,任何给定时间的误差均小于15.0,可以根据动画速度要求降低误差,以提高准确性。 您还可以减少持续时间以提高速度。

- (void)conveyComplete:(UIView*)v
{
    [self convey:v delay:0];
}

- (void)convey:(UIView*)v delay:(int)nDelay
{
    [UIView animateWithDuration:.5 
                          delay:nDelay
                        options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)  
                     animations: ^
                    {
                        CGRect rPos = v.frame; 
                        rPos.origin.x -= 15.0;
                        v.frame = rPos;
                    }
                    completion: ^(BOOL finished)
                    {
                        [self conveyComplete:v];
                    }];

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM