4

我正在尝试使用动画蒙版制作圆形蒙版图像视图,并使用不同的解决方案。下面的示例完成了这项工作,但我遇到了两个问题:

1)为什么我不能使图像可点击?添加例如。一个UITapGestureRecognizer不起作用。我的猜测是,掩码会阻止触摸操作传播到视图层次结构中的较低级别。

2)动画蒙版运行非常快,我无法使用UIView块动画调整持续时间

我该如何解决这些?

- (void) addCircle {
    // this is the encapsulating view
    //
    base = [[UIView alloc] init];
    //
    // this is the button background
    //
    base_bgr = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"c1_bgr.png"]];
    base_bgr.center = CGPointMake(60, 140);
    [base addSubview:base_bgr];
    //
    // icon image
    //
    base_icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"c1_ico.png"]];
    base_icon.center = CGPointMake(186*0.3/2, 182*0.3/2);
    base_icon.transform = CGAffineTransformMakeScale(0.3, 0.3);
    [base addSubview:base_icon];
    //
    // the drawn circle mask layer
    //
    circleLayer = [CAShapeLayer layer];
    // Give the layer the same bounds as your image view
    [circleLayer setBounds:CGRectMake(0.0f, 0.0f, [base_icon frame].size.width,
                                      [base_icon frame].size.height)];
    // Position the circle
    [circleLayer setPosition:CGPointMake(186*0.3/2-7, 182*0.3/2-10)];
    // Create a circle path.
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:
                          CGRectMake(0.0f, 0.0f, 70.0f, 70.0f)];
    // Set the path on the layer
    [circleLayer setPath:[path CGPath]];

    [[base layer] setMask:circleLayer];

    [self.view addSubview:base];
    base.center = CGPointMake(100, 100);
    base.userInteractionEnabled = YES;
    base_bgr.userInteractionEnabled = YES;
    base_icon.userInteractionEnabled = YES;

    //
    // NOT working: UITapGestureRecognizer
    //
    UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapit:)];
    [base_icon addGestureRecognizer:tapgesture];

    //
    // BAD but WORKS :) properly positioned UIButton over the masked image
    //
    base_btn = [UIButton buttonWithType:UIButtonTypeCustom];
    base_btn.frame = CGRectMake(base.frame.origin.x, base.frame.origin.y, base_icon.frame.size.width, base_icon.frame.size.height);
    [base_btn addTarget:self action:@selector(tapit:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:base_btn];
}

这是点击处理程序,这是蒙版动画。无论我尝试的持续时间是多少,它的动画速度都很快 - 大约 0.25 秒,我无法调整它。

- (void) tapit:(id) sender {
    //...
        [UIView animateWithDuration:1.0
                              delay:0.0
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^ {
                             [circleLayer setTransform:CATransform3DMakeScale(10.0, 10.0, 1.0)];
                         }
                         completion:^(BOOL finished) {
                             // it is not necessary if I manage to make the icon image tappable
                             base_btn.frame = [base convertRect:base_icon.frame toView:self.view];  
                         }];
    }    
}
4

1 回答 1

1

1) 触摸不会向下传播,base因为它是在没有框架的情况下启动的,所以它的框架将为 CGRectZero。视图不会收到超出其范围的触摸事件。base只需设置一个包含整个点击目标的有效框架。

2)setTransform:在图层上调用一个隐式动画,它使用核心动画的默认持续时间 0.25(你猜对了:))。最好的解决方案是使用CABasicAnimation而不是UIView基于 - 的动画。像这样的东西:

CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.toValue = @(10.0f);
scaleAnimation.duration = 2;
[circleLayer addAnimation:scaleAnimation forKey:nil];

注意:默认情况下,CABasicAnimation完成后会从图层中移除,并且图层将恢复为旧值。您可以通过例如将该动画的removedOnCompletion属性设置为NO并稍后使用CALayer'removeAnimationForKey:方法自行删除它来防止它(只需设置一个键而不是在nil添加动画时传递),但这取决于您想要完成的具体操作。

于 2013-11-02T23:28:41.433 回答