gpt4 book ai didi

ios - UIButton 按下/释放动画奇怪地不一致

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:00 26 4
gpt4 key购买 nike

我正在尝试编写一个自定义 UIButton 子类,它将在按下和释放期间“动画化”。

按下时,按钮应“收缩”(向其中心)至其原始大小的 90%。释放时,按钮应“扩展”到 105%,再次收缩到 95%,然后恢复到原来的大小。

这是我现在得到的代码:

#pragma mark -
#pragma mark - Touch Handling
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self animatePressedDown];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
[self animateReleased];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self animateReleased];
}

- (void)animatePressedDown {
NSLog(@"button.frame before animatedPressedDown: %@", NSStringFromCGRect(self.frame));
[self addShadowLayer];
CATransform3D ninetyPercent = CATransform3DMakeScale(0.90f, 0.90f, 1.00f);
[UIView animateWithDuration:0.2f
animations:^{
self.layer.transform = ninetyPercent;
}
completion:^(BOOL finished) {
NSLog(@"button.frame after animatedPressedDown: %@", NSStringFromCGRect(self.frame));
}
];
}
- (void)animateReleased {
[self.shadowLayer removeFromSuperlayer];
CATransform3D oneHundredFivePercent = CATransform3DMakeScale(1.05f, 1.05f, 1.00f);
CATransform3D ninetyFivePercent = CATransform3DMakeScale(0.95f, 0.95f, 1.00f);
[UIView animateWithDuration:0.1f
animations:^{
self.layer.transform = oneHundredFivePercent;
}
completion:^(BOOL finished) {
NSLog(@"button.frame after animateReleased (Stage 1): %@", NSStringFromCGRect(self.frame));
[UIView animateWithDuration:0.1f
animations:^{
self.layer.transform = ninetyFivePercent;
}
completion:^(BOOL finished) {
NSLog(@"button.frame after animateReleased (Stage 2): %@", NSStringFromCGRect(self.frame));
[UIView animateWithDuration:0.1f
animations:^{
self.layer.transform = CATransform3DIdentity;
self.layer.frame = self.frame;
}
completion:^(BOOL finished) {
NSLog(@"button.frame after animateReleased (Stage 3): %@", NSStringFromCGRect(self.frame));
}
];
}
];
}
];
}

无论如何,上面的代码工作得很好……有些时候。在其他时候,按钮动画按预期工作,但在“释放”动画之后,按钮的最后一帧从其原始位置向上和向左“移动”。这就是为什么我有那些 NSLog 语句,以准确跟踪按钮的框架在动画的每个阶段的位置。当“转变”发生时,它发生在 animatePressedDownanimateReleased 之间的某处。至少,animatePressedDown 中显示的框架始终是我所期望的,但是animateReleased 中框架的第一个值经常是错误的.

虽然我的应用程序中的相同按钮在不同应用程序运行期间表现一致,但我看不出这种疯狂的模式。

我对我的所有按钮都使用了自动布局,所以我无法弄清楚使一个按钮正确运行而另一个按钮改变其位置的区别是什么。

最佳答案

我真的很讨厌自己回答问题,但我想通了。

(我已经为这个问题苦苦挣扎了好几天,但在我问了这个问题之后它终于解决了。它不是总是这样吗?)

显然,我遇到的问题是(在“正在移动的”按钮上)我在动画中间更改了按钮的标题。

一旦我设置了一个基于 block 的系统来仅在按钮的“反弹”/“弹出”动画完成后调用标题更改,问题就消失了。

我仍然不知道为什么会这样工作,因为我设置的标题不会改变按钮的整体大小,但是按照描述设置按钮解决了我的问题。

这是我的自定义 UIButton 子类的代码,添加了 block 属性:

@interface MSSButton : UIButton {

}
@property (copy , nonatomic) void(^pressedCompletion)(void);
// Other, non-related stuff...
@end

@implementation MSSButton
#pragma mark -
#pragma mark - Touch Handling
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self animatePressedDown];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
[self animateReleased];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self animateReleased];
}

- (void)animatePressedDown {
[self addShadowLayer];
CATransform3D ninetyPercent = CATransform3DMakeScale(0.90f, 0.90f, 1.00f);
[UIView animateWithDuration:0.2f
animations:^{
self.layer.transform = ninetyPercent;
}
];
}

- (void)animateReleased {
[self.shadowLayer removeFromSuperlayer];
CATransform3D oneHundredFivePercent = CATransform3DMakeScale(1.05f, 1.05f, 1.00f);
CATransform3D ninetyFivePercent = CATransform3DMakeScale(0.95f, 0.95f, 1.00f);
[UIView animateWithDuration:0.1f
animations:^{
self.layer.transform = oneHundredFivePercent;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.1f
animations:^{
self.layer.transform = ninetyFivePercent;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.1f
animations:^{
self.layer.transform = CATransform3DIdentity;
}
completion:^(BOOL finished) {
if (self.pressedCompletion != nil) {
self.pressedCompletion();
self.pressedCompletion = nil;
}
}
];
}
];
}
];
}
@end

由于我的 IBActionanimateReleased 之前触发(由于我的触摸处理方法中列出的方法顺序),我只需设置 pressedCompletion block 在我的 IBAction 中,标题在动画序列结束时更改。

关于ios - UIButton 按下/释放动画奇怪地不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25329094/

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