【问题标题】:UITapGestureRecognizer not work when I animate the UIImageView当我为 UIImageView 设置动画时,UITapGestureRecognizer 不起作用
【发布时间】:2012-12-03 15:31:22
【问题描述】:

当我想为UIImageView 设置动画时,添加到其中的UITapGestureRecognizer 无法工作。为什么???

-(void) testTap:(id)sender {
    NSLog(@"Test tap...");
}

-(void) testSlide {
    UITapGestureRecognizer* testTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(testTap:)] autorelease];
    testTap.numberOfTapsRequired = 2;

    UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tip_slide"]] autorelease];
    [imageView setFrame:CGRectMake(40, 40, 200, 200)];
    imageView.userInteractionEnabled = YES;
    imageView.multipleTouchEnabled = YES;
    [imageView addGestureRecognizer:testTap];

    [self.view addSubview:imageView];


    // When I add the following code, the UITapGestureRecognizer will not work. WHY???
    imageView.alpha = 0;
    CGAffineTransform t = imageView.transform;
    if (CGAffineTransformIsIdentity(t)) {
        UIViewAnimationOptions options = UIViewAnimationCurveEaseInOut;
        [UIView animateWithDuration:1.0 delay:0 options:options animations:^{
            imageView.alpha = 1.0;
        } completion:^(BOOL finished) {
            if (finished) {
                [UIView animateWithDuration:1.0 delay:2.0 options:options animations:^{
                imageView.alpha = 0.4;
                } completion:^(BOOL finished) {
                    if (finished) {
                        [imageView removeFromSuperview];
                    }
                }];
            }
        }];
    }
}

【问题讨论】:

    标签: ios animation gesture uitapgesturerecognizer


    【解决方案1】:

    您需要在动画期间允许用户交互。

    UIViewAnimationOptions options = UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction;
    

    【讨论】:

    • 天啊。花了这么多时间来弄清楚为什么它不起作用。你的答案是救命稻草!
    • 另外值得注意的是:将视图的 alpha 值设置为 0 会导致交互失败。为了解决这个问题,我将我的值设为低值 view.alpha = 0.05 而不是 0。
    • 这是 Swift 5 的解决方案:UIView.animate(withDuration: 0.2, delay: 0.0, options: [.allowUserInteraction], animations: { self.backgroundColor = newColor })
    【解决方案2】:

    只是为 Swift 添加这个......这样的东西会很好用...... 使用 .AllowUserInteraction

    UIView.animateWithDuration(0.4, delay: 1.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 20, options: UIViewAnimationOptions.AllowUserInteraction, animations:
                    {self.frame = originalFrame}, completion: nil)
    

    【讨论】:

    • 如果你有很多动画选项,你可以像这样传递它们:``` UIView.animateWithDuration(1.0, delay: 0.2, options: [.CurveEaseOut, .Repeat, .Autoreverse, .AllowUserInteraction],动画:{ self.leftArrowImageView.alpha = 0.0 },完成:nil)```
    猜你喜欢
    • 2012-07-18
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 2021-08-20
    • 2012-09-23
    • 2017-12-10
    相关资源
    最近更新 更多