圆形蒙版视图的动画设置过快,无法点击

nzs

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

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];  
                         }];
    }    
}
菲利普·雷迪奇

1)触摸不会从其向下传播,base因为它是在没有框架的情况下启动的,因此其框架将为CGRectZero。视图不会发生超出其边界的触摸事件。只需在base包括整个点击目标的有效框架上进行设置

2)setTransform:在图层上调用隐式动画,该隐式动画使用Core Animation的默认持续时间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,然后稍后使用CALayerremoveAnimationForKey:方法将其删除(只设置一个键而不是传递nil添加动画的权限)来防止它发生,但这取决于您究竟要完成此操作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

CABasicAnimation圆形蒙版显示缩放路径动画

来自分类Dev

在iOS中对UIImageView的圆形蒙版进行动画处理

来自分类Dev

在iOS中对UIImageView的圆形蒙版进行动画处理

来自分类Dev

Matlab中的圆形蒙版

来自分类Dev

CAShapeLayer蒙版视图

来自分类Dev

CALayer圆形蒙版动画行为不正确+ Swift

来自分类Dev

在iOS中进行图像蒙版的圆形蒙版

来自分类Dev

UIView图层蒙版动画

来自分类Dev

UIView上的圆形图层的快速蒙版

来自分类Dev

如何修改蒙版使其完美圆形

来自分类Dev

去除圆形蒙版周围的空白

来自分类Dev

可扩展的圆形svg蒙版

来自分类Dev

FFMpeg:在视频上添加圆形蒙版

来自分类Dev

如何创建圆形半透明蒙版

来自分类Dev

图层蒙版的蒙版。核心动画

来自分类Dev

设置动画时无法点击注释

来自分类Dev

设置Matplotlib Tricontourf的蒙版

来自分类Dev

使用 CAShapeLayer 为蒙版部分的透明度设置动画

来自分类Dev

iOS在UIImage上的动画蒙版

来自分类Dev

创建动画调整蒙版(混合模式)

来自分类Dev

如何从JS制作蒙版SVG动画?

来自分类Dev

SVG 剪切蒙版在使用 CSS 动画时无法按预期工作

来自分类Dev

CAShapeLayer子视图无法正确设置动画

来自分类Dev

Android-在视频上创建圆形蒙版

来自分类Dev

如何在图像上制作圆形蒙版

来自分类Dev

CameraRoll正方形显示和圆形蒙版-UIImagePickerController

来自分类Dev

如何快速在相机上添加圆形蒙版

来自分类Dev

具有模糊和圆形蒙版滤镜的GPUImageVideoCamera

来自分类Dev

CS0120无法从其他脚本设置图层蒙版