iPhone UIView 动画禁用 UIButton 子视图

iPhone UIView Animation Disables UIButton Subview(iPhone UIView 动画禁用 UIButton 子视图)

本文介绍了iPhone UIView 动画禁用 UIButton 子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I've got a problem with buttons and animations. Basically, I'm animating a view using the UIView animations while also trying to listen for taps on the button inside the view. The view is just as large as the button, and the view is actually a subclass of UIImageView with an image below the button. The view is a subview of a container view placed in Interface Builder with user interaction enabled and clipping enabled. All the animation and button handling is done in this UIImageView subclass, while the startFloating message is sent from a separate class as needed.

如果我不做动画,buttonTapped: 消息会正确发送,但在动画期间不会发送.我也尝试过实现 touchesEnded 方法,并且发生了相同的行为.

If I do no animation, the buttonTapped: message gets sent correctly, but during the animation it does not get sent. I've also tried implementing the touchesEnded method, and the same behavior occurs.

UIImageView 子类初始化(我将按钮填充了颜色,因此我可以看到框架设置正确,确实如此):

UIImageView subclass init (I have the button filled with a color so I can see the frame gets set properly, which it does):

- (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;
}

启动动画的动画方法(如果我不调用这个按钮可以正常工作):

Animation method that starts the animation (if I don't call this the button works correctly):

- (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 动画可以有效地禁用按钮.
  • 禁用动画会使按钮工作.
  • 按钮在屏幕上的大小和位置都正确,并随着视图正确移动.

推荐答案

动画只是养眼.动画滞后于视图的实际移动.动画开始时,按钮已经位于目标点.您只是看到了视图/按钮移动的电影.

The animation is just eye candy. The animation lags behind the actual movement of the view. The button is already at the destination point when the animation starts. You just see a movie of the view/button moving.

如果您希望按钮在动画期间可点击,您必须自己制作动画.

If you want a button to be clickable during the animation, you'll have to make the animation yourself.

这篇关于iPhone UIView 动画禁用 UIButton 子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iPhone UIView 动画禁用 UIButton 子视图