gpt4 book ai didi

cocoa - CABasicAnimation后NSView不响应鼠标事件

转载 作者:行者123 更新时间:2023-12-03 17:13:34 25 4
gpt4 key购买 nike

我有一个 pageLeftpageRight 动画,可以对 2 个 View 的位置和 alpha 进行动画处理,总共有 4 个同步 CABasicAnimation:

  1. 将下一页移动到 View 中
  2. 将当前页面移出 View
  3. 淡入下一页
  4. 淡出当前页面

动画和设置一切都很顺利。这个问题是关于页面动画之后的鼠标事件。我发现动画运行后,nextView接收mouseDown:事件。如果由于下面运行了 shouldNotAnimate block 而未运行动画,则 nextView 确实会接收 mouseDown: 事件.

这意味着我设置 CABasicAnimation 和 CATransaction 的方式导致了此问题。我对它可能是什么感到困惑。有什么想法吗?

<小时/>

动画代码:

BOOL forward = currentIndex < index;

if (shouldNotAnimate) {
// handles swapping pages faster than the animation duration
[self.sourceViewContainer setSubviews:[NSArray array]];

[nextView.layer removeAllAnimations];
[nextView setFrame:self.sourceViewContainer.bounds];
[nextView.layer setPosition:self.sourceViewContainer.bounds.origin];
[nextView.layer setOpacity:1.0];
[self.sourceViewContainer addSubview:nextView];

[currentView removeFromSuperview];
[currentView.layer removeAllAnimations];
[currentView setFrame:self.sourceViewContainer.bounds];
[currentView.layer setPosition:self.sourceViewContainer.bounds.origin];
[currentView.layer setOpacity:1.0];

self.animationsRunning = NO;

} else {

self.animationsRunning = YES;

[CATransaction begin];
[CATransaction setCompletionBlock:^{
self.animationsRunning = NO;
}];

// Setup incoming push animation
[nextView setFrame:CGRectMake(self.sourceViewContainer.bounds.size.width * (forward ? 1 : -1), 0, self.sourceViewContainer.bounds.size.width, self.sourceViewContainer.bounds.size.width)];
[self.sourceViewContainer addSubview:nextView];
CABasicAnimation *incomingAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
incomingAnimation.fromValue = [NSValue valueWithPoint:nextView.frame.origin];
incomingAnimation.toValue = [NSValue valueWithPoint:self.sourceViewContainer.bounds.origin];
incomingAnimation.duration = PANEL_PAGE_SWIPE_ANIMATION_DURATION;
incomingAnimation.removedOnCompletion = NO;
incomingAnimation.fillMode = kCAFillModeForwards;
incomingAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
incomingAnimation.completion = ^(BOOL finished) {
[nextView.layer setPosition:self.sourceViewContainer.bounds.origin];
[nextView.layer removeAnimationForKey:@"incoming"];
};

[nextView.layer addAnimation:incomingAnimation forKey:@"incoming"];


// Setup outgoing push animation
CGRect offscreen = CGRectMake(self.sourceViewContainer.bounds.size.width * (forward ? -1 : 1), 0, self.sourceViewContainer.bounds.size.width, self.sourceViewContainer.bounds.size.height);
CABasicAnimation *outgoingAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
outgoingAnimation.fromValue = [NSValue valueWithPoint:self.sourceViewContainer.bounds.origin];
outgoingAnimation.toValue = [NSValue valueWithPoint:offscreen.origin];
outgoingAnimation.duration = PANEL_PAGE_SWIPE_ANIMATION_DURATION;
outgoingAnimation.removedOnCompletion = NO;
outgoingAnimation.fillMode = kCAFillModeForwards;
outgoingAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
outgoingAnimation.completion = ^(BOOL finished) {
[currentView removeFromSuperview];
[currentView.layer setPosition:self.sourceViewContainer.bounds.origin];
[currentView.layer removeAnimationForKey:@"outgoing"];

};
[currentView.layer addAnimation:outgoingAnimation forKey:@"outgoing"];


// Setup incoming alpha animation
CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0f];
fadeInAnimation.duration = PANEL_PAGE_SWIPE_ANIMATION_DURATION;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.fillMode = kCAFillModeForwards;
fadeInAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
fadeInAnimation.completion = ^(BOOL finished) {
[nextView.layer setOpacity:1.0f];
[nextView.layer removeAnimationForKey:@"fadeIn"];
};
[nextView.layer addAnimation:fadeInAnimation forKey:@"fadeIn"];


// Setup outgoing alpha animation
CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeOutAnimation.fromValue = [NSNumber numberWithFloat:1.0f];
fadeOutAnimation.toValue = [NSNumber numberWithFloat:0.0f];
fadeOutAnimation.duration = PANEL_PAGE_SWIPE_ANIMATION_DURATION;
fadeOutAnimation.removedOnCompletion = NO;
fadeOutAnimation.fillMode = kCAFillModeForwards;
fadeOutAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
fadeOutAnimation.completion = ^(BOOL finished) {
[currentView removeFromSuperview];
[currentView.layer setOpacity:1.0f];
[currentView.layer removeAnimationForKey:@"fadeOut"];

};
[currentView.layer addAnimation:fadeOutAnimation forKey:@"fadeOut"];

[CATransaction commit];

} // Run animations
<小时/>

解决方案

我必须在完成 block 中显式设置 View 框架,即使我是在动画调用之前立即完成此操作的。

    [CATransaction begin];
[CATransaction setCompletionBlock:^{
self.animationsRunning = NO;
[nextView setFrame:self.sourceViewContainer.bounds];
}];

最佳答案

我想我知道发生了什么事。

CAAnimation 对象实际上不会更改它们动画的基础属性。相反,它将属性应用于表示层,该表示层是绘制的而不是常规层。当您使用removedOnCompletion = false创建动画时,动画完成后表示层将保持事件状态,但底层属性仍然不会更改。另外,移动 View 的图层不会移动 View 本身,因此它不会响应新位置处的用户交互。

我建议使用 UIView block 动画(UIView animateWithDuration:completion: 及其亲戚)移动 View ,而不是使用 CAAnimation 对象。这些方法实际上将 View 移动到新位置,以便在动画完成后它能够响应最终位置的用户交互。

您可以在一个动画 block 中完成所有移动和 alpha 更改。

关于cocoa - CABasicAnimation后NSView不响应鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13651591/

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