简体   繁体   English

带有核心动画缩放的UIButton不会更改命中区域

[英]UIButton scaled with core animation doesn't change hit area

I have a uibutton that I animate to scale down. 我有一个uibutton,可以对其进行缩放。 But the hit area doesn't scale down with it and stays at its orignal size. 但是,命中区域不会随其缩小,而是保持其原始大小。

my code: 我的代码:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];

float widthScale = size.width / view.frame.size.width;
float heightScale = size.height / view.frame.size.height;

animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(widthScale, heightScale, 1.0)];

CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:animation];
animGroup.duration = 0.5;
animGroup.removedOnCompletion = NO;
animGroup.fillMode = kCAFillModeForwards;

[view.layer addAnimation:animGroup forKey:nil];

The button has a image that i set with setImage:forState: I give the button a frame and i put the autoresizeSubviews to YES 该按钮具有我使用setImage:forState设置的图像:我给按钮一个框架,然后将autoresizeSubviews设置为YES

This is an old discussion but I just met a similar situation and the question doesn't seem to be really answered... 这是一个古老的讨论,但是我只是遇到了类似的情况,而且这个问题似乎并没有得到真正的回答。

Layers do not respond to touches directly (touches delegate is only the UIView's controller but not its underlaying CALayer). 图层不会直接响应触摸(触摸委托仅是UIView的控制器,而不是其底层CALayer)。 A possible solution to fix the hit area after animating the layer is to modify the view.frame property (of the backing view) adapting its size and origin explicitly/"manually" to the final (expected) size of the animated layer. 在对图层进行动画处理后修复命中区域的一种可能解决方案是修改(后视图的) view.frame属性,使其大小和原点明确/“手动”适应动画图层的最终(预期)大小。

The transformed view.frame will allow again response to touches in a correct area without affecting the animated CALayer shape. 转换后的view.frame将允许在正确区域中再次响应触摸,而不会影响动画CALayer形状。 In other words, the CALayer object and the frame property of the UIView seem untied. 换句话说,CALayer对象和UIView的frame属性似乎没有约束。

CAAnimation scales/etc. CAAnimation比例/等 the view's underlying layer; 视图的基础层; the view itself occupies the same bounds. 视图本身占据相同的边界。 To modify the view itself, try using a UIView animation block and modifying the view's transform property. 要修改视图本身,请尝试使用UIView动画块并修改视图的transform属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM