gpt4 book ai didi

ios - 圆形 mask View 动画太快,不可点击

转载 作者:可可西里 更新时间:2023-11-01 06:09:47 25 4
gpt4 key购买 nike

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

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

2) ma​​sk 的动画运行速度非常快,我无法使用 UIView block 动画调整持续时间

我该如何解决这些问题?

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

这是点击处理程序,这是 mask 动画。无论我在持续时间内尝试了多少,它的动画速度都很快 - 大约 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。 View 不会获得在其范围之外开始的触摸事件。只需在包含整个点击目标的 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 ),但这取决于您究竟想用它完成什么。

关于ios - 圆形 mask View 动画太快,不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19745470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com