iPad - 在屏幕上查看动画时没有用户交互
好吧,这是一个新手问题,所以我提前道歉。我有一个 UIView,我在 Interface Builder 中将其布置在屏幕外。当用户按下按钮时,我想在屏幕上为该视图添加动画效果。它可以工作,但我无法与 UIView 中的任何按钮进行交互。我读过,当您制作动画时,只有视图会移动,并且实际对象保留其位置,因此我尝试设置 UIView 图层的位置,但这会导致我的 UIView 菜单在左侧显示额外的 81 像素。发生这种情况时,我可以与按钮进行交互,但我需要它与屏幕右侧齐平。这是我在 openMenu 按钮上的 IBAction 中的代码:
CABasicAnimation *moveView = [CABasicAnimation animationWithKeyPath:@"position"];
moveView.delegate = self;
moveView.duration=0.5;
// If the menu is displayed, close it!
CGPoint currentPosView = [[entirePage layer] position];
CGPoint destView;
[moveView setFromValue:[NSValue valueWithCGPoint:currentPosView]];
if (menuDisplayed) {
// Move right to our destination
destView.x = currentPosView.x;
destView.y = currentPosView.y + 81;
[moveView setToValue:[NSValue valueWithCGPoint:destView]];
// Otherwise, open it
} else {
// Move left to our destination
destView.x = currentPosView.x;
destView.y = currentPosView.y - 81;
[moveView setToValue:[NSValue valueWithCGPoint:destView]];
}
// Animate the view
[[entirePage layer] addAnimation:moveView forKey:@"move"];
// Set the final position of our layer
[[entirePage layer] setPosition:destView];
menuDisplayed = !menuDisplayed;
然后在animationDidStop 方法中:
CGPoint currentPosMenu = [[menuBar layer] position];
if (menuDisplayed) {
currentPosMenu.x -= 81;
} else {
currentPosMenu.x += 81;
}
[[menuBar layer] setPosition:currentPosMenu];
Help???
Alright this is a newbie question, so I apologize in advance. I have a UIView which I laid out in Interface Builder off-screen. When the user presses a button, I would like to animate this view on-screen. It works, but I can't interact with any buttons in the UIView. I have read that only the view is moved when you animate and that the actual objects retain their positions, so I have tried setting the position of the UIViews' layers, but it causes my UIView menu to display an extra 81 pixels to the left. When this happens, I can interact with the buttons, but I need it to be flush with the right of the screen. Here is my code in the IBAction on the openMenu button:
CABasicAnimation *moveView = [CABasicAnimation animationWithKeyPath:@"position"];
moveView.delegate = self;
moveView.duration=0.5;
// If the menu is displayed, close it!
CGPoint currentPosView = [[entirePage layer] position];
CGPoint destView;
[moveView setFromValue:[NSValue valueWithCGPoint:currentPosView]];
if (menuDisplayed) {
// Move right to our destination
destView.x = currentPosView.x;
destView.y = currentPosView.y + 81;
[moveView setToValue:[NSValue valueWithCGPoint:destView]];
// Otherwise, open it
} else {
// Move left to our destination
destView.x = currentPosView.x;
destView.y = currentPosView.y - 81;
[moveView setToValue:[NSValue valueWithCGPoint:destView]];
}
// Animate the view
[[entirePage layer] addAnimation:moveView forKey:@"move"];
// Set the final position of our layer
[[entirePage layer] setPosition:destView];
menuDisplayed = !menuDisplayed;
And then in the animationDidStop method:
CGPoint currentPosMenu = [[menuBar layer] position];
if (menuDisplayed) {
currentPosMenu.x -= 81;
} else {
currentPosMenu.x += 81;
}
[[menuBar layer] setPosition:currentPosMenu];
Help???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要弄乱图层。您可以通过在 Interface Builder 中将视图布置在它们动画进入视图后所处的位置来实现您想要的效果。换句话说,首先将物品放在最终/正确的位置。不要在 IB 中做任何奇怪的事情。
如果您使用的是 UIViewController),您可以将视图偏移一定量,然后将它们恢复到动画块中的原始位置:
在您的代码中,在应用程序启动后或在 -viewDidAppear: 方法中( 用户对你的看法的互动,这可能有很多事情。如果您的所有视图都是 UIImageView 之类的子视图,则 UIImageView 默认将 userInteractionEnabled 设置为 NO (至少如果您在代码中构建一个视图 - 我不确定 IB 对于图像视图的默认设置是什么)。这也可能意味着您的视图的超级视图实际上太小,并且其框架不包含子视图。 (如果为超级视图打开了剪切,您甚至无法看到存在此特定问题的子视图。)
编辑:
我更仔细地阅读内容,发现您希望内容随着按钮按下而移动,所以换句话说,视图从一开始就需要隐藏。在这种情况下,您仍然可以将 IB 中的所有内容放置在其最终位置,但只需将它们设置为隐藏在 IB 中即可。当您按下按钮时,请在翻译视图之前将视图设置为可见(隐藏=否)。这应该允许它们放置在 IB 中的最终/正确位置,确保它们位于正确的超级视图等上,同时仍然获得您想要的动画效果。
You shouldn't need to mess with the layers. You could probably achieve the effect you want by laying out the views in Interface Builder at the positions they will be in after they are animated into view. In other words, first lay things out in their final/correct positions. Don't do anything funky in IB.
In your code, after the app is launched or in a -viewDidAppear: method if you're using a UIViewController, you could offset the views by some amount and then restore them to their original position in an animation block:
As for the lack of user interaction on your views, that could be a number of things. If all of your views are subviews of something like a UIImageView, UIImageView has userInteractionEnabled set to NO by default (at least if you build one in code - I'm not sure off hand what IB's default settings are for an image view). It could also mean that the superview of your views is actually too small and its frame does not contain the subviews. (If clipping was turned on for the superview, you wouldn't even be able to see subviews that are having this particular problem.)
EDIT:
I read things more carefully and see you want things to move in response to a button press, so in other words the views will need to be hidden from the start. In that case you could still lay everything out in IB in their final position, but just set them as hidden in IB. When you push the button, set the views visible (hidden=NO) before translating them. That should allow them to be laid out in their final/correct place in IB making sure that they are positioned on the proper superviews, etc while still getting the animation effect you want.
暂时忘掉动画吧;您应该通过设置其
frame
属性来移动视图。例如:
在确认视图放置在正确的位置之前,不要担心动画。验证该代码正常工作后,请在设置
frame
属性后执行动画。请注意,您可能根本不需要使用 CoreAnimation API,除非您正在做一些复杂的事情。 UIKit 可以自己做动画。例如:
Forget about the animations for now; you should move your view by setting its
frame
property.E.g.:
Don't worry about the animation until you've verified that your view is being placed in the correct location. Once you've verified that that code is working correctly, then do your animation after you set the
frame
property.Note that you probably don't need to use the CoreAnimation API at all, unless you're doing something complex. UIKit can do animation on its own. E.g: